我正在尝试使用“记住我”功能和注销选项为“登录”页面编写一个PHP代码。由于我是php的新手,我正在尝试使用静态用户名和密码而不使用ant数据库访问。但我的问题是php会话,我完全不理解,所以当我尝试登录时,它带我欢迎页面,但显示你没有登录。 这是login.php
<?php
@$id=$_POST['username'];
@$pass = $_POST['password'];
$loginErr="";
if(isset($_POST['login']))
{
if($id=="sajan" && $_POST['password']="sajan123")
{
if($_POST['remember_me']=='on' || $_POST['remember_me']== 1)
{
setcookie("user", $_POST['username'] , time()+(86400 * 30),'/');
setcookie("password", $pass = $_POST['password'] , time()+(86400 * 30));
header('location: welcome.php');
exit;
}
else {
setcookie("user", "" , time()-3600,'/');
setcookie("password", "" , time()-3600);
header('location: welcome.php');
exit;
}
}
else $loginErr="invalid username or password";
}
?>
<html>
<head>
<link rel="stylesheet" href="./style.css" type="text/css">
</head>
<body>
<div id="main">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>" name="login">
<h2 style="margin:0 0 15px 0;">LOGIN</h2>
<label>Username</label>
<input type="text" name="username" value="<?php echo @$_COOKIE['user'];?>">
<label>Password</label>
<input type="password" name="password" value="<?php echo @$_COOKIE['password'];?>"/>
<input type="checkbox" name="remember_me" class="check" <?php if(isset($_COOKIE['user']))
{
echo 'checked="checked"';
}
else {
echo '';
}
?>/>
<label style="width:200px;">Remember me</label>
<input class="submit" type="submit" name="login" value="Login" />
<div style="float:left; color:red;"><?php echo $loginErr;?></div>
</form>
</div>
</body>
</html>
这是我的welcome.php页面
<?php
if (!isset($_SESSION['user']))
{
echo "you are not logged in ";
echo isset($_SESSION);
exit;
}
else {
echo "Welcome ".$_COOKIE['user']." .<br>";
echo "Your login is successful. ";
}
?>
和我的logout.php页面
<?php
session_start();
unset($_SESSION);
session_destroy();
session_write_close();
header('Location: login.php');
exit;
?>
答案 0 :(得分:3)
SESSION ,因此首先定义 SESSION 并在欢迎页面中签入以供进一步使用...试试这个...
if($id=="sajan" && $_POST['password']="sajan123")
{
session_start();
$_SESSION['user']=$id; //assgn a value to session when successful login
if($_POST['remember_me']=='on' || $_POST['remember_me']== 1)
{
setcookie("user", $_POST['username'] , time()+(86400 * 30),'/');
setcookie("password", $pass = $_POST['password'] , time()+(86400 * 30));
header('location: welcome.php');
exit;
}
else {
setcookie("user", "" , time()-3600,'/');
setcookie("password", "" , time()-3600);
header('location: welcome.php');
exit;
}
}
答案 1 :(得分:3)
基本上你没有启动session
,你怎么能指望程序在另一页上返回一个会话变量。
试试这个:
<?php
//Starting session
session_start();
@$id=$_POST['username'];
@$pass = $_POST['password'];
$loginErr="";
if(isset($_POST['login']))
{
//Creating session variable
$_SESSION['user'] = $id;
//EDIT: $_POST['password']=="sajan123"
if($id=="sajan" && $_POST['password']=="sajan123")
{
if($_POST['remember_me']=='on' || $_POST['remember_me']== 1)
{
setcookie("user", $_POST['username'] , time()+(86400 * 30),'/');
setcookie("password", $pass = $_POST['password'] , time()+(86400 * 30));
header('location: welcome.php');
exit;
}
else {
setcookie("user", "" , time()-3600,'/');
setcookie("password", "" , time()-3600);
header('location: welcome.php');
exit;
}
}
else $loginErr="invalid username or password";
}
?>
答案 2 :(得分:2)
您在welcome.php中检查了!isset($_SESSION['user'])
,而不是$_COOKIE['user']
答案 3 :(得分:2)
您忘记在登录时设置会话变量尝试此代码将起作用
<?php
if(isset($_POST['login']))
{
if($id=="sajan" && $_POST['password']="sajan123")
{
session_start();
$_SESSION['username']=$_POST['username'];
if($_POST['remember_me']=='on' || $_POST['remember_me']== 1)
{
setcookie("user", $_POST['username'] , time()+(86400 * 30),'/');
setcookie("password", $pass = $_POST['password'] , time()+(86400 * 30));
header('location: welcome.php');
exit;
}
else {
setcookie("user", "" , time()-3600,'/');
setcookie("password", "" , time()-3600);
header('location: welcome.php');
exit;
}
}
else $loginErr="invalid username or password";
}
?>
答案 4 :(得分:1)
您必须首先检查SESSION是否已创建,以便有人访问此页面并且已经记录,它可以正确地重定向他:
<?php
session_start();
if (isset($_SESSION['username']) {
if ($_SESSION['username']=="sajan" && $_SESSION['password']="sajan123") {
header('location: welcome.php');
}
} else if (isset($_POST['login'])) {
$id = $_POST['username'];
$pass = $_POST['password'];
if($id=="sajan" && $_POST['password']="sajan123") {
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
if($_POST['remember_me']=='on' || $_POST['remember_me']== 1) {
setcookie("user", $id, time()+(86400 * 30),'/');
setcookie("password", $pass, time()+(86400 * 30));
header('location: welcome.php');
exit;
}
else {
setcookie("user", "" , time()-3600,'/');
setcookie("password", "" , time()-3600);
header('location: welcome.php');
exit;
}
} else {
$loginErr="invalid username or password";
}
}
?>