tags:

views:

19

answers:

1

I was trying to display a link in a page which will point to previous page the user has visited in drupal . Previously i was using sessions

echo $_SESSION['back']
$_SESSION['back']=htmlentities($_SERVER['REQUEST_URI']);

This worked fine, but i was told to use variable_get and set in drupal and not to use sessions So i did this

global $prev_global;
$prev_global=variable_get($prev_page,$default='http://mysite.local');
variable_set($prev_page,htmlentities($_SERVER['REQUEST_URI']));
. . .
echo "PREV:".$prev_global;

But this always points to the current page being viewed , what went wrong here ?

+3  A: 

I don't know who told you to use variable_get() and variable_set(), but consider never listening to them again. variable_get() and variable_set() act on global variables, not user-based variables.

You had it right the first time. Use $_SESSION: that's what it's there for.

Mark Trapp
+1 - short lived, user specific information is what `$_SESSION` is intended to hold.
Henrik Opel
hmmm ok . but i dont think i have a say in this :(
kantu
@kantu: if someone is forcing you to use `variable_get()`/`variable_set()` for session variables, I'd be really concerned. It demonstrates a fundamental lack of understanding of both PHP and the Drupal API.
Mark Trapp
I am new to drupal i was told like " not to mess with session variables and to use variable_get _set that s what they are for !" lol
kantu
@kantu: there's a simple test case to show `variable_set()` is wrong: the variable will be the same for all users, not just the user that visited the page. Alternatively, set the variable for user 1, have user 2 visit a new page, and note that the variable for user 1 changed to the value user 2 set. As for not wanting to mess with `$_SESSION`: I don't know why that would be. It's just an associative array: it won't bite.
Mark Trapp