Jump to content

Problem transfering variables using session


mnvdk

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.