Jump to content

$_SESSION just once?


knowNothing

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/
Share on other sites

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:
[code=php:0]extract($_SESSION);[/code]
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.
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88543
Share on other sites

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:
[code]
<a href="<? echo $PHP_SELF."?delete=1&idnum=".$_SESSION['idnum']; ?>">delete</a>
[/code]



it seems that the values for $delete and $idnum are not being fed back into $PHP_SELF
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88544
Share on other sites

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


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 [url=http://uk2.php.net/manual/en/language.variables.predefined.php]here[/url]
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88546
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88558
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88572
Share on other sites

Awsome.  It worked.  I just used:

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

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.
Link to comment
https://forums.phpfreaks.com/topic/20140-_session-just-once/#findComment-88575
Share on other sites

Archived

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

×
×
  • 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.