Creating a password protected area
This is one of the most easiest ways to create a password protected area in PHP. This simple script can protect any of your content should you need it protected.

This is the most stylish way to perform a password protected page.

Read more for the tutorial.

First, we need the PHP script for the page. Create a new document and call it pass.php, then copy and paste the code below.
<?php
session_start
();

if(!isset(
$_SESSION['pUser']))
{
    echo 
'';
}
else
{
    if(
$_POST['Submit'])
    {
        if(
$_POST['textfield'] != "yourPASSWORDhere")
        {
            echo 
'Wrong password!';
        }
    }
    else
    {
        
    }
}
?>
Now we need to start off by creating the HTML form.
<form action="" method="post" name="loginForm" id="loginForm">
<div align="center"><strong>Password:</strong>
<input type="text" name="textfield"><br><br><input type="submit" name="Submit" value="Login"></div>
</form>
All of the HTML above should go inside the echo statement, like so:
<?php
if(!isset($_SESSION['pUser']))
{
    echo 
'<form action="" method="post" name="loginForm" id="loginForm">
<div align="center"><strong>Password:</strong>
<input type="text" name="textfield"><br><br><input type="submit" name="Submit" value="Login"></div>
</form>'
;
}
?>
Then after the last else statement you can do a few things. You can either put your content in the page directly, however you first need to split the tags. Like so.
<?php
else
{
    
?>
Your HTML content here!
    <?php
}
?>
You can also directly include your protected content using the include statement.
<?php
else
{
    include(
"path/to/my/secretfile.txt");
}
?>
It's wise to name your secret file something no one could type. Your secret file does not have to be in .txt format either. It can be in .php, .txt, .html, etc.

Also, don't forget to set your password!
<?php
if($_POST['textfield'] != "yourPASSWORDhere")
?>
The end result should look like this:
<?php
session_start
();

if(!isset(
$_SESSION['pUser']))
{
    echo 
'<form action="" method="post" name="loginForm" id="loginForm">
<div align="center"><strong>Password:</strong>
<input type="text" name="textfield"><br><br><input type="submit" name="Submit" value="Login"></div>
</form>'
;
}
else
{
    if(
$_POST['Submit'])
    {
        if(
$_POST['textfield'] != "yourPASSWORDhere")
        {
            echo 
'Wrong password!';
        }
    }
    else
    {
        include(
"path/to/my/secretfile.txt");
    }
}
?>

SOCIAL BOOKMARK - Posted by Steven Sullivan on 7th August, 2006 - 08:30:08 GMT

Comments

1

Posted by Jamie Hudson on 7th January, 2007 at 00:54:20 GMT


Very nice, you could also add some nifty snipets to that with Setcookies so that on each page you could have another script checking the cookies to see if somone is logged on. That way multiple pages could be protected with just a little script.


* Name:
* E-Mail:
Not shown to public
Website:
Emotions:
* Comment:

Up

Down

characters left
* Security Code:
Type this code in the box: 794422
* Required field
© Steven Sullivan 2006 - All Rights Reserved         Video Disclaimer - Valid XHTML & CSS