doddsey_65 Posted November 26, 2010 Share Posted November 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/ Share on other sites More sharing options...
doddsey_65 Posted November 26, 2010 Author Share Posted November 26, 2010 also when i echo $_COOKIE['remember_me'] i get an undefined error Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1139875 Share on other sites More sharing options...
MrXHellboy Posted November 26, 2010 Share Posted November 26, 2010 You're sure its $_COOKIE['remember_me']; You didnt set that, so that would be logical. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1139876 Share on other sites More sharing options...
doddsey_65 Posted November 26, 2010 Author Share Posted November 26, 2010 i meant $_COOKIE['remember'] Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1139878 Share on other sites More sharing options...
revraz Posted November 26, 2010 Share Posted November 26, 2010 Check your server's php.ini and make sure the cookie path is valid. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1139891 Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2010 Share Posted November 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140092 Share on other sites More sharing options...
doddsey_65 Posted November 27, 2010 Author Share Posted November 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140240 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2010 Share Posted November 27, 2010 Do you have error reporting enabled? You'll need to post the script from the beginning to the point at which the cookies are set. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140326 Share on other sites More sharing options...
doddsey_65 Posted November 27, 2010 Author Share Posted November 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140327 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2010 Share Posted November 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140335 Share on other sites More sharing options...
doddsey_65 Posted November 27, 2010 Author Share Posted November 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140340 Share on other sites More sharing options...
Pikachu2000 Posted November 27, 2010 Share Posted November 27, 2010 There's no way to answer that without knowing if the cookie is being set or not. The problem needs to be defined before the solution can be found. Quote Link to comment https://forums.phpfreaks.com/topic/219889-cookies/#findComment-1140344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.