简体   繁体   English

如何从 PHP 调用 HTML 文件

[英]How to call an HTML file from PHP

I am trying to create an administrator login page using HTML and PHP (the PHP is here for several other purposes too) where once the administrator logs in, an HTML file needs to run.我正在尝试使用 HTML 和 PHP(PHP 也用于其他几个目的)创建管理员登录页面,一旦管理员登录,就需要运行一个 HTML 文件。

I will include my login page code below.我将在下面包含我的登录页面代码。 I need to insert the command in the if statement in the PHP file.我需要在 PHP 文件的if语句中插入命令。 I tried different ways of using the include function;我尝试了使用 include 函数的不同方式; maybe I am not using it right.也许我没有正确使用它。

Code:代码:

PHP file PHP文件

?php

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

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

HTML file: 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>

change改变

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

to

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

You have openend PHP tags in an already open PHP script.您在已经打开的 PHP 脚本中有 openend PHP 标签。

Don't forget the test.html page is still accesible without logging in. If i were to directly put in test.html in my browser, i'd get your protected page.不要忘记 test.html 页面在没有登录的情况下仍然可以访问。如果我直接在浏览器中放入 test.html,我会得到你的受保护页面。

Change it to a PHP script and check for a logged in user.将其更改为 PHP 脚本并检查登录用户。 If the user is not logged in either 301 them to the login page or die your script.如果用户没有登录,要么 301 他们到登录页面,要么死你的脚本。

use below if you want to redirect to the new page如果您想重定向到新页面,请在下面使用

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

or if your want to include any page based on condition then use或者如果您想根据条件包含任何页面,请使用

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

Use header("yourlink.html");使用header("yourlink.html"); and don't forget to exit()并且不要忘记exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM