How can we use session in drupal module ... please give me advice ...

Comments

heine’s picture

comterkumar’s picture

I have two custom modules named "lists" and "list_details"

I have written code

session_start();
$_SESSION['type'] = "cat1";

in first module

and

session_start();
echo $_SESSION['type'];

in second module

But Nothing is displaying ..Please help me

heine’s picture

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.

comterkumar’s picture

yes . It is working now

I have written session_start(); which created problems

Thank you Heine for your support

solipsist’s picture

Here's a function from one of my modules.

<?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];
  }
}
?>

--
Jakob Persson
NodeOne - www.nodeone.se

--
Jakob Persson - blog
Leancept – Digital effect and innovation agency

hanzhong520’s picture

but I must press F5 to reload the page if I want to get a session??? what 's the problem? can you help me?

danielb’s picture

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");

(don't use session_start)

achilles085’s picture

i used this code to enable assigning imported feeds to a specific user and it works!

thanks alot for this!

solipsist’s picture

Consider using PURL instead of sessions as it works even when using caching.

--
Jakob Persson - blog
Leancept – Digital effect and innovation agency

norman.lol’s picture

drupal_session_start();
$_SESSION['type'] = "cat1";
coliniluvyou’s picture

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!

coliniluvyou’s picture

Just to add,

I am using the bartik theme

And just added the session code like so:

page--front.tpl example

echo mysession_session("type","value");

When I call the function, it breaks the login. Not sure how i can implement sessions?

Pls advise.
Ta

darrenwh’s picture

Hi,
You just need to set and unset your sessions variable.

ie


$_SESSION['test'] = new stdClass();
$_SESSION['test']->cat = 'fred';
$_SESSION['test']->dog = 'tom';

echo $_SESSION['test']->cat;

outputs: fred

Drupal looks after all the session start and end stuff. see session.inc

coliniluvyou’s picture

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

coliniluvyou’s picture

Hi Again,

It does not work. Setting variables is fine but login fails.

For example