<?php
/*****
login.php:
This file contains a simple front end login script.

author: jez hancock
email: munk@munk.me.uk
date: 15/10/2002
*****/
include_once("config.php");

// Check user not logged in already:
checkLoggedIn("no");

// Page title:
$title="Member Login Page";

// if $submit variable set, login info submitted:
if($_POST["submit"]) {
    
//
    // Check fields were filled in
    //
    // login must be between 4 and 15 chars containing alphanumeric chars only:
    
field_validator("login name"$_POST["login"], "alphanumeric"415);
    
// password must be between 4 and 15 chars - any characters can be used:
    
field_validator("password"$_POST["password"], "string"415);

    
// if there are $messages, errors were found in validating form data
    // show the index page (where the messages will be displayed):
    
if($messages){ 
        
doIndex();
        
// note we have to explicity 'exit' from the script, otherwise
        // the lines below will be processed:
        
exit;
    }

    
// OK if we got this far the form field data was of the right format;
    // now check the user/pass pair match those stored in the db:
    
if( !($row checkPass($_POST["login"], $_POST["password"])) ) {
        
// login/passwd string not correct, display error:
        
$messages[]="Incorrect login/password, try again";
    } 

    
// if there are $messages, errors were found in validating form data
    // break from this if loop to display login page with the errors:
    
if($messages){
        
doIndex();
        exit;
    }

    
// if we got here, no errors - start a session using the info
    // returned from the db:
    
cleanMemberSession($row[login], $row[password]);

    
// and forward user to members page (populate session id in URL):
    
header("Location: members.php?".session_name()."=".session_id());
} else {
    
doIndex();
}

// This function displays the default 'index' page for this script:
function doIndex() {
    
// import the global $messages array.
    // if any errors were detected above, they will be stored
    // in the $messages array:
    
global $messages;

    
// also import the $title for the page - note normally just declare
    // all globals on one line - ie global $messages, $title
    
global $title;

    
// drop out of PHP mode to display the plain HTML:
?>
<html>
<head>
<title><?=$title?></title>
</head>
<?php doCSS()?>
<body>
<h1><?=$title?></h1>
<?php
// if there are any messages stored in the $messages array, call the displayErrors
// function to output them to the browser:
if($messages) { displayErrors($messages); }
?>
<form action="<?=$_SERVER["PHP_SELF"]?>" method="POST">
<table>
<tr><td>Login:</td><td><input type="text" name="login" value="<?php print $_POST["login"?>" maxlength="15"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" value="" maxlength="15"></td></tr>
<tr><td>&nbsp;</td><td><input name="submit" type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
<?php
}
?>