今天我正在学习如何使用会话,在做了一些研究并尝试将会话添加到我的代码后,我遇到了一些问题。首先,如果有人查看下面的登录页面,我会很感激,如果我的会话设置正确,请告诉我:
登录页面(index.php):
<?PHP
if (isset($_POST['submit']))
{
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$file = file_get_contents("data.txt");
if(!strstr($file, "$username||$password"))
{
print '<script> alert ("Sorry! You have entered a Invalid Username or Password."); window.location="index.php"; </script>';
}
if(empty($username))
{
print '<script> alert ("Sorry! You have entered a Invalid Username or Password."); window.location="index.php"; </script>';
}
else
{
function validateUser()
{
session_regenerate_id ();
$_SESSION['valid'] = 1;
$_SESSION['username'] = $username;
}
header("Location: /home.php");
}
}
?>
<!doctype html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black; margin:auto">
<?php include "header.php"; ?>
<div id="content" style="background-color:#EEEEEE; width:500px; height:400px; float: left">
<br>
<form align="center" method="post" action="index.php" >
Username:
<input type="text" name="username" />
<br/>
<br/>
Password:
<input type="password" name="password" />
<br/>
<br/>
<input type="submit" value="Login" name="submit"/>
<input type="button" value="Register" onclick="document.location='registration.php'" />
</form>
</div>
<?php include "footer.php"; ?>
</div>
</body>
</html>
接下来,当用户登录时,我希望会话在所有页面中继续,所以这是我的主页(home.php):
<?php session_start(); ?>
<!doctype html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black; margin:auto">
<?php include "header.php"; ?>
<?php include "toolbar.php"; ?>
<div id="content" style="background-color:#EEEEEE; width:500px; height:375px; float: left">
<p align="center"><?php echo "<b>Welcome</b>, " . $_SESSION['username']; ?></p>
<form align="center" method="link" action="practice.php">
<input type="submit" value="Practice Mode">
</form>
</div>
<?php include "footer.php"; ?>
</div>
</body>
</html>
我不确定我是否正确地执行此操作,我读到我必须在每个页面上使用session_start()来继续会话,但是当我尝试显示以欢迎消息登录的用户的名称时,似乎不起作用。
此外,我制作了一种菜单栏,显示每个页面上都会包含的登录用户,但不再显示用户名:
菜单栏(toolbar.php):
<div id="toolbar" style="width:500px; clear:both; height:25px">
<table border="0">
<tr>
<td>
<form align="left" method="link" action="home.php">
<input type="submit" value="Home">
</form>
</td>
<td style="width:400px"><div align="center"><?php echo "Logged in as: ". $_SESSION['username']; ?></div></td>
<td>
<form align="right" method="link" action="index.php">
<input type="submit" value="Logout">
<?php session_destroy(); ?>
</form>
</td>
</tr>
</table>
</div>
最后一件事,我试图在菜单栏中设置退出按钮,但我不确定它是否正常工作。
如果有人可以查看此代码并给我一些指示,我将不胜感激。
答案 0 :(得分:1)
在开始设置或初始化其中的值之前,您需要使用session_start()
<?PHP
function validateUser($username)
{
session_start();
session_regenerate_id();
$_SESSION['valid'] = 1;
$_SESSION['username'] = $username;
}
if (isset($_POST['submit']))
{
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$file = file_get_contents("data.txt");
if(!strstr($file, "$username||$password"))
{
print '<script> alert ("Sorry! You have entered a Invalid Username or Password."); window.location="index.php"; </script>';
}
if(empty($username))
{
print '<script> alert ("Sorry! You have entered a Invalid Username or Password."); window.location="index.php"; </script>';
}
else
{
validateUser($username);// changed here
header("Location: /home.php");
exit();
}
}
?>
从toolbar.php中删除<?php session_destroy(); ?>
。
注销:
<a href="logout.php">Logout</a>
在logout.php中:
<?php @session_start();
session_unset($_SESSION['username']);
$_SESSION['username']="";
session_destroy();
// header("location:yourhomepage.php");
echo '<META http-equiv="refresh" content="0;URL=yourhomepage.php">';
exit();
?>
答案 1 :(得分:0)
取出toolbar.php中的<?php session_destroy(); ?>
。
因为当你在home.php中<?php include "toolbar.php"; ?>
时,你正在破坏会话,然后它才会被销毁。