2011-11-29 243 views
1

我有我的網頁登錄系統。您創建用戶名和密碼,然後返回主頁以使用正確的登錄詳細信息登錄。我輸入我的詳細信息,它讓我進來,並提出一個警告框,說「歡迎回來」。當我點擊確定後,它會簡要顯示網頁,然後直接回到登錄頁面。我已經開始會話,因爲註冊頁面正常工作。任何想法爲什麼登錄頁面是這樣做的?php登錄會話

的代碼如下所示:

if(empty($_POST['client_username']) || empty($_POST['client_password'])) { 
    die('You did not fill in a required field'); 
    } 


    $qry = sprintf("SELECT client_username, client_password FROM client WHERE client_username = '%s' LIMIT 1", mysql_real_escape_string($_POST['client_username'])); 
    $result = mysql_query($qry); 

    if($result) { 
     if(mysql_num_rows($result) == 0) { 
      die('That username does not exist in our database.'); 
     } 
    } 



    $info = mysql_fetch_assoc($result); 

    if(md5($_POST['client_password']) != $info['client_password']) { 
     die('Incorrect password, please try again.'); 
    } 


    $client_id = mysql_query("SELECT* FROM client WHERE client_id = '$client_id'"); 

    $qry = sprintf("UPDATE client SET client_last_access = NOW() WHERE client_username = '%s'", $info['client_username']); 
    if(!mysql_query($qry)) { 
     die('Error: ' . mysql_error()); 
    } else { 

     $_SESSION['client_username'] = $info['client_username']; 
     $_SESSION['client_password'] = $info['client_password']; 

     $_SESSION['client_id'] = $client_id; 


     echo '<script>alert("Welcome Back");</script>'; 
     echo '<meta http-equiv="Refresh" content="0;URL=pv.php">'; 
    } 
+0

什麼是pv.php?您使用該頁面作爲url回顯元刷新標記。 –

+0

pv.php是主頁,它不應該在那裏? – mayman212

+0

你是否在任何地方調用'session_start()'? – steveo225

回答

1

完全卸下的最後一行。

0

請按照此示例代碼。當然這對你有幫助。

的login.php

<?php session_start(); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Login</title> 
</head> 

<body> 
<?php 
if(isset($_POST['login'])) 
{ 
    $_SESSION['username'] = 'test'; 
    echo '<script type="text/javascript">alert("Welcome Back!");</script>'; 
    echo '<meta http-equiv="Refresh" content="0;URL=suc.php">'; 
} 
?> 
<form name="loginform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<input type="submit" name="login" value="Login" /> 
</form> 
</body> 
</html> 

和suc.php

<?php 
session_start(); 
if($_SESSION['username'] != '') 
{ 
    echo "success"; 
} 
?> 

檢查它。

0

pv.php您的登錄頁面?如果是,刪除線以下:

echo '<meta http-equiv="Refresh" content="0;URL=pv.php">'; 

如果沒有,請確保session_start();是在您使用會話其他網頁的頂部。

編輯

閱讀您的意見。你如何檢查下一頁的會話?

session_start(); 
if(!isset($_SESSION['client_username']) && !isset($_SESSION['client_password']) && !isset($_SESSION['client_id'])) 
{ 
    header("Location: login.php"); 
    exit; 
}