Niron
Niron

Reputation: 31

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.

I will include my login page code below. I need to insert the command in the if statement in the PHP file. I tried different ways of using the include function; maybe I am not using it right.

Code:

PHP file

?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>
    <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>

Upvotes: 1

Views: 39221

Answers (3)

xkeshav
xkeshav

Reputation: 54072

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');
}

Upvotes: 2

Nicolas Pelican
Nicolas Pelican

Reputation: 1

Use header("yourlink.html"); and don't forget to exit()

Upvotes: -1

Michael Koelewijn
Michael Koelewijn

Reputation: 476

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.

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.

Change it to a PHP script and check for a logged in user. If the user is not logged in either 301 them to the login page or die your script.

Upvotes: 5

Related Questions