Php 会话不是真的被破坏了吗?
我遇到了一个问题,注销后会话没有被销毁。(尽管出于某种原因,它在IE中工作得非常完美……但在任何其他浏览器(chrome、firefox、opera或safari)中都不工作) 这是登录授权的代码Php 会话不是真的被破坏了吗?,php,mysql,session,cookies,session-variables,Php,Mysql,Session,Cookies,Session Variables,我遇到了一个问题,注销后会话没有被销毁。(尽管出于某种原因,它在IE中工作得非常完美……但在任何其他浏览器(chrome、firefox、opera或safari)中都不工作) 这是登录授权的代码 <?php session_start(); $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="testdatabas
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="testdatabase"; // Database name
$tbl_name="users"; // 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");
// email and password sent from form
$enteredEmail=$_POST['email'];
$enteredPassword=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$enteredEmail = stripslashes($enteredEmail);
$enteredPassword = stripslashes($enteredPassword);
$enteredEmail = mysql_real_escape_string($enteredEmail);
$enteredPassword = mysql_real_escape_string($enteredPassword);
$sql="SELECT * FROM $tbl_name WHERE email='$enteredEmail' and password='$enteredPassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $enteredEmail and $enteredPassword, table row must be 1 row
if($count==1){
// Register $enteredEmail, $enteredPassword and redirect to file "redirect.php"
$_SESSION['email']="email";
header('location:redirect.php');
}
else {
header("location:index.php?error");
}
?>
然后它重定向到主页
我将此代码放在标题中以显示用户是否登录
<?php
if (!isset($_SESSION['email']) || $_SESSION['email'] == ''){
include_once('loggedout.php');
}
else {
include_once('loggedin.php');
}
?>
我还将这两个文件放在每页的顶部:
<?php
if ($_SESSION['email']="email" ) {
session_start();
}
?>
及
最后,这是用户单击注销后进入的注销页面的代码:
<?php
session_start();
session_unset();
session_destroy();
$_SESSION = array();
?>
<html>
<head>
<title>Logged Out</title>
</head>
<body>
<p align="center">You have been successfuly logged out.</p>
<p align="center"><a href="home.php">Go back to homepage.</a></p>
</body>
</html>
注销
您已成功注销
但是,在返回主页后,它首先显示为用户已注销,但随后只需重新加载页面即可将用户重新登录
我尝试了很多不同的会话销毁标记,但不管怎样,我都遇到了同样的问题。
(是的,我对整个php都比较陌生,所以非常感谢您的帮助)
有什么想法吗?发生了什么事,怎么解决?
提前谢谢 根据您当前的代码,以下是一些建议:
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
session_unset()
已弃用,不应与$\u会话一起使用
-将其丢弃setcookie
处理程序删除域中设置的cookie:session_start(); // init session vars
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(),'',time()-3600); # unset session id/cookies
}
unset($_SESSION['email']); // this is the key to unsetting your session.
session_destroy(); // destroy session
session_commit(); // commit session write (optional)
// unset session/cookies
$cs = array_keys($_COOKIE);
for ($x=0;$x<count($cs);$x++) setcookie($cs[$x],"",time()-1);
unset($_SESSION['email']); // this is the key to unsetting your session.
session_destroy();
session_commit();
代码(可选):
session_start(); // init session vars
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(),'',time()-3600); # unset session id/cookies
}
unset($_SESSION['email']); // this is the key to unsetting your session.
session_destroy(); // destroy session
session_commit(); // commit session write (optional)
// unset session/cookies
$cs = array_keys($_COOKIE);
for ($x=0;$x<count($cs);$x++) setcookie($cs[$x],"",time()-1);
unset($_SESSION['email']); // this is the key to unsetting your session.
session_destroy();
session_commit();
//取消设置会话/cookies
$cs=数组\u键($\u COOKIE);
对于($x=0;$x谢谢所有的帮助,但是我发现了问题。
显然,我必须对我的注销文件的url非常具体
我把注销链接转到http://domain.com/logout.php
而不是
http://www.domain.com/logout.php
…facepalm如果没有彻底检查代码,您会说它在某些浏览器中有效,而在其他浏览器中无效。这是缓存问题吗?我在多台计算机上进行了测试,仍然只在IE中有效,并且在所有其他浏览器中都存在此问题。:/@RyanKempt@user3817799可能您的Cookie未在其他浏览器中启用。请尝试启用它,因为会话是空的那么一块饼干