【发布时间】:2013-12-05 05:39:30
【问题描述】:
我使用来自http://www.php.net/manual/en/function.session-start.php 的本教程来了解 php 中的会话。 page1 和 page2 的代码工作正常。我希望代码能够在 html 中工作,所以我编辑了 php 并将它们保存为两个新文件。
page1.html
<html>
Welcome to page #1
<?php
session_start();
echo 'setting variables';
$_SESSION['username'] = 'User1234';
$_SESSION['password'] = 'SecretPassword';
?>
<br /><a href="page2.html">page 2</a>
</html>
page2.html
<html>
<?php
// page2.php
session_start();
?>
Welcome to page #2
<br />
Username: <?php echo $_SESSION['username']; >
<br/>
Password:
<?php echo $_SESSION['password'] ?> <br/>
<br /><a href="page1.html">page 1</a>
</html>
导致 page2.html 返回 this 的错误发生在哪里?
Welcome to page #2
Username:
Password:
page 1
*编辑** 通过添加一个允许我使用 EventHandler 的 .htaccess 文件来修复它
【问题讨论】:
-
可能您的 HTML 文件没有被作为 PHP 代码处理。您的服务器是否配置为将
.html文件作为 PHP 处理?如果没有,您可以尝试将文件重命名为.php。 -
在第 2 页中,您将在
Welcome to page #2行之前创建一个新会话。 -
session_start()也应该放在任何直接 (HTML) 输出之前 -
正如Phil 上面所说,不这样做会导致
headers already sent...,因为您在<?php之上还有<html>