Login in system not defining index

+2 Gary Miller · November 19, 2014
function user_id_from_username($username) {
$username = sanitize($username);
return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

this function gets id for


function login($username, $password) {
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);

if (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1){
return $user_id;
}else{ 
return false;
}


}

and it does work because instead of returning $user_id above , if i echoed it would show but..

<?php 
include 'core/init.php';
logged_in_redirect();
if(empty($_POST) === false) {
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) === true || empty($password) === true){
$errors[] = 'You need to enter a username and password';
}else if(user_exists($username) === false){
$errors[] = 'We can\'t find that username and or password';
}else if (user_active($username) === false) {
$errors[] = 'Please activate your account';
}else {

if(strlen($password) > 32) {
$errors[] = 'Password too long';
}

$login = login($username, $password);
if($login === false) {
$errors[] = 'That usernmae/password combination is incorrect';
} else {

$_SESSION['user_id'] = $login; 
header('Location: index.php');
exit();
}
}

} else {
$errors[] = 'No data received.';
}
include 'includes/overall/header.php';
if (empty ($errors) === false) {

?>
<h2>We tried to log you in, but...</h2>
<?php 
echo output_errors($errors);
}
include 'includes/overall/footer.php';
?>

the $_SESSION['user_id'] remains undefined 
this system worked for the longest time then all of a sudden the it wont set the user_id... idk what could be wrong, I've spent hours trying to figure it out. I've been doing a lot of other coding in other .php pages for my site but i havent messed with this set up. Is there something else I could of done that is messing this up?

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Miller · November 19, 2014
If i echo $login instead of setting it = to $_SESSION it does echo out the id!? Why wont it set it?
0 Gary Miller · November 19, 2014
I would delete this if i could.. I woe up this morning and realized that I passed an invalid argument in my user_data function. It had the id but it couldn't get the data because of the invalid.
  • 1

PHP

106,942 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators