Just started using PDO for my newest project. I'd like to get input on how this login script looks and if I should alter it in any way.
When the user clicks the link to the index.php page, a script immediately fires and checks if the windows username exists in the users table in the DB.
<?php
include("include/sessions.php");
$windowusername = $_SERVER['REMOTE_USER'];
$sql = 'SELECT , username, fullname, email FROM users WHERE username = :username';
$sth = $dbc->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':username' => $windowusername));
$res = $sth->fetchAll(PDO::FETCH_ASSOC);
$numrows = count($res);
if($numrows == 1)
{
foreach($res as $row)
{
$_SESSION['user'] = $row;
}
header('Location: home.php');
}
else
{
header('Location: chassisRegistration.php');
}
$this->connection = null; // <- wasn't sure if this is necessary
?>
All of the above works without any errors.
As long as the windows username exists in the users table, the session variable is set, then route the user to the home page.
If the user does not exist, route them to the registration page.
I would appreciate any input on how this script looks. Please let me know if there is anything I should do differently.
Thank you in advance.
exit()
or__halt_compiler();
after usingheader(...)
.. The reason behide this because it can be possible to get into unvalid execution paths in the code when the html client is ignoring HTTP location headers.. \$\endgroup\$header('Location: home.php'); exit();
because its in fact a vulnerability to trust the HTTP client to respect the HTTP location header. "Are you referring to the closing of the connection?" And ideally this line of code should happen before anny redirects. \$\endgroup\$