Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting SESSION var from GET var 2

Status
Not open for further replies.

anulaibar

Programmer
Mar 1, 2003
17
0
0
SE
I have the following piece of code to get a value from the user which stylesheet to apply to the page.

session_start();
if (isset($_GET['css'])){
$_SESSION['css'] = $_GET['css'];
}
if (!isset($_SESSION['css'])){
$_SESSION['css'] = "3";
}

I then call the variable in the link tag:
<link href=&quot;['css']?>.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; />

It works for the first page, but somehow the session variable seems to be killed when i browse around the page.
Does it have something to do with the session_start() method?

Thanks for any help...
 
I always make the mistake of not putting session_start(); at the top of every script where I am trying to use session variables.

I spend hours trying to debug the scripts, make myself a coffee and then remember to put it in. Now I make the coffee first

MrBelfry
 
The piece of code above is in a file called start.php which I include in every page.

Att you can see what I mean. From the indexpage I send the get variable, and on the next page, in the right nav bar, you can choose your stylesheet.

It used to work before, I don't know what has happened.

Thanks,
Olle
 
Code:
if (isset($_GET['css'])){
       $css = $_GET['css'];
       setcookie('css', $css);
    } elseif (isset($_COOKIE['css'])) {
        $css = $_COOKIE['css'];
    } else {
        $css = '3';
        setcookie('css', $css);
    }

<link href=&quot;[URL unfurl="true"]http://www.medieteknik.itn.liu.se/bas/css/styles<?=$css?>.css&quot;[/URL] rel=&quot;stylesheet&quot; type=&quot;text/css&quot; />

(-:
 
Thanks guys for replying,

A friend suggested that the session vars are set correctly, but that the temp directory on the server is full, so that no new files can be stored. That would explain why the script suddenly stopped working, but it doesn't solve my problem.
I'm not sure about how sessions are stored, but i thought all session vars were deleted when you close the browser window...?

I would be thankful for any thoughts or suggestions on the issue.

Olle
 
The cookie stored on the browser that contains the session store index is deleted when the browser is closed. But your server cannot know when a browser is closed -- HTTP is a stateless protocol, so there is no permanent connection between the server and the browser. PHP can't know when a user has closed his browser.

PHP's session-handling system has a garbage collection mechanism. What the garbage-collection deletes when is dependent on the php.ini settings session.gc_probability and session.gc_maxlifetime.

Every time a script issues session_start(), PHP picks a random number between 1 and 100. If that number is less than or equal to the setting of session.gc_probability, then the garbage-collection mechanism kicks off.

If the garbage-collection mechanism activates, it deletes every session store older than the setting session.gc_maxlifetime.

By default, session.gc_probability is set to 1. That means that on average over the long term, the garbage-collection mechanism will only run once out of every 100 times a script invokes session_start() (but it can, of course, happen a lot less often than that). On my systems, I have set session.gc_probability to 100. That way, the garbage-collection mechanism runs every time session_start() is invoked.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir214: Excellent explanation of the nitty-gritty of session garbage collection.

A star for you!

-Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
Yes, thanks a lot for your help sleipnir214

Olle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top