PHP SESSION注销错误 [英] PHP SESSION Logout Error

查看:45
本文介绍了PHP SESSION注销错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人可能将PHP mySQL函数用于网站的登录部分

Many people may use a PHP mySQL function for login sections for a website

我正在尝试使用此代码;

I am trying to use this code;

在每个内容页面上-检查是否已登录(在每个内容页面的标题中)

ON EACH CONTENT PAGE - TO CHECK IF LOGGED IN (in the header of every content page)

<?php
session_start();
if(! isset($_SESSION["myusername"]) ){
header("location:main_login.php");
}
?>
<html>
<body>
Page Content Here
</body>
</html>

登录脚本(由我的main_login.php页面引用)

THE LOGIN SCRIPT (which is referred to by my main_login.php page)

<?php

ob_start();
$host="ClubEvents.db.9606426.hostedresource.com"; // Host name 
$username="ClubEventsRead"; // Mysql username 
$password="Pa55word!"; // Mysql password 
$db_name="ClubEvents"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_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 "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>

注销代码

<?
session_start();
session_destroy();

header("location:main_login.php");
exit();
?> 

MAIN_LOGIN.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
phpinfo()
?>

但是某些东西不起作用,login_success.php页面仅在登录时才可访问,因此注销不起作用,或者login_success.php检查不起作用.但是我不知道该怎么说,我已经尝试过和他们俩一起玩,而且仍然没有前进的脚步

but something isnt working, the page login_success.php should only be accessable when logged in, so either the logout isnt working, or, the login_success.php check isnt working. But I don't know how to tell, I have tried playing around with them both, and still no further forward

致谢

亨利

推荐答案

我从未遇到以下问题:

unset($_SESSION[$myusername]);

这样做的好处是您只清除登录会话,并且可以根据需要自由地在会话中存储其他信息,因为session_destroy()会清除所有会话数据.

The advantage here is that you are only clearing the log in session and can freely store other information in the session if needed because session_destroy() will clear ALL session data.

还要查看您的代码,如果设置了用户名会话,它将始终将用户发送到登录页面.

Looking at your code as well, it's always sending the user to the login page if the username session is set.

更改:

<?php
session_start();
if( isset($_SESSION[$myusername]) ){
header("location:main_login.php");
}
?>

to(请注意isset之前的!).您只想在未设置会话的情况下重定向到登录页面.

to (notice the ! before isset). You only want to redirect to the login page when the session isn't set.

<?php
session_start();
if(! isset($_SESSION[$myusername]) ){
header("location:main_login.php");
}
?>

评论摘要:

Session[$myusername]更改为Session['myusername'],并且isset检查更改为!isset.这表明该会话不是最初设置的.

Session[$myusername] changed to Session['myusername'] and the isset check was changed to !isset. This identified the session wasn't being set in the first place.

这篇关于PHP SESSION注销错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆