Problem using the cookies for remember me feature

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 2331 Views - Last Post: 11 March 2012 - 05:24 AM Rate Topic: -----

#1 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Problem using the cookies for remember me feature

Posted 03 March 2012 - 04:11 AM

Problem using the cookies for remember me feature


This is the code I have for creating the cookies
$login_submit	= (isset($_POST['login'])) ? true: false;
$login_remember	= (isset($_POST['remember']) && $_POST['remember'] == 1) ? true: false;

$login_email	= isset($_COOKIE['login_email']) ? $_COOKIE['login_email'] : request_var('login_email', '');
$login_password	= isset($_COOKIE['login_password']) ? $_COOKIE['login_password'] : request_var('login_password', '');

if (!logged_in())
{
	if ($login_submit)
	{
		if ($login_remember)
		{
			//setcookie('imgit_logged_in', 1, time()+31556952);	
			setcookie('login_email', $_POST['login_email'], time()+60*60*24*365, '/');
            setcookie('login_password', md5($_POST['login_password']), time()+60*60*24*365, '/');
		}
//Code continues here,but irrelevant for my problem so I won't post it.



And of course this is the HTML form:

<form method="post" action="">
	<strong class="normal">E-mail:</strong> <input type="email" name="login_email" maxlength="255" class="input_fields" placeholder="Your E-mail" required />&nbsp;&nbsp;
	<strong class="normal">Password:</strong> <input type="password" name="login_password" maxlength="35" class="input_fields" placeholder="Your Password" required />&nbsp;
	<strong class="normal"><input type="checkbox" name="remember" value="remember">Remember</strong>
	<input type="submit" name="login" class="button-small" value="Log in" /> &nbsp;
	<strong class="normal pointer" id="close_login">X</strong>
</form>


And that's what I have to access the cookie:
else if (empty($login_errors) && !$register_submit && logged_in() && !isset($_COOKIE['imgit_note']) || isset($_COOKIE['login_email']) && isset($_COOKIE['login_password']))
	{
		echo '<div class="info" id="welcome_back">Welcome back <span class="capitalize">' . get_username() . '</span>, now upload some pics or try out our tools.';
		echo '<span class="pointer right" title="Dismiss forever" onclick="setCookie(\'imgit_note\', \'1\', 365); $(\'#welcome_back\').fadeOut(\'fast\');">X</span>';
		echo '<span class="pointer right" title="Dismiss" onclick="$(\'#welcome_back\').fadeOut(\'fast\');">✔</span> </div>';
	}


The cookies aren't being created when clicking remember me! don't know what I did wrong.. :\

btw I know that storing the pass and email in the cookie isn't the best idea,but I just want to get it to work now and will take care of security later.

Thanks a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem using the cookies for remember me feature

#2 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:14 AM

You have a dangling parenthesis after $_POST['remember'] == 1)
$login_remember = (isset($_POST['remember']) && $_POST['remember'] == 1) ? true: false;


You might want to try this any way.
$login_remember = (isset($_POST['remember']) ? ($_POST['remember'] == 1) ? true: false;


Where is logged_in() being defined?

You'll probably need to assign the return value from logged_in() to a variable.

$logged = logged_in();


Then use $logged in your conditionals.

This post has been edited by hiddenghost: 03 March 2012 - 05:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:19 AM

OK I changed the $login_remember as you suggested.

This is the logged_in function:
function logged_in()
{
	return (isset($_SESSION['imgit_uid'])) ? true : false;	
}

Was This Post Helpful? 0
  • +
  • -

#4 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:20 AM

What about request_var()?

Are you using phpBB?

What is request_var()?
Was This Post Helpful? 1
  • +
  • -

#5 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:25 AM

I'm not using phpBB.

This is request_var
function request_var($index, $replacement)
{	
	if (empty($index))
	{
		die('Index must be set');	
	}
	
	if (is_integer($index) || is_string($index) && is_integer($replacement) || is_string($replacement))
	{
		$post_var 	  = (!empty($_POST[$index])) ? $_POST[$index] : '';
		$get_var  	  = (!empty($_GET[$index])) ? $_GET[$index] : '';
		$request_var  = (!empty($_REQUEST[$index])) ? $_REQUEST[$index] : '';
		
		return (($post_var) ? $post_var : (($get_var) ? $get_var : (($request_var) ? $request_var : $replacement)));
	}
}



Thank you so much for your time!
Was This Post Helpful? 0
  • +
  • -

#6 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:29 AM

View Postwadie, on 03 March 2012 - 06:19 AM, said:

OK I changed the $login_remember as you suggested.

This is the logged_in function:
function logged_in()
{
	return (isset($_SESSION['imgit_uid'])) ? true : false;	
}


That was quick.

Yes logged_in() could be used by just calling it where ever you want to instead assigning it to a variable.

I would be less calls is you assigned the first call to a variable though.

This post has been edited by hiddenghost: 03 March 2012 - 05:29 AM

Was This Post Helpful? 1
  • +
  • -

#7 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:33 AM

I have to use logged_in in the conditionals in order to access the cookies,right ?

But they are not being created at the first place! :online2long:
Was This Post Helpful? 0
  • +
  • -

#8 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:43 AM

The action is empty.
<form method="post" action="">


You need to set it to a php file.
<form method="post" action="php_file.php">


Preferably to the page that is receiving your $_POST's

This post has been edited by hiddenghost: 03 March 2012 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 05:57 AM

But the login email and pass work in that form without adding a php file name to the action,so why do I have to add it now ?
Was This Post Helpful? 0
  • +
  • -

#10 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 06:10 AM

How do you know they're working?
Can you echo out the value of $_POST?

You have the word 'required' in your form too.
What's 'required' doing in your form?

I'm going to bed now.

Keep trying.

A final word of advice.

Break the code down into smaller parts and test those. Your more likely to see problems with code if you have a small chunk to look at instead of a load of code that could have 20 different things breaking it.

So 1 or 2 things could be breaking one little 3 line chunk of code. Much easier to find 1 or 2 things instead of 20.

This post has been edited by hiddenghost: 03 March 2012 - 06:26 AM

Was This Post Helpful? 1
  • +
  • -

#11 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 07:40 AM

Hi,

You don't have to supply an action to a form element, by default it will just submit to the current page. That is why it is working without an action attribute :)
Was This Post Helpful? 1
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 08:03 AM

Just to point this out:
// The ternary operator here is kind of redundant. 
// The isset() function already returns a boolean.
$res = (isset($_POST['login'])) ? true: false;

// You may as well just leave it at:
$res = isset($_POST['login']);


// This also works fine:
$res = (isset($_POST['login']) && $_POST['login'] == 'something');


Was This Post Helpful? 1
  • +
  • -

#13 wadie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-March 12

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 10:40 AM

Thank you guys so much for your time and for taking it slow with me,since as you might have figured out I'm still a noob. xD

I did it all again,started from the original files like new!

Still,the cookies aren't being created and I don't think I'm doing anything wrong.

HTML:
<form method="post" action="">
	<strong class="normal">E-mail:</strong> <input type="email" name="login_email" maxlength="255" class="input_fields" placeholder="Your E-mail" required />&nbsp;&nbsp;
	<strong class="normal">Password:</strong> <input type="password" name="login_password" maxlength="35" class="input_fields" placeholder="Your Password" required />&nbsp;
	<strong class="normal"> <input type="checkbox" name="remember" value="remember"/>Remember</strong>
	<input type="submit" name="login" class="button-small" value="Log in" /> &nbsp;
	<strong class="normal pointer" id="close_login">X</strong>
</form>


Creating the cookies:

if (!logged_in())
{
	if ($login_submit)
	{
		if ($login_remember)
		{
			//setcookie('imgit_logged_in', 1, time()+31556952);
			setcookie('imgit_email',$login_email,time()+31556952,'/','localhost',false);
			setcookie('imgit_password',$login_password,time()+31556952,'/','localhost',false);
			
		}


And this is the code that checks if cookies exist in order to log in if they do (that's the least important part for now,since I don't even have the cookies that I want to use!)

else if (empty($login_errors) && !$register_submit && logged_in() && !isset($_COOKIE['imgit_note']) && isset($_COOKIE['imgit_email']) && isset($_COOKIE['imgit_password']))
	{
		echo '<div class="info" id="welcome_back">Welcome back <span class="capitalize">' . get_username() . '</span>, now upload some pics or try out our tools.';
		echo '<span class="pointer right" title="Dismiss forever" onclick="setCookie(\'imgit_note\', \'1\', 365); $(\'#welcome_back\').fadeOut(\'fast\');">X</span>';
		echo '<span class="pointer right" title="Dismiss" onclick="$(\'#welcome_back\').fadeOut(\'fast\');">✔</span> </div>';
	}


Let's take it from here again..what's the problem ? :o
Was This Post Helpful? 0
  • +
  • -

#14 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 01:35 PM

View PostJstall, on 03 March 2012 - 08:40 AM, said:

Hi,

You don't have to supply an action to a form element, by default it will just submit to the current page. That is why it is working without an action attribute :)


Ah. I considered that too. But I had to see.
Was This Post Helpful? 0
  • +
  • -

#15 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Problem using the cookies for remember me feature

Posted 03 March 2012 - 02:00 PM

I'm tellin ya start with this:
<form method="post" action="">
	<strong class="normal">E-mail:</strong> <input type="email" name="login_email" maxlength="255" class="input_fields" placeholder="Your E-mail">
	<input type="submit" name="login" class="button-small" value="Log in">
</form>


And this:
$email_stored = $_POST['login_email'];

print_r($_POST);

setcookie('email', $email_stored, time()+60*60*24*365, '/');

print_r($_COOKIE);



Slowly add to that about the same amount of code until you get where you originally were.

The whole of the above should look like:
<?php
$email_stored = $_POST['login_email'];

setcookie('email', $email_stored, time()+60*60*24*365, '/');

?>
<html>
<head></head>
<body>
<?php
print_r($_POST);
print_r($_COOKIE);
?>
<form method="post" action="">
	<strong class="normal">E-mail:</strong> <input type="email" name="login_email" maxlength="255" class="input_fields" placeholder="Your E-mail">
	<input type="submit" name="login" class="button-small" value="Log in">
</form>
</body>
</html>



Keeping in mind that cookies must be set before any output of html inside of <html></html>

When ever you add another cookie there's no need to add another print_r($_COOKIE); bacause print_r will output all your cookies. when you script is done and working you can delete the print_r($_COOKIE); line or comment it out and you should be set.

Also when cookies are set they won't be available right away. You'll have to reload the page to see them work.

This order of action is not going to reveal them:
Submit form ---> post info ---> set cookie
You need to be able to:
Submit form ---> post info ---> set cookie ---> open page again ---> see cookie results.

When your logic is:

if($_COOKIE['my_cookie']){
// do something
}
when you first submit the form you will not see the cookie set.
You would have to open the page again to see it set and to // do something.

This post has been edited by hiddenghost: 03 March 2012 - 02:03 PM

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3