PHP 使用会话登录


PHP Login using Session

我正在学习如何制作登录页面,在那里我学习了有关会话如何工作的更多详细信息。我搜索了不同的网站,直到找到一个带来成功的网站。但是,成功登录后,我想设置用户可以单击其个人资料,编辑个人资料,帐户设置和登录后注销的位置。注销没有问题,但个人资料链接未显示。

以下是测试登录检查:

ob_start();  
// To protect MySQL injection (more detail about MySQL injection)
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM users WHERE email='$email' and password='$password'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count==1){
// Register $username, $password and redirect to file "login_success.php"
session_register('email');
session_register('password');
session_register('userID'); // Tried and didn't work
session_register('lastlogin'); // Tried and didn't work
ob_end_flush();

这是在主页上

session_start(); 
// See if they are a logged in member by checking Session data
$userlinks = "";
if (isset($_SESSION['email'])) {
    // Put stored session variables into local php variable
    $userID = $_SESSION['userID'];
    $username = $_SESSION['username'];
    $lastlogin = $_SESSION['lastlogin'];
    //If session is successful, the following is available: profile, last login,    account and logout.
    $userlinks = '
    <a href="testuser.php?userID=' . $_SESSION['userID'] . '">' . $_SESSION['email'] . '</a> &bull; (' . $_SESSION['lastlogin'] . ') 
    <a href="testuser_account.php">Account</a> &bull; 
    <a href="testlogout.php">Log Out</a>';
} else {
    // If session failed, see if the user has a Damju account.
    $userlinks = 'Have an account? <a href="testlogin.php">Click Here</a> <br/> Not registered? <a href="testreg.php">Click Here</a>';
}

这是我想要的: (a href="testuser.php?userID=#")username(/a)

正如你所看到的,我试图弄清楚自己,所以我不会看起来我没有尝试。

引用

  • http://www.phpeasystep.com/phptu/6.html

PS:我查看了其他链接,但它们几乎以不同的方式制作。混乱。。。

感谢任何理解的人,如果你不理解,请道歉。

使用

$_SESSION['userid'] = $userid;

然后,只要在每个页面上启动会话,您就可以从任何地方访问会话变量

session_start();

$_SESSION['userid'];

例如

$userid= $_SESSION['userid'];
echo $_SESSION['userid'];

如果您甚至想查看会话中的内容,只需执行

print_r($_SESSION);

当用户注销时,请确保取消设置变量,然后销毁会话

unset($_SESSION['userid']);
session_destroy();