Jump to content

[SOLVED] Making Sessions Last Forever?


Xyphon

Recommended Posts

Question... I am switching from cookies over to sessions, for security reasons.. And well, my biggest problem is, cookies you can set the time and make them last months-years, basically as long as you want..

 

But how do you make sessions last forever? Thanks in advance.

Link to comment
Share on other sites

yup ... like

if(!ini_set("session.gc_maxlifetime", "24000")){ //24000 sec long
      throw new Exception("Failed to set execution time");
}

 

I am again emphasizing the fact that just do a simple google search on session.gc_maxlifetime to make sure whether ini_set support session.gc_maxlifetime

Link to comment
Share on other sites

That's because the only purpose of the session garbage collection is to delete old session data files. It is not intended to end sessions or log someone out. It runs randomly, so session data files can exist for any random amount of time past the session.gc_maxlifetime value.

Link to comment
Share on other sites

I just found this, may be useful for you..

 

http://us2.php.net/manual/en/function.session-cache-expire.php

 

Hello,

Some of my customers asked me for a solution form their session not to expire when filling large forms. Sometimes it takes them more than 2 hours to submit (phone, desk customers...).

I know I could have forced an ilayer to be refreshed dynamically and then include PHP code using session_cache_expire() function, but this refreshing a page changes the form focus. So I found this the only solution for them not to loose focus over their form element.

 

You can make a session not to expire by using this code.

Its a mixture of PHP and JavaScript and can be used on the same page were your code goes or it can be called using an ilayer/iframe from your page.

 

I know this is not the best practice, but in some cases were user has no control over server globals and security is not important this can help.

 

Here is the code.

 

<?

//First of all we prevent browsers from caching the image
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

//This piece of code returns a blank gif
if($_GET[img] > 0){
    header('Content-Type: image/gif');
    header("Content-Disposition: inline; filename=".time().".gif");
    echo base64_decode(str_replace("\n","","
R0lGODlhAQABAPcAAAAAAAAAQAAAgAAA/
wAgAAAgQAAggAAg/wBAAABAQABAgABA/
wBgAABgQABggABg/wCAAACAQACAgACA/
wCgAACgQACggACg/wDAAADAQADAgADA/
wD/AAD/QAD/gAD//yAAACAAQCAAgCAA/
yAgACAgQCAggCAg/yBAACBAQCBAgCBA/
yBgACBgQCBggCBg/yCAACCAQCCAgCCA/
yCgACCgQCCggCCg/yDAACDAQCDAgCDA/
yD/ACD/QCD/gCD//0AAAEAAQEAAgEAA/
0AgAEAgQEAggEAg/0BAAEBAQEBAgEBA/
0BgAEBgQEBggEBg/0CAAECAQECAgECA/
0CgAECgQECggECg/0DAAEDAQEDAgEDA/
0D/AED/QED/gED//2AAAGAAQGAAgGAA/
2AgAGAgQGAggGAg/2BAAGBAQGBAgGBA/
2BgAGBgQGBggGBg/2CAAGCAQGCAgGCA/
2CgAGCgQGCggGCg/2DAAGDAQGDAgGDA/
2D/AGD/QGD/gGD//4AAAIAAQIAAgIAA/
4AgAIAgQIAggIAg/4BAAIBAQIBAgIBA/
4BgAIBgQIBggIBg/4CAAICAQICAgICA/
4CgAICgQICggICg/4DAAIDAQIDAgIDA/
4D/AID/QID/gID//6AAAKAAQKAAgKAA/
6AgAKAgQKAggKAg/6BAAKBAQKBAgKBA/
6BgAKBgQKBggKBg/6CAAKCAQKCAgKCA/
6CgAKCgQKCggKCg/6DAAKDAQKDAgKDA/
6D/AKD/QKD/gKD//8AAAMAAQMAAgMAA/
8AgAMAgQMAggMAg/8BAAMBAQMBAgMBA/
8BgAMBgQMBggMBg/8CAAMCAQMCAgMCA/
8CgAMCgQMCggMCg/8DAAMDAQMDAgMDA/
8D/AMD/QMD/gMD///8AAP8AQP8AgP8A/
/8gAP8gQP8ggP8g//9AAP9AQP9AgP9A/
/9gAP9gQP9ggP9g//+AAP+AQP+AgP+A/
/+gAP+gQP+ggP+g///AAP/AQP/AgP/A/
///AP//QP//gP///yH5BAEAAP8ALAAAA
AABAAEAAAgEAP8FBAA7"));
    exit;
}

?>

<!-- HERE YOU CAN INSERT THE HTML OR PHP CODE -->

<img name="keepUpdated" src="<?=$_SELF?>?img=999">
<script language="Javascript">
<!--
var updateEvery = 30;//Seconds
function keepUpdated() {
    timer=setTimeout("updateImage()",1000*updateEvery)
}
function updateImage() {
    var randNum = Math.floor(Math.random()*999);
    document.images.keepUpdated.src ="<?=$_SELF?>?img="+randNum;
    keepUpdated();
}
keepUpdated();
//-->
</script>

 

I hope this can be helpful for someone.

 

Bermi Ferrer

Link to comment
Share on other sites

Why do you think that cookies are less secure?  Is it because people can change the info in them?  Maybe you should should look at the way that you are encrypting the info in the cookies themselves.  That or use sessions with cookies together.  If the server is somehow root hacked people will be able to see your session variables anyway.  Being that you are on a shared server there is always a possibility of that happening.

 

My 2 cents

 

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.