使用Session将数据从php传递到另一个页面


Using Session to Pass Data from php to another page

我知道这个问题已经被问了很多次了;然而,我所看到和尝试的各种事情都没有奏效。

我所想做的就是将数据从一个页面传递到下一个页面,这样我就可以使用用户的电子邮件来做各种数据库的事情。

以下是我的login.php文件(节略,仅显示登录内容):

<?php
session_start();
ini_set("display_errors", "1");
$user_email = $_POST['user_email'];
$_SESSION['user_email'] = $user_email;
$password = $_POST['user_password'];
//DB stuff
if ($num_results == 1) {
header('Location: nextPage.html');
exit;
}
else {
echo "<p> Username or Password is incorrect! </p>";
}
?>

这是我的下一页html文件:

<?php
session_start();
ini_set("display_errors", "1");
$email = $_SESSION['user_email'];
echo $email;
?>

<html>
<head>
<title> next page </title>
</head>
<body>
You have logged in!
</body>
</html>

每当我试图回显电子邮件时,它都是空白的,当我测试它时,它会"告诉"我它是空的。

任何建议都会很有帮助,非常感谢!!

出现问题的原因是header('Location: nextPage.html');指向了.html文件。

将会话从一个页面传递到另一个页面只能在PHP环境下工作,并且应该设置为header('Location: nextPage.php');

但是,通过在.htaccess 中使用以下指令,.html文件可以作为PHP运行

AddType application/x-httpd-php .html

如果你只打算在一个页面上包含PHP,最好这样设置:

<Files nextPage.html>
AddType application/x-httpd-php .html
</Files>