2013-02-11 105 views
0

我目前遇到了登錄問題 - 第一次登錄嘗試總是被拒絕,但第二次嘗試成功。任何想法如何解決這個問題?首次嘗試登錄被拒絕,但第二次嘗試成功

的login.php

if(isset($_POST['pmsubmit'])) 
    { 
    LoginSubmit('pm', 'pmname', 'pmpass'); 
    } 

    if (isset($_POST['tssubmit'])) { 
    LoginSubmit('ts', 'dept', 'tspass'); 
    } 

function LoginSubmit($pm_or_ts, $the_name_input, $the_pass_input) 
{ 
    global $pdo; 
    $salt = "$2a$12ehTk6%^jswam)*usnyruhst"; 
    $posted_name = $_POST[$the_name_input]; 
    $posted_pass = crypt($_POST[$the_pass_input], $salt); 
    // check if password matches the one in the table 
    $query = $pdo->prepare("SELECT * FROM db_pass WHERE pass = :pass"); 
    $query->execute(array(":pass" => $posted_pass)); 
    // if there is a match then we log in the user 
    if ($query->rowCount() > 0) 
    { 
    // session stuff 
    $_SESSION[$the_name] = $posted_name; 
     $_SESSION['id'] = $row['id']; 
    // refresh page 
    header('Location: ' . $pm_or_ts . '/index.php') ; 
    exit; 
    } 
    // if there is no match then we present the user with an error 
    else 
    { 
    echo ' 
    <script> 
    $(function() { 
     $("#dialog-message").dialog({ 
     modal: true, 
     buttons: { 
     Ok: function() { 
      $(this).dialog("close"); 
      } 
     } 
     }); 
    }); 
    </script> 
     <div id="dialog-message" title="Incorrect password"> 
     The password you have provided is incorrect.<br>Please try again. 
    </div> 
    '; 
    } 
} 
?> 

PM/index.php文件

<?php 
session_start(); 
setcookie("pmw", $_SESSION[$thename], time()+7200, '/'); 
require_once("../resources/php/connection.php"); 

if (empty($_SESSION[$thename]) || empty($_COOKIE['pmw'])) { 
    header("Location: ../login.php"); 
    exit; 
} 
?> 

TS/index.php文件

<?php 
session_start(); 
setcookie("ts", $_SESSION[$thename], time()+7200, '/'); 
require_once("../resources/php/connection.php"); 

if (empty($_SESSION[$thename]) || empty($_COOKIE['ts'])) { 
    header("Location: ../login.php"); 
    exit; 
} 
?> 
+0

嗯,首先,你要在你的登錄函數中設置'$ _SESSION [$ the_name]',但'$ the_name'似乎沒有被定義。這就是爲什麼我相信PHP通知應該始終處於開啓狀態(或未定義變量不應超過通知)的原因之一。 – 2013-02-11 13:39:01

+0

你也在設置'$ _SESSION ['id'] = $ row ['id']'並且'$ row'沒有被定義。 – 2013-02-11 13:39:34

+0

@ColinMorelli謝謝你的迴應。我需要做些什麼改變? – methuselah 2013-02-11 13:43:10

回答

2

我想你login.php你沒有開始會話,你直接將值初始化爲$ _SESSION [$ the_name]。所以在login.php文件的開頭寫上session_start()。

相關問題