Jump to content

Correct way of doing a PHP Timeout


j.smith1981

Recommended Posts

I was wanting to make some kind of session timeout ability, just purely out of curiosity.

 

I have tried going off my own theory, before looking at any tutorials, well have looked a bit but tried getting the jist and having a go myself the next day (anyways that being said), this code here:

 

<?php
ini_set('display_errors', 1);
session_name("jeremysmith_test_session");
session_start();

$_SESSION['start_time'] = time();

if($_SESSION['start_time'] < $_SESSION['start_time'] + 500) {
  printf("Time out be out dated!");
}

 

Just does not seem to want to work, is there any reason for this, that you could think of?

 

I appreciate any replies,

Jeremy.

Link to comment
Share on other sites

Look at it. On each request you set $_SESSION['start_time'] to the current time. Meaning, it will always equal less than the current time + 500.

 

You need to set an end_time on the first request and check start_time against that on subsequent requests.

Link to comment
Share on other sites

Oh ok then that is fine.

 

Will work this one out and try it out maybe on a subsequent page, just to make it feel like a real system so to speak.

 

I mean for a user defined time out, it is in seconds is not it? I mean if a user was to say not use a no time out, in a checkbox element, they could say want their login valid for 60minutes, but in the way their inputting it, would be multiplied by 60?

 

Am I thinking about this correctly?

 

Plus if I wanted to do purely MySQL authentication time outs, then I would set when the users session would become invalid right? Saying something like:

 

<?php
['sessiontime'] <= time();
?>

 

Am I thinking alright about this?

 

Thank you for your reply and I look forward to your next response,

Jeremy.

Link to comment
Share on other sites

Sorry it does not matter got it working now:

 

if(time() >= $_SESSION['timeout'] )

 

Obviously me just waffling, but got it working, using say time() + 60 * 60 (so I can theoretically set a text box, that a user can enter to say how long they want their session (if I allow this), for them to stay logged in or a checkbox to allow them to stay permenantly logged in.

 

Thank you for all your help, just needed to go over some logic.

 

Jeremy.

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.