Forum Moderators: coopster

Message Too Old, No Replies

Did I get my SESSION variable coding right?

I wrote this short code but t doesn't work...

         

PieSocial

12:20 am on Apr 9, 2005 (gmt 0)

10+ Year Member



Here is the code that i've written:

<snip>

Made an image so it's easier to read.

When I visit my page it seems to set the SESSION variable every time. Even when I visit the page without sending a querystring and the SESSION variable is already set.. What have I done wrong in my code?

//set SESSION colour variable.
If I've passed a "querystring" to change the colours then change SESSION colour.

//extract SESSION colour variable.
If SESSION variable exists then extract it otherwise choose to set SESSION variable to orange.

//setup PAGE colors.
Use SESSION variable to setup my page colours.

[edited by: jatar_k at 7:17 am (utc) on April 9, 2005]

PieSocial

2:04 am on Apr 9, 2005 (gmt 0)

10+ Year Member



Here's the code too. In text:

<?php

//set SESSION color variable.
if($_REQUEST['color']) {
$_SESSION['color'] = $_REQUEST['color'];
}

//extract SESSION color variable.
$color = "or";
if(isset($_SESSION['color'])) $color = $_SESSION['color'];

//setup PAGE colors.
if($color=="bl") $bgcolor = 'bgblue';
elseif($color=="gr") $bgcolor = 'bggreen';
elseif($color=="pi") $bgcolor = 'bgpink';
else {
$_SESSION['color'] = "or";
$color = "or";
$bgcolor = "bgorange";
}
$imagename = './leftofcube_'.$color.'.png';

?>

IamStang

2:19 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



Someone else can pipe up if this is not the best way to check. I am fairly new to PHP and was told about this on another board.

Simple check to see if it works. Can be modified for any session. Write it in a new php page and call it from your original script. If you get a blank screen, then you have problems with your script. I keep a copy of this around just for this purpose.

<?PHP
session_start();
header("Cache-control: private");
$color = $_SESSION['color'];

echo $color;
?>

If no errors, then it works fine. If blank, you know to look into your code.

Hope it helps,
IamStang

mcibor

2:23 pm on Apr 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Session is valid only for a session, that means only while you're on the server. Before every use of session you need to start it:

session_start()

However I don't see the idea, why do you want to use session to store the color. Wouldn't it be better to store it in the cookie, so the user won't have to chose over and over again different color schemes?

Best regards!
Michal Cibor

PieSocial

7:44 pm on Apr 9, 2005 (gmt 0)

10+ Year Member



It's not meant for user selected color schemes, I want the pages to change color only when I tell them to.

Thank you for the fix.