Author Topic: Problem transfering variables using session  (Read 559 times)

0 Members and 1 Guest are viewing this topic.

Offline mnvdkTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Problem transfering variables using session
« on: August 27, 2006, 01:03:40 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

Offline jvalarta

  • Enthusiast
  • Posts: 53
    • View Profile
Re: Problem transfering variables using session
« Reply #1 on: August 27, 2006, 01:07:31 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();

Offline mnvdkTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Problem transfering variables using session
« Reply #2 on: August 27, 2006, 01:18:32 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.

Offline drkstr

  • Enthusiast
  • Posts: 66
  • Gender: Male
    • View Profile
    • Open Base Interactive - Technology for Information Based Strategy
Re: Problem transfering variables using session
« Reply #3 on: August 27, 2006, 01:23:24 PM »
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.
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.php

regards,
...drkstr

Offline jvalarta

  • Enthusiast
  • Posts: 53
    • View Profile
Re: Problem transfering variables using session
« Reply #4 on: August 27, 2006, 01:26:18 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)

Offline drkstr

  • Enthusiast
  • Posts: 66
  • Gender: Male
    • View Profile
    • Open Base Interactive - Technology for Information Based Strategy
Re: Problem transfering variables using session
« Reply #5 on: August 27, 2006, 01:31:32 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

Offline mnvdkTopic starter

  • Irregular
  • Posts: 6
    • View Profile
Re: Problem transfering variables using session
« Reply #6 on: August 27, 2006, 02:11:26 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?
« Last Edit: August 27, 2006, 02:19:32 PM by mnvdk »