Hi Monkey,
I think I can help you. Just last week I had the same question, and with the help of a couple of people here, I now have a Login page which verifies name and password exist in my database, and redirects to susequent pages - even according to a status attribute which I've attached to each user... ie: Admins go to admin pages, Users go to user pages and Banned/Inactive users go to an inactive account page.
It also checks cookies, for refresh and reload without losing login... and uses sessions to keep users logged in over multiple pages. Both of which are destroyed by the 'logout' page, which I can show you also, if needed.
It works a treat, but uses an SQL Database for the user data.
You may not have a database, but I'm sure the redirect code will still interest you.
It's the Switch/Case function right at the end.
Thread where I got good help:
http://www.phpbuilder.com/board/showthread.php?t=10353571
Cheers.
I have used a single "Failed.php" page for all failed logins, which just says "Failed Login - Retry". Whether the case be: name not in DB and/or password mismatch. Simply because I don't want people to be able to start guessing what names are in the DB by trying a few and noting which ones only give a "failed password" message. But for debugging, initially it helps to have seperate header redirects, to know where your code is taking you.
It's quite long now that I have all my working features in it, but even so... here's my working code. Probably bloated and "over-done", but it works. And for only my 2nd week of writing PHP, I think it's pretty damn good 😃 :
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// Connects to your Database
mysql_connect("localhost", "LOGIN", "PASSWORD") or die(mysql_error());
mysql_select_db("MY_DB") or die(mysql_error());
session_start();
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: main.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit']))
{ //if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass'])
{
header("Location: failed.php");
exit;
}
// checks it against the database
if (!get_magic_quotes_gpc())
{
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0)
{
header("Location: failed.php");
exit;
}
else
{
while($info = mysql_fetch_array( $check ))
{
$_STATUS = $info['status'];
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password'])
{
header("Location: failed.php");
exit;
}
switch($_STATUS){
case "admin":
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
$_SESSION['status'] = admin;
header("Location: admin.php");
break;
case "active":
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
$_SESSION['status'] = active;
header("Location: main.php");
break;
case "inactive":
header("Location: inactive.php");
$_SESSION['status'] = inactive;
break;
default:
header("Location: failed.php");
}
}
}
}
?>