Setting variables in Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Setting variables in Sessions

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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";
	}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Not quite what I was asking...
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post 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.
Post Reply