Jump to content

Archived

This topic is now archived and is closed to further replies.

mnvdk

Problem transfering variables using session

Recommended Posts

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

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.php

regards,
...drkstr

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.