Jump to content


Photo

$_SESSION just once?


  • Please log in to reply
7 replies to this topic

#1 knowNothing

knowNothing
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 08 September 2006 - 05:02 PM

Since session_register() is deprecated and I don't have access to turn register_globals on, and don't really want to anyway...

If I declare a variable using $_SESSION, will I have to eternally refer to it as $_SESSION["variable_here"] wherever I use it in the code or does using $_SESSION once, in effect, make it permanently global, and I can use $variable_here throughout the code?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 September 2006 - 05:27 PM

I would not recommend you to do that, as it is basically lazy programming. Yes when ever you want to access a session var you will need to use $_SESSION['var_name']. Why do you want to use $var_name to access your session variables?

What you could do is extract the session variables from the session array using extract function, eg:
extract($_SESSION);
Then you can use $var_name to access your var_name session. However this to me is lazy programming.

However you'll still need to use $_SESSION['var_name'] to create the var_name session and to change the value of the session. You can only use extract when you want to retrieve the session vars.

#3 knowNothing

knowNothing
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 08 September 2006 - 05:29 PM

Maybe I don't even need this... here's my problem..

I'm making a news admin script and I'm trying to pass variables back into $PHP_SELF  so that I can delete and edit news posts, but when I  use something like:
<a href="<? echo $PHP_SELF."?delete=1&idnum=".$_SESSION['idnum']; ?>">delete</a>



it seems that the values for $delete and $idnum are not being fed back into $PHP_SELF

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 September 2006 - 05:31 PM

Prehaps use this:
echo '<a href="' . $_SERVER['PHP_SELF'] . '?delete=1&idnum=' . $_SESSION['idnum'] . '">delete</a>';


To get the delete and idnum variables fromo the URL you use $_GET['delete'] and $_GET['idnum'] instead of $delete and $idnum.

You might want to look into the superglobal arrays over at php.net. You use the supergloabl arrays when register_global is off (and when its on).
The superglobals are _POST, _GET, _COOKIE, _SESSION, _SERVER etc. You can learn more about supergloabls here

#5 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 08 September 2006 - 05:49 PM

I would always recommend reffering to $_SESSION['var_name'] becuase this way you know that your always getting your data, 'straight from the hourses mouth' and it hasn't had the chance to be accidentally overwritten.  Since $_SESSION is such a unique syntax, it's not likely that you would overwrite a $_SESSION, and clients can't assign values to $_SESSION through global namespace.
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#6 knowNothing

knowNothing
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 08 September 2006 - 05:50 PM

cool.. ok in this case, if i pass the variable back to $_SERVER['DOCUMENT_ROOT'], i can simply reference it further using $_GET['delete'] without having to decare it as a session variable?

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 September 2006 - 06:07 PM

That is correct, but it'll only work if your link is like this:
mysite.com/page.php?delete=1

Then you access the delete variable using $_GET['delete']

However if you want to use the get variable in another page, that doesnt send the delete variable through the url you'll need to use a session or a cookie to store the delete variable.

#8 knowNothing

knowNothing
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 08 September 2006 - 06:12 PM

Awsome.  It worked.  I just used:

<a href="<?php echo $_SERVER['PHP_SELF'] . '?delete=1&idnum=' . $idnum; ?>">delete</a>

and then once the variable was passed back into the document, I referenced it using $_GET['idnum'] and $_GET['delete'] and the function performed properly (removed the correct news entry).  I have no need to use them in another document, so I wont need to use $_SESSION.

Thanks for all your help guys.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users