无法在php中跨页面使用登录会话

无法在php中跨页面使用登录会话,php,Php,我无法将登录会话信息传递到其他HTML页面。下面是我的登录php代码。我可以成功登录,但无法将信息传递到其他页面,如HTML主页,虽然我没有登录到相同的页面,但它会被打开。我尝试了不同的代码 <?php require_once("config.php"); $email=$_POST['email']; $password=$_POST['password']; $email = stripslashes($email); $password = stripslashes($pas

我无法将登录会话信息传递到其他HTML页面。下面是我的登录php代码。我可以成功登录,但无法将信息传递到其他页面,如HTML主页,虽然我没有登录到相同的页面,但它会被打开。我尝试了不同的代码

<?php

require_once("config.php");

$email=$_POST['email'];
$password=$_POST['password'];

$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string(strip_tags($email));
$password = mysql_real_escape_string(strip_tags($password));

// Check occurence of email password combination
$sql="SELECT * FROM register WHERE email='$email'";
$result=mysql_query($sql); 


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $email, table row must be 1 row
if($count==1)
{
$row = mysql_fetch_array($result);
if($password == $row['password'])
{
session_start();
$_SESSION['login'] = "1";
header("location:home.html");
exit;
}
else 
{
echo "Please enter correct Password";
header("location:login.html");
session_start();
$_SESSION['login'] = '' 
exit();
}
 }  
else
{
header("Location:register.html");
exit();
}

?>


Below is the php snippet that I use at the top of my HTML page:
<?php

require_once("config.php");
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

header ("Location: login.html");

exit;

}
?>

将.html文件更改为.php文件。然后从
session_start()开始
在此之后,您将能够使用
$\u SESSION['login']

SESSION\u start();应仅在登录成功时使用onse

从“我在HTML页面顶部使用的php代码片段”中删除这一行,一切都应该正常

所以你的代码应该是:

<?php

require_once("config.php");
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

header ("Location: login.html");

exit;

}
?>

完美。感谢尼尔斯如此迅速的回应。它工作得很好