我一直在做很多關於php登錄的研究,以及他們如何工作一段時間。幾天前我遇到了這個網站。 http://www.intechgrity.com/create-login-admin-logout-page-in-php-w/#comment-25300。看着它,我注意到它使用了session_register
這是PHP 5.3中的一個大問題。我開始玩這個遊戲,並且我的登錄部分沒有問題,但由於某種原因,我無法獲取用戶名顯示在管理頁面的標題中。我想要的是:<h1>Welcome To Admin Page Username</h1>
,它給了我<h1>Welcome To Admin Page</h1>
。我想知道是否因爲我有2個會話使用相同的變量?在php登錄會話中抓取用戶名
我從教程變化:
check_login.php -
<?php
define(DOC_ROOT,dirname(__FILE__)); // To properly get the config.php file
$username = $_POST['username']; //Set UserName
$password = $_POST['password']; //Set Password
$msg ='';
if(isset($username, $password)) {
ob_start();
include(DOC_ROOT.'/config.php'); //Initiate the MySQL connection
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($username);
$mypassword = stripslashes($password);
$myusername = mysqli_real_escape_string($dbC, $myusername);
$mypassword = mysqli_real_escape_string($dbC, $mypassword);
$sql="SELECT * FROM login_admin WHERE user_name='$myusername' and user_pass=SHA('$mypassword')";
$result=mysqli_query($dbC, $sql);
// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "admin.php"
$_SESSION["admin"]= $myusername;
$_SESSION["password"]= $mypassword;
$_SESSION["name"]= $myusername;
header("location:admin.php");
}
else {
$msg = "Wrong Username or Password. Please retry";
header("location:login.php?msg=$msg");
}
ob_end_flush();
}
else {
header("location:login.php?msg=Please enter some username and password");
}
?>
admin.php的 -
<?php
session_start(); //Start the session
define(ADMIN,isset($_SESSION["name"])); //Get the user name from the previously registered super global variable
if(isset($_SESSION["admin"])){ //If session not registered
header("location:login.php"); // Redirect to login.php page
}
else //Continue to current page
header('Content-Type: text/html; charset=utf-8');
?>
<!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" xml:lang="en" lang="en">
<head>
<title>Welcome To Admin Page Demonstration</title>
</head>
<body>
<h1>Welcome To Admin Page <?php echo ADMIN /*Echo the username */ ?></h1>
<p><a href="logout.php">Logout</a></p> <!-- A link for the logout page -->
<p>Put Admin Contents</p>
</body>
</html>
這是唯一的2頁我做了改變。謝謝你的幫助。
更新:我厭倦了大家誰回答變化,仍然有一個空白。我只想放棄它。它令我困惑,它如何與舊代碼一起工作,但與新代碼無關。感謝所有的幫助。
更新2:得到它的工作感謝Gumbo有關使用var_dump();
的建議。我所做的是我從* check_login.php *中取得了一切,並將其放入admin.php,並從array(0){}
變爲array(3) { ["username"]=> string(6) "admin" ["admin"]=> string(6) "admin" ["password"]=> string(5) "************" }
。
感謝大家的全力幫助。
您應該刪除對strips strips的調用。 'mysqli_real_escape_string'足以防止注入。 – webbiedave 2011-05-04 21:19:42