Dreamhost

Looking for somewhere to host your site? Browse our PHP web hosts section!

Home > PHP Scripts > Password protect a page

Password protect a page

Description

Password protection for a single page. Visitors are required to enter a password and username into a login form to view the page content.

The code


Deprecated: Function eregi_replace() is deprecated in /home/totallyphpuk/totallyphp.co.uk/functions/function.syntaxHighlight.php on line 29

Deprecated: Function eregi_replace() is deprecated in /home/totallyphpuk/totallyphp.co.uk/functions/function.syntaxHighlight.php on line 30
<?php

// Define your username and password
$username "someuser";
$password "somepassword";

if (
$_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

?>

<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

</form>

<?php

}
else {

?>

<p>This is the protected page. Your private content goes here.</p>

<?php

}

?> 

Get the code

Download the file to your computer: Click here to get the file