【问题标题】:Why php to html conversion isn't working? [closed]为什么 php 到 html 的转换不起作用? [关闭]
【发布时间】: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...,因为您在&lt;?php 之上还有&lt;html&gt;
  • PHP sessions with HTML的可能重复

标签: php html session


【解决方案1】:

您应该将文件的扩展名更改为.php,以便 PHP 将其识别并解释为 PHP 文件。

【讨论】:

    【解决方案2】:

    任何包含 php 的文件都需要具有 .php 扩展名而不是 .html。此外,如果您正在运行自己的 Web 服务器(如 Apache 或类似服务器),请确保您的计算机上安装了 PHP,以便服务器可以解释 PHP。

    TLDR:

    将文件名更改为 .php 并将 href 链接更改为 .php 。

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2013-10-23
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多