mnvdk Posted August 27, 2006 Share Posted August 27, 2006 Hi all...I'm fairly new to PHP, and for some time now, I've had a problem deciding how to transfer variables between pages, and finally i settled for using session variables for this... I'm not sure if it's the best way to do it, for the purposes I have, and if you got some better way please advice me. But, anyway, this is how I've tried to do it:in globals.php, I have declared this function:function ses_var($var,$def) { if (!isset($_SESSION[$var])) { $_SESSION[$var] = $def; } $var = $_SESSION[$var]; return $var;}in variables.php i use it:ses_var('style','def');and in index.php i have this: <style type="text/css" media="screen"> <?php include("style/".$style.".css") ?> </style>both globals.php and variables.php are, of course, included first in index.php.My problem is that the variable $style is empty, or undeclared, so it keeps looking for style/.css, instead of style/def.css, as it should... What's my problem? How should I do this?Thanks-Matt Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/ Share on other sites More sharing options...
jvalarta Posted August 27, 2006 Share Posted August 27, 2006 Yes, using sessions is a good way to do this. The biggest error made here is not to declare the session. you should have this at the top of every page you wish to use session variables within:session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81162 Share on other sites More sharing options...
mnvdk Posted August 27, 2006 Author Share Posted August 27, 2006 Yeah, I know, I already did this, but it still doesn't work...I shouldn't need to register session or anything like that should I?Well, thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81168 Share on other sites More sharing options...
drkstr Posted August 27, 2006 Share Posted August 27, 2006 [quote]Yeah, I know, I already did this, but it still doesn't work...I shouldn't need to register session or anything like that should I?Well, thanks anyway.[/quote]You need to start it on every page or tell your php.ini to do it automaticly.Ass far as the undefined thing goes, what data type is the variable your storing? If it is a data structure, you will need to serialize it first, then unserialize when you're ready to retrieve the data.http://us3.php.net/manual/en/function.serialize.phpregards,...drkstr Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81170 Share on other sites More sharing options...
jvalarta Posted August 27, 2006 Share Posted August 27, 2006 You do need to tell PHP which variables are to be stored in the session, two ways to do this:session_register('var');or$_SESSION['var'](I believe session_register is the old method in which to do this) Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81172 Share on other sites More sharing options...
drkstr Posted August 27, 2006 Share Posted August 27, 2006 [quote author=jvalarta link=topic=105833.msg422861#msg422861 date=1156699578]You do need to tell PHP which variables are to be stored in the session, two ways to do this:session_register('var');or$_SESSION['var'](I believe session_register is the old method in which to do this)[/quote] I've always just assigned them without any registration, $_SESSION['var']="this is a string";Is this not a preferred method? I'm only asking because I am also new to PHP and don't want to get into any bad habits from the beginning.Thanks!...drkstr Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81175 Share on other sites More sharing options...
mnvdk Posted August 27, 2006 Author Share Posted August 27, 2006 Ah! Stupid me!The problem was in the function ses_var(), while I thought I was declaring: $style = $_SESSION['style']; i was actually declaring style = $_SESSION['style']; without the $-sign, because the ses_var function was declared:function ses_var($var,$def) { if (!isset($_SESSION[$var])) { $_SESSION[$var] = $def; } $var = $_SESSION[$var]; return $var;}So my problem is now, how do I declare a variable called $style through the ses_var() func. In other words, how do I write:$$var = $_SESSION['$var']; <-- when this doesn't work?EDIT: I can of course bypass this by using $_SESSION['var'] instead of $var in the rest of my script/page, but is this recommendable? Quote Link to comment https://forums.phpfreaks.com/topic/18817-problem-transfering-variables-using-session/#findComment-81189 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.