如何从 PHP 调用 HTML 文件

新手上路,请多包涵

我正在尝试使用 HTML 和 PHP 创建管理员登录页面(PHP 在这里也用于其他几个目的),一旦管理员登录,HTML 文件就需要运行。

我将在下面包含我的登录页面代码。我需要在 PHP 文件的 if 语句中插入命令。我尝试了使用 include 函数的不同方式;也许我没有正确使用它。

代码:

文件

?php

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

    if ($username =='admin' and $password =='Parthurnax')
    {
        include 'test.html';
    }
    else
    {
        echo 'you are not the admin';
    }
?>

HTML 文件:

 <html>
    <body>
        <div align="center">
            <form action="page.php" method="POST">
                <b>Username:</b><input type="text" name="username"><br>
                <b>Password:</b><input type="password" name="password"><br>
                <input type="submit">
            </form>
        </div>
    </body>
</html>

原文由 Niron 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 644
2 个回答

改变

if ($username =='admin' and $password =='Parthurnax')
{
    <?php include 'test.html';?>
}
else
{
    echo 'you are not the admin';
}

if ($username =='admin' and $password =='Parthurnax')
{
    include 'test.html';
}
else
{
    echo 'you are not the admin';
}

您在一个已经打开的 PHP 脚本中有 openend PHP 标签。

不要忘记 test.html 页面在不登录的情况下仍然可以访问。如果我直接在浏览器中输入 test.html,我会得到你的受保护页面。

将其更改为 PHP 脚本并检查是否有登录用户。如果用户未登录,要么 301 他们到登录页面,要么死掉你的脚本。

原文由 Michael Koelewijn 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您想重定向到新页面,请在下面使用

if(whatever_condition_set_true) {
 header('Location: whatever_page.html');
 exit;
}

或者如果您想根据条件包含任何页面,请使用

if(whatever_condition_set_true) {
   include_once('whatever_page.html');
}

原文由 diEcho 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏