By amanshakya on
How to I define and use session variables in drupal so that I can store temporary information while browsing through pages? I would be very thankful for any help.
How to I define and use session variables in drupal so that I can store temporary information while browsing through pages? I would be very thankful for any help.
Comments
$_SESSION['xx'] = value;
Sessions are started in session.inc which is loaded by boostrap.inc which is almost the first thing done in index.php. You should be able to access session variables everywhere in your code using the $_SESSION array. If you are building theme xyz then set up the following code.
When you want to store variable z from page to page you can use the following code.
On the next page you can access $y using the following code.
Are you experienced at using & to create references?
http://petermoulding.com/technology/content_management_systems/drupal/
petermoulding.com/web_architect
Session Variables!
Thanks to peterx for describing this function! It was exactly what I needed, and only took 5 pages of search to find it! A good one for the handbook.
Drupal session variables must be arrays!
$_SESSION['xyz'] = array();Thx peterx. Note the addition of an array to the session global. You cannot use scalars, as they won't be recognized (or stored) by Drupal, and you'll scratch your head wondering why your session variable isn't getting updated. E.g. this won't work:
$_SESSION['xyz'] = 'fred';Didn't work for me - please help
I am working currently on a program that requires external authentication. I can't get it to work, though I applied your instructions.
This is what I've done so far:
(Error messages works well)
Thanks!
Where do you use that code?
You code includes redirects, redirects work before cookies are set, and sessions require cookies. Your code might run before Drupal starts sessions.
If you want to retain data from before sessions are started, consider using a table in the database in the database.
petermoulding.com/web_architect
petermoulding.com/web_architect
Strings do work (on Drupal 6
Setting $_SESSION to strings does work (on Drupal 6 at least)....
Do it like this
In fact you can you can do it like this:
$_SESSION['xyz'] = array();
$_SESSION['xyz']['#value'] = 'fred';
$person_name = $_SESSION['xyz']['#value'];
Works for me.
Sasha
Print & Screen
Watch out in which hook!
Hello,
You should also take care of which hook you are using your session variable. Some hooks will just be cached and not executed each time if you enable caching on your site.
I wrote an article about which hook to use here :
http://www.ravelsoft.com/blog/2010/session-variable-drupal
Good luck with your issue.
Félix Delval
Unable to access session variables in other functions
@felix, I'm creating a module and had issues with the $form_state rebuilding the form everytime I set the storage. While researching for a solution I stumbled upon this post and followed it to the dot. I have a mymodule_boot where the session variables are initialised
I set my variables in the hook_submit
When I access the session variable
I get an empty string. Is there something that I'm missing. Thanks in advance
Acer Aspire 5745
[i5 430M, 3GB, 320GB]
Ubuntu 12.04 (Precise Pangolin)
Drupal 6.15, 7.x
DigitalOcean, Go Daddy, Rackspace,
Exact same problem
i am having the same problem (http://drupal.org/node/1197892). No answer either. Could it be a PHP setting? Or are we just missing something painfully obvious?
variable_set/get to session_set/get
Add this code and use sessions like you use variable_set and variable_get
nice code Mar, where would
nice code Mar, where would you suggest adding? Can it be put in a module or do I have to hack core?
Change session handling completely
It is possible to change session handling to let you use and set session variables before redirects. I did it for as few sites. Not clean but it works. Let me know if anyone wants to mod their site.
petermoulding.com/web_architect
Still the case?
Can i just ask if is still the case in Drupal 7 that you have to use arrays ?
Thanks
I would guess that is still the case
I would guess that is still the case, since
$_SESSIONis a PHP artifact, not something from Drupal.Even if you don't have to use arrays, it would still be a good idea to store everything in a custom array to prevent name collisions:
--
Read more at iRolo.net
session breaks login
Hi All,
I have created a session without problems it works as I expect but it prevents me from logging in properly.
No overlay appearing and when I go to /admin
Access Denied
You are not authorized to access this page.
In the sessions table I get one entry with uid 0.
It is as if, allowing me an anonymous session but if I login it wont update the session once I am authenticated....preventing access to backend. not sure why D7 would do this????
I had to destroy the anonymous session but surely this is not right?
BTW, I am just using Bartik theme just to test this.
Any help greatly appreciated!
Thanks
Colin
New topic
Colin,
You may want to do a new top-level forum post about this. And, please include examples of custom code you have written.
--
Read more at iRolo.net