How to password protect a page using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to password protect a page using php

Post by Saman » Fri Jul 15, 2011 6:25 pm

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

Code: Select all

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

} 

?>
Shalom
Corporal
Corporal
Posts: 3
Joined: Mon Sep 26, 2011 9:09 am

Re: How to password protect a page using php

Post by Shalom » Sun Dec 25, 2011 11:42 am

SUperb :) :) :)
Post Reply

Return to “PHP & MySQL”