I see this question come through here quite frequently and I just happened to finish another login script so I figured I would post it and you can take what you want from it. If you have any questions, feel free to ask.
<?php
session_start();
//include pdo mysql db connection, try to keep login info out of docroot
include("../common/db/connection.php");
$arrErrors = array(); //array to collect errors
//if form was submitted
if(isset($_POST['action']) && trim($_POST['action']) == "login")
{
//if txtUser is blank
if(!isset($_POST['txtUser']) || trim($_POST['txtUser']) == "")
{
$arrErrors[] = "user name or email address is blank";
}
//if pass was blank
if(!isset($_POST['txtPass']) || trim($_POST['txtPass']) == "")
{
$arrErrors[] = "password is blank";
}
//if there were no errors thus far
if(count($arrErrors) == 0)
{
//form validator class to determine if txtUser is email or username
include("lib/FormValidator.php");
$validate = new FormValidator();
//standard email validation
$strColName = $validate->isEmail($_POST['txtUser'])?"email":"username";
//start pdo prepare and query
$objStatement = $objDB->prepare("select userpk, pass from user where " . $strColName . " = ?");
$objStatement->execute(array($_POST['txtUser']));
$arrResult = $objStatement->fetchAll(PDO::FETCH_ASSOC);
//if response from database is not a valid response
if(!is_array($arrResult) || count($arrResult) == 0)
{
$arrErrors[] = "0 records found with that information";
}
//else if password is not valid
else if(md5($_POST['txtPass']) != $arrResult[0]["pass"])
{
$arrErrors[] = "0 records found with that information";
}
//else another check on the errors array, probably not needed
else if(count($arrErrors) == 0)
{
//if done right, userpk should be all you need to maintain state for standard security
$_SESSION['user'] = $arrResult[0]["userpk"];
}
//check to make sure that the userpk is in valid format, no reason it shouldn't be but you know
if(isset($_SESSION['user']) && is_numeric($_SESSION['user']) && $_SESSION['user'] > 0)
{
header("location: index.php");
}
//if for some strange reason the userpk is not valid, clear all session and send error to user
else if(count($arrErrors) == 0)
{
$_SESSION['user'] = NULL;
$_SESSION = array();
$arrErrors[] = "there was an unknown error regarding this login";
}
}
}
//buffer class manages html head, template header, footer and buffers output for me
include("lib/Buffer.php");
$buffer = new Buffer();
?>
<form action="/login.php" method="post">
<input type="hidden" name="action" value="login" />
<table cellpadding="0" cellspacing="0">
<?php
//output errors to user
if(count($arrErrors) > 0)
{
?>
<tr><td style="color:red;">
<span style="font-size:14px; font-weight:bold;">Please correct the following errors</span><br />
<?php
foreach($arrErrors as $error)
{
echo $error . "<br />";
}
?>
</td></tr>
<?php
}
?>
<tr>
<td>
Username or Email Address<br />
<input type="text" name="txtUser" id="txtUser"<?php echo isset($_POST['txtUser']) && trim($_POST['txtUser']) != ""?' value="' . trim($_POST['txtUser']) . '"':''; ?> />
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
Password<br />
<input type="password" name="txtPass" id="txtPass" />
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Login" />
</td>
</tr>
</table>
</form>
<?php
//notify buffer class to save the contents of this page
$buffer->closePage();
//all is well, we are ready to ouput to the browser
$buffer->outPut();
?>
121,1 Bot