I'm upgrading/rewriting our online client portal and wondering if my log in page code looks okay so far. during this rewriting/learning process I am upgrading a number of things: PHP 5.2 to 5.3.5, MYSQL 4.1 to 5.5, md5() to SHA512 using pbkdf2, salt and multiple iterations, MySQL functions to PDO, HTML4 to 5. I'm learning about and trying to implement prepared statements, exceptions and OOP, thanks in large part to reading StackOverflow. Its a lot to wrap my head around and its not done yet (just starting) but I just want to make sure I am on the right track. I connect to MySQL on the webserver for log in/user management and I connect to Sybase on a fileserver for client account database. I have looked into classes but not sure I am ready to go there yet. So please tell me how the code looks, what I can do different/better, logic, flow.... thanks
<?php
set_include_path( 'c:/server/web/new/includes' );
require 'functions.php';
debug_errors();
secure_session_start();
require 'pbkdf2.php';
require 'connect_mysql.php';
require 'connect_sybase.php';
if( isset( $_SESSION[ 'user_id' ] ) )
{
header( "Location: index.php" );
exit();
}
if( isset( $_POST[ 'process' ] ) )
{
try
{
$pdo_mysql = pdo_mysql();
$pdo_sybase = pdo_sybase();
$user_id = $_POST[ 'user_id' ];
$password = $_POST[ 'password' ];
if( !( preg_match( '/^[a-zA-Z0-9]{1,60}$/', $user_id ) ) || strlen( $password ) < 1 || strlen( $password ) > 1024 )
{
$error = 'Please enter a valid User ID and Password';
}
else
{
try
{
$mysql_select = $pdo_mysql->prepare( "SELECT userid, password, email, login_attempts FROM users WHERE userid = :userid LIMIT 1" );
$mysql_select->bindParam( ':userid', $user_id );
$mysql_select->execute();
if( $mysql_select->rowCount() == 1 )
{
$row = $mysql_select->fetch( PDO::FETCH_ASSOC );
if( $row[ 'login_attempts' ] == 5 )
{
$error = 'Your login account has been locked.';
}
else
{
if( validate_password( $password, $row[ 'password' ] ) ) //password is correct
{
//session stuff here
//header to index
}
else //password is wrong
{
$error = 'Incorrect User ID and/or Password';
$login_attempts =++ $row[ 'login_attempts' ];
$mysql_update = $pdo_mysql->prepare( "UPDATE users SET login_attempts = :login_attempts WHERE userid = :userid" );
$mysql_update->bindParam( ':login_attempts', $login_attempts );
$mysql_update->bindParam( ':userid', $row[ 'userid' ] );
$mysql_update->execute();
}
}
}
else
{
$error = 'Incorrect User ID and/or Password';
}
}
catch( PDOException $ex )
{
$error = "A database query error occured.";
log_action( "pdo_mysql", "(" . $ex->getCode() . ")" . $ex->getMessage() );
}
}
}
catch( PDOException $ex )
{
$error = "Unable to connect to database.";
log_action( "pdo_mysql", "(" . $ex->getCode() . ")" . $ex->getMessage() );
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>login</title>
</head>
<body>
<div>login page</div>
<?php
if( !( empty( $error ) ) )
{
echo $error;
}
?>
<form action="login.php" method="post">
<label for="user_id">User ID</label>
<input type="text" name="user_id" />
<label for="password">Password</label>
<input type="text" name="password" />
<input type="hidden" name="process" value="true"/>
<input type="submit" name="submit" value="Submit" />
</form>
</body>