Help Needed In Remember Me Feature In Login!

Ask about a PHP problem here.
Post Reply
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Help Needed In Remember Me Feature In Login!

Post by Shahlin »

I Have A Login With Remember Me Feature In It.
The Problem Is When A Person Checks 'Remember Me' And Logs In, Then His Data Isn't Shown. When He Logs In Without Checking 'Remember Me', His Data Is Shown!
Everything Works Fine! Except The User's Data Isn't Shown.

This Is The Notice I Get : Undefined index: user_id in C:\xampp\htdocs\vu\func\user.func.php on line 17

My user_data function!
function user_data() {
    $args = func_get_args () ;
    $fields = '`'.implode('` , `',$args).'`' ;
	
    $id = (isset($_COOKIE['user_id'])) ? (int)$_COOKIE['user_id'] : $_SESSION['user_id'];
	
    $query = mysql_query ("SELECT $fields FROM `users` WHERE `user_id`='".$id."' ") ;
    $query_result = mysql_fetch_assoc($query) ;
    foreach ($args as $field) {
    $args [$field] = $query_result[$field] ;
    }
    return $args ;
}
My Login Page :
<?php
if (logged_in()) {
$user_data = user_data ('name') ;
	echo 'Hello! '.$user_data['name'] ;
} else {
?>

<form action="" method="POST">
	<p>
		Email : <input type="email" name="login_email" />
		Password : <input type="password" name="login_password" />
		<input type="checkbox" name="rememberme" /> Remember Me
		<input type="submit" value="Log in" />
	</p>
</form>

<?php
}
if (isset ($_POST['login_email'], $_POST['login_password'])) {

	$login_email = $_POST['login_email'] ;
	$login_password = $_POST['login_password'] ;
	$rememberme = $_POST['rememberme'] ;
	
	$errors = array() ;

	if (empty ($login_email) || empty ($login_password)) {
		$errors[] = 'Email and password required.';
	} else {
	
	$login = login_check ($login_email,$login_password) ;
	
	if ($login === false) {
			$errors[] = 'Please type in a valid email/password.' ;
	    } 
	
	}
	if (!empty ($errors)) {
		foreach ($errors as $error) {
			echo '<li>'.$error.'<br />' ;
		}
	} else {
		if ($rememberme=='on')
		setcookie ('username', $login_email, time()+7200) ;
		
		else if ($rememberme=="")
		$_SESSION['user_id'] = $login ;
		header ('Location: index.php') ;
		exit () ;
	}
	
}
?>
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Help Needed In Remember Me Feature In Login!

Post by jacek »

    foreach ($args as $field) {
    $args [$field] = $query_result[$field] ;
    }
    return $args ;
You should use a different variable in place of $args in the loop really.

Also this code is kind of all in the wrong order, maybe try my version of the login tutorial ?
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Help Needed In Remember Me Feature In Login!

Post by Shahlin »

jacek wrote:
    foreach ($args as $field) {
    $args [$field] = $query_result[$field] ;
    }
    return $args ;
You should use a different variable in place of $args in the loop really.

Also this code is kind of all in the wrong order, maybe try my version of the login tutorial ?
Uhmmm Can You Tell Me The Solution? :)

I Already Watched Your Video And Created A Perfect One! But I Don't Know How To Use That And Connect It With My Page.
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Help Needed In Remember Me Feature In Login!

Post by jacek »

Shahlin wrote:Uhmmm Can You Tell Me The Solution? :)
Well the solution would be just explaining how I do login pages, which is in the videos :?

The actual error is caused by this line
$id = (isset($_COOKIE['user_id'])) ? (int)$_COOKIE['user_id'] : $_SESSION['user_id']
it happens because the variable $_SESSION['user_id'] has no value. Was the code you posted the full thing, because it night be as simple as you forgot the session_Start() call.
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Help Needed In Remember Me Feature In Login!

Post by Shahlin »

jacek wrote:
$id = (isset($_COOKIE['user_id'])) ? (int)$_COOKIE['user_id'] : $_SESSION['user_id']
it happens because the variable $_SESSION['user_id'] has no value. Was the code you posted the full thing, because it night be as simple as you forgot the session_Start() call.
EDITED : Uhmmm Nevermind. I Managed To Fix It! :D
Thanks Anyways! :D
Just A PHP Beginner!
Post Reply