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