Controlling session Time

<?php
session_start();  
echo session_id();

The code above produces the result below.

I like to make it doesn’t change when I reflesh the page within 10 seconds
And
I like to make it changes when I reflesh the page after 10 seconds.
The following is my trials for it.

after I opened xampp/php/php.ini, I changed like the following.

And I modified more like the following

After modifying like the above.
I reflesh several times expecting it changes after 10 seconds.
But it seems not to change.

(Q1) Is it possible to control the session time?
(Q2) If it is possible to control the session time, How can I make the value of session_id() is changing whenever I reflesh it after 10 seconds?

Why do you want a session time of 10 seconds?

The best way to it with sessions would be to store a timestamp in the session and terminate the session when you detect 10 seconds have passed.

<?php
session_start();

if (!isset($_SESSION['session_start'])) {
    $_SESSION['session_start'] = time();
}

if (isset($_SESSION['session_start']) && $_SESSION['session_start'] < time() - 10) {
    // session ran for more that 10 seconds - buy bye
    session_destroy();
}
1 Like

It’s for tests.
If I set it by 86,400, I can check only one time a day.

Actually I like to make it 86400, instead of 1440.

I like to make it always alive for a day(86400).
How can I make it always alive for a day without changing the session_id() value “7f1te8nvobuc8v2m9d4lvrphh5”?

if I make it like the following, the session will be alive for a day?

session.gc_maxlifetime 86400

From the manual:

session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and potentially cleaned up.

So, yes it might get cleaned up after a day. Meaning the session may last for a day, but it might last longer than a day. It will however not last less than a day.

Good, the later part is absolutely clear in logic.

session.gc_probability=1
session.gc_divisor=1000
session.gc_maxlifetime 86400

I have modifiation1 above and modification2 below.
(Q3) Are both modification1 above and modification2 below absolutely last the session at least for a day?

session.gc_probability=1
session.gc_divisor=1
session.gc_maxlifetime 86400

(Q4) If the answer to (Q3) is yes, what is the difference between the modification1 and modifcation2 ?

This is all in the manual …

But anyway, the gc_probability and gc_divisor together make up the chance that sessions will be cleaned at the start of any PHP request.

So with the settings session.gc_probability=1 and session.gc_divisor=1000 old sessions will be cleaned 1 every 1000 requests. All other 999 requests sessions are not cleaned, even if they expired.

So indeed if you set both to 1 then sessions will always be cleaned, but that has a penalty of being quite heavy. If you really want to stop a session after a certain time you’re better off using the mechanism storing the session start in the session, as I already showed above. That mechanism also has the advantage it will always work regardless of the gc settings in php.ini.

I think the code below is better in running my server for making it less heavy.
How about applying the code below?

session.gc_probability=1
session.gc_divisor=10000
session.gc_maxlifetime 86400

If I visit the page again after 10 seconds with the code below,
I can check that the session is cleared.
However, as soon as I make a refreshing the browser after the session is cleared.
The previous session is still alive with the same value “7f1te8nvobuc8v2m9d4lvrphh5” although what I want is changing the value of the session_id.

<?php
session_start();

if (!isset($_SESSION['session_start'])) {
    $_SESSION['session_start'] = time();
}

if (isset($_SESSION['session_start']) && $_SESSION['session_start'] < time() - 10) {
    // session ran for more that 10 seconds - buy bye
    session_destroy();

There’s a function for that. Look it up in the manual :slight_smile:

Do you mean there’s a special function for changing the value of the session_id after desinated seconds, i.e. 10 seconds on the my example above, in the manual?
I am afraid I have some weakness in reading the manual as a whole due to non-native speaker of English and …

The PHP manual is available in many languages.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.