如何创建会话变量,一旦注销成功,无需导航到之前访问过的页面。
Login.php和logout.php 页面如下:
的login.php
require( 'dbConfig.php');
session_start();
$msg = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["userid"];
if ($name == '' ) {
$msg = "You must enter all fields";
}
else
{
$sql = "SELECT * FROM user WHERE userid = '$name' ";
$query = mysql_query($sql);
if ($query === false) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($query) > 0) {
$_SESSION['userid'] = $name;
header('Location: teams.php');
exit;
}
$msg = "Username do not match";
}
}
?>
Logout.php
<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>
问题:成功注销后,该页面将导航到之前访问过的页面。
提前感谢任何帮助。
答案 0 :(得分:0)
在 logout.php 页面中,而不是if
条件,只需写下:
session_destroy();
所以你的页面代码是:
<?php
session_start(); // not compulsory to write
session_destroy();
?>