PDA

View Full Version : [PHP] Password Protection



Turbocom
05-09-2008, 02:48 AM
Alright so last night I was up all night just trying to figure this out, and its not even a good one so I need help. Basically I read a ton of Tutorials and I didn't read any good ones. So basically I need a password protected page.

When logged in with the password its redirected to another page, anyways heres what I have


<?php
// If password is valid let the user get access
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
?>
<p align="center"><br><br><br>
<b>Congratulations</b><br>you have gained access to the Protected and Secret Area!</p>

<?php
}
else
{
// Wrong password or no password entered display this message
if (isset($_POST['password']) || $password == "") {
print "<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b><br>Please enter the correct password</font></p>";}
print "<form method=\"post\"><p align=\"center\">Please enter your password for access<br>";
print "<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>";
}


+ Rep to all who help

Moh
05-09-2008, 03:17 AM
index.php


<?php
session_start();

$password = "test";

if($_SESSION["logged_password"] == $password) {
header('Location: page.php');
}
if(isset($_POST["password"])) {

if($_POST["password"] == "") {
echo("<p align=\"center\"><font color=\"red\"><b>You didn't enter a password!</b><br>Please enter the correct password</font></p>");
} elseif($_POST["password"] != $password) {
echo("<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b><br>Please enter the correct password</font></p>");
} else {
$_SESSION["logged_password"] = $_POST["password"];
header('Location: page.php');
}

} else {
echo("<form method=\"post\"><p align=\"center\">Please enter your password for access<br>
<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>");
}
?>


page.php


<?php
session_start();

$password = "test";

if($_SESSION["logged_password"] != $password) {
session_destroy();
header('Location: index.php');
} else {
?>
This is the page :)
<?
}
?>


If you want to use multiple accounts and passwords, ask me tomorrow when im not tired :)

Turbocom
05-09-2008, 03:42 AM
index.php


<?php
session_start();

$password = "test";

if($_SESSION["logged_password"] == $password) {
header('Location: page.php');
}
if(isset($_POST["password"])) {

if($_POST["password"] == "") {
echo("<p align=\"center\"><font color=\"red\"><b>You didn't enter a password!</b><br>Please enter the correct password</font></p>");
} elseif($_POST["password"] != $password) {
echo("<p align=\"center\"><font color=\"red\"><b>Incorrect Password</b><br>Please enter the correct password</font></p>");
} else {
$_SESSION["logged_password"] = $_POST["password"];
header('Location: page.php');
}

} else {
echo("<form method=\"post\"><p align=\"center\">Please enter your password for access<br>
<input name=\"password\" type=\"password\" size=\"25\" maxlength=\"10\"><input value=\"Login\" type=\"submit\"></p></form>");
}
?>


page.php


<?php
session_start();

$password = "test";

if($_SESSION["logged_password"] != $password) {
session_destroy();
header('Location: index.php');
} else {
?>
This is the page :)
<?
}
?>


If you want to use multiple accounts and passwords, ask me tomorrow when im not tired :)
Thanks Jack :) I was tired as well I kept getting one error, until I figured it out I had

page.php and it was suppose to be misc/page.php :P Thank you!!

Turbocom
06-09-2008, 04:51 AM
Will it forget the password when I clear my cache?

L?KE
08-09-2008, 04:19 PM
Yeah cos it's a session, I believe?

And i thought you wanted a pw protected page, not two? :S

Want to hide these adverts? Register an account for free!