I am a PHP hobbyist. I stepped away for a few years, and now I am back. When I left, I was using mysql_query. I have now read PDO is all the rage. I am now teaching myself PDO using online tutorials.
If someone who was experienced with PDO wouldn't mind, could you please check the below code I created utilizing PDO to validate a simple user name and password authorization script? It seems to work for me. I would just like to know if I am doing something wrong, and if my script is secured as it can be. I would very much appreciate it. Thanks.
<?php
include_once 'includes/connect.php';
session_start();
// username and password sent from form
$myusername = $_POST['myusername'];
$mypassword = $_POST['mypassword'];
//scrub against SQL Injection - Is this still needed now that I use PDO???
$myusername = strip_tags($myusername);
$mypassword = strip_tags($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
//check to see if user name AND password exists in Database
$stmt = $db->prepare("SELECT * FROM userinfo WHERE UserUserName = ? AND UserPassword = ?");
$stmt->execute(array($myusername, $mypassword));
$accountValid = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($accountValid)
{
//Valid User Name and Password - Get User ID, and then set Session Variables
$stmt2 = $db->prepare("SELECT UserID FROM userinfo WHERE UserUserName = ? AND UserPassword = ?");
$stmt2->execute(array($myusername, $mypassword));
$userid = $stmt2->fetchColumn();
$_SESSION['username'] = $myusername;
$_SESSION['userid'] = $userid;
echo "Correct Username and Password: " . $_SESSION['username'] . " " . $_SESSION['userid'];
}
else
{
//Invalid User name or Password
echo "Wrong Username or Password";
}
?>