Saturday 5 November 2016

php - Login Script problem




if (isset($_POST['login'])) {
$query = mysql_query("
SELECT id FROM users
WHERE username = '".mysql_real_escape_string($_POST['username'])."'
AND password = '".mysql_real_escape_string($_POST['password'])."'
");


/* wrong login information? output a message and terminate the script */
if (!mysql_num_rows($query)){
header("Location: error.php");
exit();
}

/* set session with unique index */
$_SESSION['id'] = mysql_result($query, 0, 'id');
mysql_query("UPDATE users SET last_login = NOW(), last_ip = '".$_SERVER['REMOTE_ADDR']."' WHERE id = '{$_SESSION['id']}'");

header("Location: success.php");
exit;
}

/* destroy session */
if (isset($_GET['logout'])){
mysql_query("UPDATE users SET online = '0' WHERE id = '{$_SESSION['id']}'");
session_unset();
session_destroy();
header("Location: index.php");

exit;
}


It works, it does send me to success php on right info but this is what im trying to do:



if (!$_SESSION['id']) { 


Username:



Password:




} else { echo "Logged in"; }





It dont show "Logged in", i have try printed $_SESSION['id'] but nothing. Its all in index.php. What can be the problem? and dont worry about no hash..gonna fix that later


Answer



Have you definitely called session_start?


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...