PDA

View Full Version : Password driven link



brianvoe
08-30-2007, 01:36 PM
I am creating a web page in which when someone has to enter a password to get in. But I want what they type in to decide what page they go to. Example: Let's say I have three passwords that I set up. code123, code876 and code391. when someone types in one of those it will direct them to a specific web page that I have made. Can someone help me figure out how to do this?

laxman
08-31-2007, 08:15 AM
You need to use a server side technology such as PHP or ASP and then you need to use a database technology such as MySQL. PHP and MySQL are free.

Once you have that set up you just use some php code like

you would have a list of passwords in the database and then when a user types in a password, you compare it against all the passwords in the database. then:

if (match is found)
{
go to secure section
}
else
{
go to some other page
}

You will have to use session variables to make sure that users can go to multiple pages and only type in the password once.

So to get this done:

1. Learn the basics of PHP and MySQL (lots of tutorials, look for them on google, also its very easy if you already know programming)

2.Learn about session variables

3.Put it together

If you have dreamweaver, there is a password option there to do it for you. I've never used it, but i've used most of the other PHP stuff and it works quite well.

brianvoe
08-31-2007, 02:45 PM
Is there any way of doing right in the page. I am only using 3 passwords. Do I have to use a database to do this?

laxman
09-01-2007, 03:42 AM
Nope, if you only want three password you do

$pass1 = stuff;
$pass2 = stuff2;
$pass3 = stuff3;

//assume you had a textfield called userInput where the user puts the password
$userInput = $GET_['userInput'];

if ($pass1 = $userInput || $pass2 = $userInput || $pass3 = $userInput)
{
redirect to the correct page
}


You will still need to use session variables so that you can go to different pages without retyping the password. There are some password templates you can download, where all you do is include them at the top of the page you want to protect. You could take one of those and modify it with the if statement above to allow three passwords.

webdezzi
12-08-2009, 01:27 AM
just a little modification to laxman's code

<?php

//initializes session for this page, include this on the first line of every pages
//that the user will have to view based on his password
session_start();

//sets the passwords
$pass1 = stuff;
$pass2 = stuff2;
$pass3 = stuff3;

//assume you had a textfield called userInput where the user puts the password
$userInput = $_GET['userInput'];

//if password supplied matches the pass1 saved on the server
if ($pass1 == $userInput)
{
$_SESSION['pass']="user1";
header("Location: page1.php"); //takes users to page1
exit();
}


//if password supplied matches the pass2 saved on the server
elseif($pass2 == $userInput)
{
$_SESSION['pass']="user2";
header("Location: page2.php"); //takes users to page2
exit();
}

//if password supplied matches the pass3 saved on the server
elseif($pass3 == $userInput)
{
$_SESSION['pass']="user3";
header("Location: page3.php"); //takes users to page3
exit();
}


else{
header("Location: error.php"); //that means the user entered a wrong password
unset($_SESSION['pass']); //it is good practice to delete this once user gets password wrong
}

?>


just make sure the file name ends witth .php so that session can work
so on every page you wanna use, just put session_start() like above and do a check like

if($_SESSION['pass']=="user1"){header("Location: yourPage1.php"); exit();}

goodluck.