我是一名 PHP 学习者,我在 PHP 会话方面遇到了一些问题。
我正在尝试做一个简单的登录测试,但是当我指向另一个页面时,似乎会话变量不会跟随。两个重要文件:
检查登录:
<html>
<head>
<title>
<?php
session_start();
?>
</title>
</head>
<body>
<?php
$connection = mysql_connect("localhost", "root", "root");
if(!connection)
{
die("could not connect to the database");
}
mysql_selectdb("phplogin", $connection) or die ("MySQL error i valg af database: " .mysql_error());
$query = "Select * from users where username='".$_POST['username']."' AND password = '".$_POST['userpass']."'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count==1){
$_SESSION['loggedIn'] = "true";
echo "The variable: ".$_SESSION['loggedIn'];
header("Location: loggedinPage.php");
exit;
}
if(!$count == 1){
header("Location:Login.php");
exit;
}
?>
</body>
和访问页面:
<html>
<head>
<title>
<?php
session_start();
?>
</title>
</head>
<body>
<?php
if($_SESSION['loggedIn'] != "true"){
echo "You are NOT logged in";
echo $_SESSION['loggedIn'];
exit;
}
echo $_SESSION['loggedIn'];
echo "You are logged in";
?>
</body>
即使您提供了正确的用户名和密码,当被引导到新页面时,它仍然表示您没有登录。另一件事是“标题(位置:等)”,这不起作用,我必须手动重定向。
任何帮助:)?- 大卫
谢谢,我现在可以登录了,但是重定向仍然不起作用?我的文件现在看起来像这样:
<?php
session_start();
?>
<?php
$connection = mysql_connect("localhost", "root", "root");
if(!connection)
{
die();
}
mysql_selectdb("phplogin", $connection) or die ("MySQL error i valg af database: " .mysql_error());
$query = "Select * from users where username='".$_POST['username']."' AND password = '".$_POST['userpass']."'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count==1){
$_SESSION['loggedIn'] = "true";
header("Location: loggedinPage.php");
}
if(!$count == 1){
header("Location:Login.php");
}
?>
<head>
<title>
</title>
</head>
<body>
</body>