Jump to content

cookies


doddsey_65

Recommended Posts

Okay im having a problem with cookies so if anyone can help i would be grateful.

 

When you login you can choose how long to stay in for. For testing purposes the choices are:

 

  • Forever
  • 1 Hour
  • 1 Day
  • Never

 

Based on your choice i am setting the cookie expiration as follows:

 

if ($_POST['remember_me'] == '1')
{
	setcookie('remember', time() + 99999999999999);
}
elseif ($_POST['remember_me'] == '3600')
{
	setcookie('remember', time() + 3600);
}
elseif ($_POST['remember_me'] == '84600')
{
	setcookie('remember', time() + 84600);
}
elseif ($_POST['remember_me'] == '0')
{
	setcookie('remember');
}		
echo $_COOKIE['remember']; 	
}

 

Then for testing I am echoing the cookie at the head of the document:

 

echo "Cookie: " . @$_COOKIE['remember'];	

 

The problem is that when the browser is closed the cookie is gone. Only the last option "never" is set as a session cookie which means the others

should stay active even when the browser is closed shouldnt they?

 

Anything i have missed here?

Link to comment
Share on other sites

If you aren't able to echo the cookie in the code above, it's because the value of a cookie isn't available until the next page load.

setcookie('my_cookie', 'snickerdoodle');
echo $_COOKIE['my_cookie'];

Will not work.

 

As far as the expirations not being there, you're not setting the cookies properly. This setcookie('remember', time() + 3600); will set a cookie with the name 'remember' and a value of time()+3600, not an expiration of time()+3600. Even though some cookie parameters are optional, you still have to indicate which parameter you're trying to set. If you want to set a cookie named 'remember', that expires in an hour:

setcookie('remember', 'value' , time() + 3600);  //sets the cookie properly
setcookie('remember', time() = 3600) // sets a cookie named 'remember', with a [i]value[/i] of time() + 3600, that expires at the end of the session.

Link to comment
Share on other sites

even setting:

 

setcookie('remember', 'Remember me', time() + 99999999);

 

the cookie is still deleted when the browser session finishes. I tried setting the cookie path in php ini but that broke the sessions. Sessions werent being strored properly for some reason, so i got rid of the change(c:/wamp/cookies) and the sessions started working again. But i cant get the cookies to work.

Link to comment
Share on other sites

error reporting is enabled but i get nothing. Here is the code up to where i am setting the cookies:

 

<?php ob_start();
session_start();

$error = '';
include ('includes/header.php');

$pagetitle = 'ASF • Login';

// if they are already logged in
if (@$_SESSION['user_id']) {
header ('Location: '.@$_SESSION['back_page']);
}

if (isset($_POST['login_btn']))
{
$user_name=mysql_real_escape_string($_POST['username']);
$pass=sha1(md5(md5(sha1(sha1($_POST['password'])))));

$check=mysql_query("SELECT user_id, user_username, 
				user_password, user_group
				FROM ".DB_PREFIX."members 
				WHERE user_username='".$user_name."'
				AND user_password = '$pass'");

$CNumb = mysql_num_rows($check);
$info = mysql_fetch_object($check);

if ($CNumb == 1)
{
$_SESSION['user_id'] = $info->user_id;
$_SESSION['user_group'] = $info->user_group;
$_SESSION['session'] = session_id();
$_SESSION['user_username'] = $user_name;

mysql_query("UPDATE ".DB_PREFIX."members
		SET last_login = '".date('Y-m-d')."'
		WHERE user_username = '$user_name'");

mysql_query("INSERT INTO ".DB_PREFIX."sessions 
				VALUES(NULL, 
				'".$_SESSION['user_id']."', 
				'".$_SESSION['user_username']."', 
				'".$_SESSION['user_group']."',  
				'".$_SESSION['session']."', 
				'".$_SERVER['REMOTE_ADDR']."', 
				'".$_SERVER['HTTP_USER_AGENT']."', 
				'".date('Y-m-d')."')");		

setcookie('remember', 'Remember me', time() + 99999999);

}
}

 

i got rid of the option to choose a time and simply set the cookie once for testing.

Link to comment
Share on other sites

You should really structure this so you don't need output buffering. Although I can't confirm it since I don't have your DB structure and code, I think that may be masking the errors you would be seeing when the script runs.

 

But in any event, delete the cookies, run the script again, and view the data using the 'view cookies' option in your browser and see what it says. When I copy your setcookie() into a script and run it locally, it sets the cookie properly, with an expiration date of Tue, 28 Jan 2014 02:36:35 GMT

Link to comment
Share on other sites

could this be something to do with being logged in. when a user logs in i set the cookie and then send the session data to the database. To check if they are logged in i check if $_SESSION['logged_in'] == TRUE but i dont do anything with the cookie. Should i be doing something?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.