the easiest way i know is to create a login.php that submits the form to itself to validate the user:
PHP Code:
function LoginForm ($Name, $Email,$L_Error) {
?>
<form name="login" method="post" id="login" action="<?=$_SERVER['PHP_SELF'];?>">
<p><strong style="color:red;"><?=$L_Error;?></strong>
<P>
Email
<input name="email" type="text" id="email" value="<?=$Email;?>" size="25">
Password
<input name="password" type="password" id="password" size="25">
<input type="submit" name="submit" value="Login" style="margin-left:50px;">
</form>
<?
}
if ($_POST['submit']=='Login') {
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
if ($password =='' OR $password ==' '){
echo LoginForm($password,$email,'Password is blank');
exit();
}
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo LoginForm($name,$email,'Invalid email format');
exit();
}
else {
$password = sha1($_POST['password']);
$query="SELECT cust_id, pshash, email, first, last FROM users WHERE email='$email' and pshash='$password' ";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
$_SESSION['cust_id'] = $row[cust_id];
$_SESSION['email'] = $email;
$_SESSION['user'] = "$row[first] $row[last]"; //get any other data from db and store in session
header("location:getin.php");
}
}
else{
echo LoginForm('',$email,'Password does not match email');
}
}
}
else{
echo LoginForm('',$email,'');
}
this is the login form i currently use. Basically the page checks of the form has been submitted, if it has, it validates the user input: correct syntax non hostile code etc, if validation fails, it displays the form updated with an error message (passed through the 3rd function argument). if validation passes it queries the database. Note i have the sha1 hash so you'll have to have the hashes stored in the db. if the db returns a row then some useful (but non sensitive info about the user is stored in the session array and finally the user is taken to your "getin.php" page via the header: And of course if the db returns zero rows, then the pages refreshes to display the form complete with error message.
all the user pages can then start with session_start(); then check for a session value for example:
PHP Code:
$session_start();
if(!isset(name)){
header("location:login.php");
die();
}
this is a simple check that will only allow users who have logged in (and thus defined the session vars, and users who haven't logged in will be directed to login page.
Let me know if this makes sense, an the code should parse, but i did some uick hacking (changed some vars and names for security reasons) so i apologize for any missed semi-colons , quotes, etc ....
OH AND P.S.
Your code could benefit greatly from jumping in and out of php.
I'm sure you know your code is very hard to read. Just jump out of php when you have a lot of html to write!! and if you insist on using the echo , then you'll find it easier to use the single quotes ' ' rather then the double , that way you dont have to escape so many doubt quotes!
Bookmarks