Jump to content


Photo

Problem transfering variables using session


  • Please log in to reply
6 replies to this topic

#1 mnvdk

mnvdk
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 27 August 2006 - 05:03 PM

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

#2 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 27 August 2006 - 05:07 PM

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();

#3 mnvdk

mnvdk
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 27 August 2006 - 05:18 PM

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.

#4 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 27 August 2006 - 05:23 PM

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.

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/m...n.serialize.php

regards,
...drkstr

#5 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 27 August 2006 - 05:26 PM

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)

#6 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 27 August 2006 - 05:31 PM

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)

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

#7 mnvdk

mnvdk
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 27 August 2006 - 06:11 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users