PHP - Password Protect a page *FIXED*

Do you want to share WYSIWYG Web Builder tips, tricks, tutorials or useful HTML code? You can post it here...
(no questions or problems please, this section is not monitored by support).
Forum rules
This section is to share tips, tricks and tutorials related to WYSIWYG Web Builder.
Please do not post questions or problems here. They will not be answered.

PLEASE READ THE FORUM RULES BEFORE YOU POST:
viewtopic.php?f=12&t=1901
Post Reply
madjamonline
 
 
Posts: 51
Joined: Tue Jun 19, 2007 4:27 pm
Location: United Kingdom
Contact:

PHP - Password Protect a page *FIXED*

Post by madjamonline »

Hi there,
I know everyone likes using the password protect extension but that is not very secure. This PHP code can be used to secure your page.

There are two ways you can do it. Make sure that on both of them, you set each of the page extensions to PHP!

If you want to protect more than 1 page and you don't want the user to keep entering the password, use this way.

Insert this code in the pages you want protected.
Enter it in the Start of Page area.

Code: Select all

<?php
$password = "enter your password here...";
$bad_page = "enter the page that the user will be redirected to if it is a bad login.";
if($_GET["cmd"] == "verify")
{
if(!($_GET["password"] == $password))
{
header('Location: '.$bad_page);
}
else
{
session_start();
$_SESSION["loggedin"]="true";
}
}
?>
Now enter this in the start of body section of your page.

Code: Select all

<?php
// EDIT THIS BIT AT YOUR OWN RISK
if($_SESSION["loggedin"] == "true")
{
?>
Now enter this in the end of body section of your page.

Code: Select all

<?php
// EDIT THIS BIT AT YOUR OWN RISK
}
if(!($_SESSION["loggedin"] == "true"))
{
// Edits allowed
?>
<font face="arial" size="2">A password is required to access this page.<br>
Please enter the password below.<br>
<form action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name=cmd value=verify>
<input type=password name=password>
<input type=submit value=Enter></form>
<?php
}
?>
If you only want to protect one page,
use this code.

Insert this code in the start of your page:

Code: Select all

<?php
$password = "your password...";
$bad_page = "incorrect password page...';
if($_GET["cmd"] == "verify")
{
if(!($_GET["password"] == $password)
{
header('Location: '.$bad_page)
}
}
?>
Now enter this bit in the start of body section:

Code: Select all

<?php
if($_GET["password"] == $password)
{
?>
Now enter this in the end of body section of your page.

Code: Select all

<?php
// EDIT THIS BIT AT YOUR OWN RISK
}
if(!($_SESSION["loggedin"] == "true"))
{
// Edits allowed
?>
<font face="arial" size="2">A password is required to access this page.<br>
Please enter the password below.<br>
<form action="<?php echo $_SERVER['PHP_SELF']?>">
<input type=hidden name=cmd value=verify>
<input type=password name=password>
<input type=submit value=Enter></form>
<?php
}
?>
If there are any problems, please say so! :D
madjamonline
 
 
Posts: 51
Joined: Tue Jun 19, 2007 4:27 pm
Location: United Kingdom
Contact:

Post by madjamonline »

I am sorry.
It works ok for me.
Did you check all of the codes?

Make sure you publish the page. The code will not work in preview mode.

If you do encounter problems, don't be afraid to say about them?! :D
bjlolmaugh
 
 
Posts: 181
Joined: Thu Nov 15, 2007 2:36 pm
Contact:

Post by bjlolmaugh »

Question: Do I use both sets of codes (1 page password and multipage password)? Or do I have to choose which set of codes to use?
Sincerely,

Barbara Lolmaugh
http://www.websitesbybarbara.com
madjamonline
 
 
Posts: 51
Joined: Tue Jun 19, 2007 4:27 pm
Location: United Kingdom
Contact:

Post by madjamonline »

bjlaird wrote:Question: Do I use both sets of codes (1 page password and multipage password)? Or do I have to choose which set of codes to use?
I don't quite understand...
madjamonline
 
 
Posts: 51
Joined: Tue Jun 19, 2007 4:27 pm
Location: United Kingdom
Contact:

Post by madjamonline »

brassnugget wrote:Am attempting to use above password protection for more than one page. Generally works well but get asked for password on every page - doesn't seem to recognise that I am alreday logged in.

Any thoughts on what might be causing this?

Thanks
This code was posted soo long ago I have forgoten but when reading it the session should be in tact. REMEMBER: (I didn't mention this!) The pages need to be in the same directory for it to remember the session!

I am working on an advanced MYSQL members system as an extension now so that will be a lot better!
Post Reply