You are not providing sufficient information to help you. Is this code located in callbacks, hooks? If this code is not wrapped in a function, you are doing it wrong.
<?php
/**
* Store and retrieve variables from session
*
* @param $key
* Key to be set or got
* @param $value
* Value to set for $key
*
* @return
* If only $key is given, sought-after value for $key
*/
function _mymodule_session($key, $value = null) {
if (isset($value)) {
unset($_SESSION[$key]);
$_SESSION[$key] = $value;
}
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
}
?>
OK I think you need to use a combination of two ideas
1) Static variable/func to store the value FOR THIS REQUEST
2) Sessions variable/cookie to store the value for FUTURE REQUESTS (pressing f5)
So here is a function that will do it
I just wrote this off the top of my head so it might need work
<?php
function lists_session($key, $value = NULL) {
static $storage;
if ($value) {
$storage[$key] = $value ;
$_SESSION['lists'][$key] = $value ; // I put 'lists' in case some other module uses 'type' in $_SESSION
}
else if (empty($storage[$key]) && isset($_SESSION['lists'][$key])) {
$storage[$key] = $_SESSION['lists'][$key];
}
return $storage[$key];
}
?>
Now, to store the type value do this: lists_session("type", "cat1");
To retrieve the type value do this: echo lists_session("type");
I am experimenting with sessions in Drupal, and following what I have seen above, I am stuck!
I have implemented the module as above setting :
function mysession_session($key, $value = null) {
if (isset($value)) {
if (isset($_SESSION[$key])){
unset($_SESSION[$key]);
}
$_SESSION[$key] = $value;
}
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
}
Basically it set the sessions and return values.
My problem is, when I call the function or directly setting a session, such as $_SESSION[$key] = $value, I am no longer able to login successfully.
For some reason, the session I have started in my code breaks the login authentication. I have added drupal_session_start(); but maybe I need to put this right at the top of the page such as html.tpl?
I start the sessions in page--front.tpl and it reads in page.tpl.
I just wanted to incorporate sessions but can not understand why this is so complicated?
Would appreciate if some1 can point me in the right direction
As soon as I arrive on the page, the session table is populated with my session using uid 0 but as soon as I try to login, I am unable to unless I delete the session or remove my own sessions altogether.
I have come across drupal_session_regenerate in the core code, surely this should update uid to the logged in user id but all I am getting is authentication problems and can not get to the backend?
I have resorted to using a the standard themes, bartik, and I am still coming with this problem.
Comments
You can use the $_SESSION
You can use the $_SESSION superglobal. See PHP.net.
Also, please read Tips for posting and Tips to get help with your code.
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.
More
I have two custom modules named "lists" and "list_details"
I have written code
in first module
and
in second module
But Nothing is displaying ..Please help me
You are not providing
You are not providing sufficient information to help you. Is this code located in callbacks, hooks? If this code is not wrapped in a function, you are doing it wrong.
There's no need to call session_start, Drupal does so during the bootstrap. If sessions do not seem to work for anonymous users, make sure the users table contains an entry for uid 0.
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.
Thanks a lot
yes . It is working now
I have written session_start(); which created problems
Thank you Heine for your support
Here's a function from one
Here's a function from one of my modules.
--
Jakob Persson
NodeOne - www.nodeone.se
--
Jakob Persson - blog
Leancept – Digital effect and innovation agency
but I must press F5 to reload
but I must press F5 to reload the page if I want to get a session??? what 's the problem? can you help me?
OK I think you need to use a
OK I think you need to use a combination of two ideas
1) Static variable/func to store the value FOR THIS REQUEST
2) Sessions variable/cookie to store the value for FUTURE REQUESTS (pressing f5)
So here is a function that will do it
I just wrote this off the top of my head so it might need work
Now, to store the type value do this: lists_session("type", "cat1");
To retrieve the type value do this: echo lists_session("type");
(don't use session_start)
thanks for this!
i used this code to enable assigning imported feeds to a specific user and it works!
thanks alot for this!
Consider using PURL instead
Consider using PURL instead of sessions as it works even when using caching.
--
Jakob Persson - blog
Leancept – Digital effect and innovation agency
it's drupal_session_start()
sessions breaks my login
Hi All,
I am experimenting with sessions in Drupal, and following what I have seen above, I am stuck!
I have implemented the module as above setting :
function mysession_session($key, $value = null) {
if (isset($value)) {
if (isset($_SESSION[$key])){
unset($_SESSION[$key]);
}
$_SESSION[$key] = $value;
}
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
}
Basically it set the sessions and return values.
My problem is, when I call the function or directly setting a session, such as $_SESSION[$key] = $value, I am no longer able to login successfully.
For some reason, the session I have started in my code breaks the login authentication. I have added drupal_session_start(); but maybe I need to put this right at the top of the page such as html.tpl?
I start the sessions in page--front.tpl and it reads in page.tpl.
I just wanted to incorporate sessions but can not understand why this is so complicated?
Would appreciate if some1 can point me in the right direction
Thx!
sessions breaks my login
Just to add,
I am using the bartik theme
And just added the session code like so:
page--front.tpl example
When I call the function, it breaks the login. Not sure how i can implement sessions?
Pls advise.
Ta
Hi,
Hi,
You just need to set and unset your sessions variable.
ie
outputs: fred
Drupal looks after all the session start and end stuff. see session.inc
Sessions and login problems
Hi Darren,
Thanks for the advice but this still does not help with the login problem. I am using Drupal 7.
I have set the session like so in my theme:
$_SESSION['xyz'] = array();
$_SESSION['xyz']["filter1"] = 0;
As explained here:
https://www.drupal.org/node/33513
As soon as I arrive on the page, the session table is populated with my session using uid 0 but as soon as I try to login, I am unable to unless I delete the session or remove my own sessions altogether.
I have come across drupal_session_regenerate in the core code, surely this should update uid to the logged in user id but all I am getting is authentication problems and can not get to the backend?
I have resorted to using a the standard themes, bartik, and I am still coming with this problem.
Any ideas why?
Thanks
Session breaks login
Hi Again,
It does not work. Setting variables is fine but login fails.
For example