Page 1 of 1

Setting variables in Sessions

Posted: Tue Jul 26, 2005 7:43 am
by theda
I've read up on sessions a little bit (...Yeah, sadly, I can't find a lot of information on it to benefit me). The only problem is that I haven't the slightest clue how to set a variable like I did using isset() and then being able to use the url blah.com/?variable=yadayadayada and have it actually work with the session.

My example:

I have a website (Gee, something unique every day!). It has the ability to change between seven themes (not including an additional theme that is "Printer Friendly") and also to change between two languages (English and Deutsch). Now, the old way I did it was without Sessions and I used conditionals with isset functions to detect the theme, but they also detected it via Cookies ($theme = |cookie|) or they'd automatically set a cookie (or in the case of languages, redirect to the index page to have the user set their own language).


Now, if I am going to kill off all the cookies (*tear*, i love cookies, they taste good), what do I have to do to implement Sessions to do the exact same thing. I kept screwing up the code with stuff like this:

Code: Select all

if(isset($variable)) {
$variable = $_SESSION['variable'] 
} else {
$variable = "yadayadayada"
}
And yes I opened and closed sessions, and I also tried having the $_SESSON part go on the left side, and the else "yada" = the session variable... But all I kept getting was "yadayadayada" instead of $_session variable, because I haven't a clue how to set variables, no doubt change them via links using sessions.

Posted: Tue Jul 26, 2005 7:49 am
by timvw
use can use isset($_SESSION['var']) to test if a var is set in the session...

i don't really understand what your problem is.. the same way as you accessed $_COOKIES should go for $_SESSION (except you need to call session_start() first)

Posted: Tue Jul 26, 2005 7:51 am
by theda
So I should still be able to do url.com/?var=something and have that session set var to something?

Edit: Going by what you say... I can change:

Code: Select all

// Theme Identity
	if (isset($HTTP_COOKIE_VARS['the']) AND empty($HTTP_GET_VARS['the'])) {
		$the = $HTTP_COOKIE_VARS['the'];
	} elseif (isset($HTTP_GET_VARS['the'])) {
		$the = $HTTP_GET_VARS['the'];
		setcookie('the',$the, time()+60*60*24*30);
		header('P3P: CP="NOI DSP NID STP"');
	} else {
		setcookie('the','see', time()+60*60*24*30);
		header('P3P: CP="NOI DSP NID STP"');
		$the = "see";
	}
Into

Code: Select all

// Theme Identity
session_start()
	if (isset($_SESSION['the']) AND empty($_SESSION['the'])) {
		$the = $_SESSION['the'];
	} elseif (isset($_SESSION['the'])) {
		$the = $_SESSION['the'];
	} else {
		$the = "see";
	}

Posted: Tue Jul 26, 2005 10:19 am
by josh
page1.php

Code: Select all

session_start();
$_SESSION['test']=123;
echo '<a href=\'page2.php\'>Go!</a>';
page2.php

Code: Select all

session_start();
$test=$_SESSION['test'];
// Xss
$test=htmlentities($test);
echo 'The value of $test is '.$test;
That's all you need to know about sessions, you can do anything variable-wise with them since thats what they are, variables.. anything you can do with a normal variable you can do with a session variable.

Edit: that is, everything excluding references

Posted: Tue Jul 26, 2005 2:23 pm
by theda
Not quite what I was asking...

Posted: Tue Jul 26, 2005 11:25 pm
by theda
I was wondering how do I use a link to set a variable while using a session... Because it never seemed to work for me in the past.