I first want to make this statement that the following script is NOT TESTED meaning that it probably won’t work as is. Secondly, this is just to give you a general idea on how to go about converting to PDO.
Here’s the connection part of the script (I thought this might be handy?):
[php]// Create the database connection as a PDO object:
try {
$db_options = array(
PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
);
$pdo = new PDO('mysql:host=localhost;dbname=your_database_name;charset=utf8', 'your_username', 'your_password', $db_options);
} catch (PDOException $e) { // Report the Error!
$errMsg = "<p>Something is not right, check your php.ini settings or code</p>";
} [/php]
The login in portion of the script:
[php]if (isset($_POST[‘username’])) {
$username = $_POST['username']; // I'm assuming you pull this in from a form?
/* Note whatever table fields that might be need you might want to add to SELECT */
$query = 'SELECT username, password FROM users WHERE username = :username';
/* The parameter values */
$query_params = array(':username' => $username);
try
{
// Execute the query against the database
$stmt = $pdo->prepare($query);
$stmt->execute($query_params);
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
// This variable tells us whether the user has successfully logged in or not.
// We initialize it to false, assuming they have not.
// If we determine that they have entered the right details, then we switch it to true.
$login_ok = false;
// Retrieve the user data from the database. If $row is false, then the username
// they entered is not registered.
$row = $stmt->fetch();
if($row)
{
/* A nice password hashing library for PHP 5 (If not using PHP 5.5)
Find it here: https://github.com/ircmaxell/password_compat/blob/master/lib/password.php
Read the Documentation for further help: */
// Verify Stored Hashed Password:
$result = password_verify($_POST['password'], $row['password']);
if ($result) {
$login_ok = true;
} else {
$errMsg = 'Invalid Credientials!';
}
}
// If login is OK:
if ($login_ok) {
// Unset password *Probably not needed, but I get a little paranoid at times...LOL */
unset($row['password']);
// This stores the user's data into the session at the index 'user'.
// We will check this index on the private members-only page to determine whether
// or not the user is logged in. We can also use it to retrieve
// the user's details.
$_SESSION['username'] = $row['username'];
// Now redirect or display a message saying the person is now logged in
}
}[/php]
A few other comments, the password should be encrypted by some kind of password hashing library (Don’t use your own!). Since you don’t show much code I’m just guessing and probably 99.9 percent wrong in the guess. Just change the password library hash script to an if statement and check against the password that was inputted (Very insecure by the way). Like I said I just threw it together, but if you want show more code and I’m sure someone else will gladly help you out.