Jump to content

Sessions..


jayjay960

Recommended Posts

Ok, this is driving me insane. Here is a script that runs at the start of each page (before any html):

<?php
$rememberme = false;
if (isset($_POST['rememberme']))
{
	$rememberme = true;
	$logintime = time();
}
if ($rememberme || isset($_SESSION['rememberme']))
{
	session_set_cookie_params($_SESSION['logintime']+(60*60*24*30));
}
session_start();
if ($rememberme)
{
	$_SESSION['rememberme'] = true;
}
?>
What I'm trying to do is have it so that when you login and have checked the checkbox named 'rememberme', the session cookie lasts for 30 days. Currently, if you log in without checking the box the cookie will expire at the end of the session as normal, however, if you tick the remember me box, the exact same thing happens. No expiry date. What's annoying me is that if I put session_set_cookie_params(60*60*24*30); directly before session_start();, it works fine and the cookie has an expiry date, but once I try to check if the user chose remember me, it stops working. The first thing I tried was simply:
[code]<?php
if (isset($_POST['rememberme']))
{
session_set_cookie_params(60*60*24*30);
}
session_start();
?>

 

But that didn't work, and since then I've tried every possible way of doing this I could think of, including destroying the session cookie before setting the cookie params, and even just setcookie('PHPSESSID', session_id(), '', 60*60*24*30);, and I've had different results each time, including (from the look of it) my $_SESSION variables being reset, being able to log in without checking the box but not with, and getting errors from session_start() that say my headers have already been sent.

 

I would really, really appreciate help with this, it's driving me insane..

Link to comment
Share on other sites

A session is intended to last one browser session (that's where it gets its name from.) You can extend a session to last longer than one browser session but you must set both the session.cookie_lifetime and the session.gc_maxlifetime and if you are on a shared web server where the session.save_path is the default location, you must set to to a private folder within your account's folder tree so that you session setting only apply to your session data files. All of these settings must be set before every session_start() (globally putting them into the master php.ini, a local php.ini, or a .htaccess file is the recommend method.)

 

Short answer - to implement a "remember me" feature, it is best to use a regular cookie and don't attempt to extend the session. Use a session for its' intended purpose, to hold variables that you want to persist during one browser session.

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.