我有一个小脚本来处理登录我的CMS。查询工作正常,但我认为我对SESSIONS有问题,因为即使用户名和密码正确,它也会重定向到登录页面。我有以下代码。请帮我解决这个问题。提前谢谢。
// checklogin.php ....
<?php
include("db/dbconnect.php");
mysqli_select_db($connect, $db);
// username and password sent from signup form
$username = mysqli_real_escape_string($connect, $_POST['username']);
$password = mysqli_real_escape_string($connect, $_POST['password']);
$sql = "SELECT username, password FROM login WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($connect, $sql) or die('Error : ' . mysqli_error($connect));
// Mysql_num_row is counting table row
$count = mysqli_num_rows($result);
// If result matched $username and $password, table row must be 1 row
if($count == 1)
{
//Register $username, $password and redirect to mainpage"
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:about.php");
}
else {
echo "Invalid Username or Password. Please try again.";
echo "<meta http-equiv=Refresh content=5;url=index.php>";
}
?>
// about.php
<?php
//check if user is logged on;
session_start();
if(!isset($_SESSION['username']) )
{
header("location:index.php");
}
?>
答案 0 :(得分:1)
您需要在为其分配用户名和密码之前启动会话。在你的&#34;如果&#34; condition($ count == 1),您直接指定用户名和密码。在这些作业之前加上这一行:
在session_start();
所以,现在你的&#34;如果&#34;条件变成了这个:
if($count == 1)
{
session_start();
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:about.php");
}
我猜,问题是什么。如果有效,请告诉我。
答案 1 :(得分:0)
添加session_start();在checklogin.php中 这将有助于你
<?php
session_start(); //missing in checklogin.php
include("db/dbconnect.php");
mysqli_select_db($connect, $db);