i am having problem with converting my sign in script with pdo
here is my script
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_name'] == 'loginform')
{
$success_page = $_COOKIE["redirect"];
$error_page = 'signup_error.php';
$indexlogcheck = 'home.php';
$mysql_table = 'users';
$crypt_pass = md5($_POST['password']);
$found = false;
$fullname = '';
$session_timeout = time()+60*60*24*30;
try
{
$pdo = new PDO('mysql:host=localhost;dbname=blog', 'Avik', '');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
echo $output;
exit();
}
try
{
$sql = "SELECT password, fullname, active FROM ".$mysql_table." WHERE username = '".$_POST['username']."'";
$statement = $pdo->prepare($sql);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($data = count($result) == 1)
{
if ($crypt_pass == $data['password'] && $data['active'] != 0)
{
$found = true;
$fullname = $data['fullname'];
}
}
if($found == false)
{
header('Location: '.$error_page);
exit;
}
else
{
if (session_id() == "")
{
session_start();
}
$_SESSION['username'] = $_POST['username'];
$_SESSION['fullname'] = $fullname;
$_SESSION['expires_by'] = time() + $session_timeout;
$_SESSION['expires_timeout'] = $session_timeout;
$rememberme = isset($_POST['rememberme']) ? true : false;
if ($rememberme)
{
setcookie('username', $_POST['username'], time() + 3600*24*30);
}
header('Location: '.$success_page);
exit;
}
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
echo $output;
exit();
}
}
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
so where am i doing wrong!!! this script works if i changed it into below codes
try
{
$sql = "SELECT password, fullname, active FROM ".$mysql_table." WHERE username = '".$_POST['username']."' AND password = '".$crypt_pass."'";
$statement = $pdo->prepare($sql);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if (count($result) == 1)
{
if (session_id() == "")
{
session_start();
}
$_SESSION['username'] = $_POST['username'];
$_SESSION['fullname'] = $fullname;
$_SESSION['expires_by'] = time() + $session_timeout;
$_SESSION['expires_timeout'] = $session_timeout;
$rememberme = isset($_POST['rememberme']) ? true : false;
if ($rememberme)
{
setcookie('username', $_POST['username'], time() + 3600*24*30);
}
header('Location: '.$success_page);
exit;
}
else
{
header('Location: '.$error_page);
exit;
}
}
but i want to know whats wrong with the code above!!