我正在将此 PHP 代码用于登录脚本:
<?php
if(isset($_POST["submit"]))
{
session_start();
//get the username, password and keyword sent from the form
$username=$_POST['username'];
$password=$_POST['password'];
//check in the database to see if the username, password and keyword match in the database
$sql="SELECT * FROM admin WHERE username=('$username') and password=MD5('$password')";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
$check="SELECT * from admin where username='$username' ";
$check2=mysql_query($check,$conn) or die(mysql_error());
$check3=mysql_fetch_array($check2);
if($check3["logintries"] > '3')
{
echo '<p align="center"><h4>Your account has been suspended due to too many failed logins. Please contact support</h4></p>';
}
else
{
//get the number of rows that match in the database
$count=mysql_num_rows($rs);
//if the number of rows equals 1, then create the session variables
if($count==1)
{
//$sql="INSERT into user_logins (user_seq, timestamp, ip_address, posted_username, posted_password, posted_keyword) values ('".$result["sequence"]."', '".date("Y-m-d H:i:s")."', '".$_SERVER["REMOTE_ADDR"]."', '".$username."', '".$password."', '".$keyword."') ";
//$rs=mysql_query($sql,$conn) or die(mysql_error());
session_start();
$_SESSION["sequence"]=$result["sequence"];
$_SESSION["loggedin"]='yes';
echo $_SESSION["loggedin"];
$_SESSION["ipaddress"]=$_SERVER["REMOTE_ADDR"];
//then redirect to the main page
//header("location: index.php");
echo '<h3>Login Has Been Successful - Please wait while we redirect you...</h3>';
//echo '<meta http-equiv="refresh" content="0;URL=index.php" />';
}
else
{
$sql="SELECT * FROM admin WHERE username='".$_POST["username"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
$logintries=$result["logintries"];
$sql2="UPDATE admin set logintries = '".($logintries+1)."' where username = '".$result["username"]."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
//other wise display an error message
//echo '<p align="center"><h4>Username or Password incorrect</h4></p>';
}
}
}
?>
如您所见,我已经回显了会话变量“loggedin”,它在页面上回显为是,但是当它转到 member.php 页面时,它不保留变量。
在 member.php 页面上,我包含了一个包含以下 PHP 代码的 authorisation.php:
<?php
if($_SESSION["loggedin"] != 'yes')
{
header("Location: /admin/login.php");
}
?>
因此,如果没有值为“yes”的会话变量“loggedin”,它只会重定向回 login.php 页面。
如上所述,由于某种原因,它没有存储变量。
这与我在另一个运行良好的网站上的代码完全相同,我唯一更改的是数据库登录详细信息。
我已在数据库中检查了所有正在选择的列,它们都是正确的列(拼写正确)
有什么想法吗?