473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to set a session to either 365 days or to when browser closes

I have this PHP code (PHP5):

if($autologin == "on")
session_set_cookie_params(60*60*24*365); // 365 days
else
session_set_cookie_params(0); // when browser closes
session_start();

In my php.ini file I have this line, which I guess will give me a 365
days timeout on my PHPSESSID server session:

session.cookie_lifetime = 31536000

My problem is, that whatever I do, the session is set to 365 days no
matter if I have "autologin" on or off. Autologin is a checkbox that
asks the user if he wants to login automatically every time he visits my
page.

If I set the session.cookie_lifetime to the default value of 0, the
session expires always when the browser closes.

What am I doing wrong?
Jul 17 '05 #1
6 2103
FamB wrote:
My problem is, that whatever I do, the session is set to 365 days no
matter if I have "autologin" on or off. Autologin is a checkbox that
asks the user if he wants to login automatically every time he visits
my page.


session_set_cookie_params() should be called before session_start(), is this
the case?
JW

Jul 17 '05 #2
Janwillem Borleffs wrote:
session_set_cookie_params() should be called before session_start(),
is this the case?


Another problem can be the way you are accessing the value of the autologin
checkbox. The way you are doing this relies on register_globals being
enabled, which isn't the case by default in PHP5.

See if using $_REQUEST['autologin'] instead of using $autologin solves the
problem when session_set_cookie_params() and session_start() are called in
the correct order.
JW

Jul 17 '05 #3
>>session_set_cookie_params() should be called before session_start(),
is this the case?

No. See my code? ;-) It is not all the code, but just the session part
of it.
Another problem can be the way you are accessing the value of the autologin
checkbox. The way you are doing this relies on register_globals being
enabled, which isn't the case by default in PHP5.

See if using $_REQUEST['autologin'] instead of using $autologin solves the
problem when session_set_cookie_params() and session_start() are called in
the correct order.


That is not the problem either. I can read the autologin value, as I get
it from the REQUEST.

The problem is, that it doesn't matter whast I write in the
"session_set_cookie_params(whatever)" function - the timeout is always
the value I have specified in the php.ini file
(session.cookie_lifetime=31536000)
Jul 17 '05 #4
FamB wrote:
No. See my code? ;-) It is not all the code, but just the session part
of it.


Your code snippet doesn't reveal if session_start is called before or after
session_set_cookie_params(), that's why I'm asking.

To quote the manual:

"The effect of this function only lasts for the duration of the script.
Thus, you need to call session_set_cookie_params() for every request and
before session_start() is called."
JW
Jul 17 '05 #5
>>No. See my code? ;-) It is not all the code, but just the session part
of it.


Your code snippet doesn't reveal if session_start is called before or after
session_set_cookie_params(), that's why I'm asking.

To quote the manual:
"The effect of this function only lasts for the duration of the script.
Thus, you need to call session_set_cookie_params() for every request and
before session_start() is called."


if($autologin == "on")
session_set_cookie_params(60*60*24*365); // 365 days
else
session_set_cookie_params(0); // when browser closes
session_start();

I am sorry, but how can you be in doubt, that session_start is called
after session_set_cookie? There is an if-else before session_start.
:-)
Jul 17 '05 #6
FamB wrote:
I am sorry, but how can you be in doubt, that session_start is called
after session_set_cookie? There is an if-else before session_start.
:-)


Overlooked that one completely.... Anyways, what happens when you run the
following script:

<?php

session_set_cookie_params(0);

session_start();
if (!isset($_SESSION['debug'])) {
print "setting debug";
$_SESSION['debug'] = 1;
} else {
print $_SESSION['debug'];
}

?>

Each time you close your browser, the "setting debug" message should be
displayed. When this is the case, it's possible the session part of your
code is never reached for some reason.
JW

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: megha | last post by:
Hi I am writing a program in JSP which requires user to log in and as he logs in database was updated with his status on and till the user does not sign out no other user with same login and...
4
by: megha | last post by:
Hi I am writing a program in JSP which requires user to log in and as he logs in database was updated with his status on and till the user does not sign out no other user with same login and...
9
by: Pack Fan | last post by:
I've noticed that session variables will persist on Mac IE even after all browser windows have been closed. One must quit the program to clear the session variables. This presents a security risk...
14
by: Darrin J Olson | last post by:
I am trying to end a session for a site without having to completely close the browser to end it. When I access the site and log in it works fine. If I log out and in with a different account...
4
by: Rafael Chemtob | last post by:
Hi, I'm using session cookies on my web app and it looks like the session is not being killed when the user closes the browser. We use a Global.asa file on our app. Do we have to explicitly...
11
by: David Lozzi | last post by:
Hello All, I am having an issue with thus far one computer on my client's web site. If the user loads the shopping cart and then closes all browser windows, then opens them back up, goes back to...
5
by: news.microsoft.com | last post by:
Hi everyone, I need some help (may be in the form of some sample code) for the subject question. I have an ASP.NET/C# application. I need to do quite a few tasks when the session ends. I...
6
by: laredotornado | last post by:
Hi, When a user logs into our site, we create a session variable to denote the session is active and another to denote who is logged in. Once the user closes the browser and re-opens it, the...
5
by: bgold12 | last post by:
When a user logs onto my site, I set some session data using php: .... session_start(); .... $_SESSION = 'value'; .... From what I understand, this should store the session data (i.e. the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.