View Full Version : Store (and then retrieve) a variable in Drupal/PHP page

05-14-2008, 04:07 AM

I am trying to do something simple - store a value in a session variable and retrieve that variable value for use in another php page's script.

The code I have written instantiates a session and stores the user name of whoever logs in to Drupal. Since I developed this code outside of Drupal I have simply used a "session_start()" command and hard-coded a value into the session variable since there is no $user->name to access without being in Drupal. As it is right now, the index.php page starts a session, stores a value into the $_SESSION variable, and I confirm it fills the variable since I can echo/print the variable at the top of the page. Other php pages are able to access the session variable and run their scripts correctly. The thing is, when I drop index.php into Drupal, the $_SESSION variable does not pass to other PHP pages. The only page where it seems to be working is the index.php page in which it was instantiated.

What can I do to 1) capture and store the user's login name and 2) retrieve the user's login name for use in other PHP scripts?

The first PHP page (index.php) takes a user's name and stores it in session variable. Right now, the session variable is hard coded to be username=SHOGIKISHI. In Drupal, $user->name pulls the login username and so that is what goes into the session variable(evidenced by what prints on screen).

below is snippet:


//global $user; //enable in Drupal
//$_SESSION['username']=$user->name; //enabled when in Drupal
$_SESSION['username']="SHOGIKISHI"; //disable when in Drupal

in a code-behind PHP page, the username is needed to run SQL queries. When "session_start()" is used and the username is hard-coded, the value is stored in session variable and the variable is retrievable in all php pages, but only if these pages are not dropped in Drupal. When the pages are dropped into Drupal, the variable doesn't pass even if the value is hard coded. It's as if the variable is empty to all PHP pages except the page in which it is instantiated.

below is snippet of another PHP page (test.class.php) retrieving the session variable for use in other PHP scripts:

if (!isset($_SESSION)) {
// session is not started.
# include ajaxcore class

###other code

# Pass variable $user->name to code-behind page
$UserName = $_SESSION['username'];

What can be done to capture login user's name and pull that value in other PHP pages? I'm not partial to the $_SESSION approach, but it seemed like the right way to do it. I'm just not able to do it right. I'm thinking there is a conflict with the "session_start()" command in my code and the fact that there is already a session running as part of Drupal? Help please?

05-14-2008, 03:45 PM
First, I'm not familiar with drupal, but I assume its some kind of content management system.
session_start() needs to be called on any page that the session is being used on. Don't think of it as a constructor, it is a function call and merely tells the system to consume the resource for session use. The trickier part will be to ensure that session_starts() are called before any flushing to the browser, or simply use output buffering to control your pages.
Same would go if you want to pass the value along to the cms, you would need to make sure a session start is passed through there.
If drupal has its own session handling (generally people put this is its own script, like session.php, or global.php for example), you can just include this session logic into your own code and share it across the drupal system as well. Thats if they handle their own.
And as a final note:

ini_set('register_globals', 1);

is bad, and will be removed in PHP version 6. Don't worry though, this isn't affected by the 'global' keyword, this affects whether get/post/cookie (whatever your gpc ordering is) is registered into the memory as their own variables: ie: $_GET['userid'] = $userid, where it automatically extracts the $_GET['userid'] into the $userid.

Hope that helps you out!

05-14-2008, 07:15 PM
Thanks for the insight Fou-Lu, what you said about session_start makes sense and has got me thinking about Drupal session management. I have also disabled the "register globals" code.

The issue I described still persists. I have been looking online at possible fixes, such as "session_write_close()", "session_register()", etc., but I haven't had any successes with those, but I may not be using them correctly anyway. I'm going to be looking at Drupal session management, but I could be going down the wrong path.

05-15-2008, 01:36 AM
Good plan mate, I'd recommend looking into how drupal handles it. If it were my program and I was using a custom session handling mechanism, I assume you that I would destroy the existance of _SESSION. It is possible that drupal is doing the same thing and that could explain why you can never retrieve sessions from drupal. Oh, and I assumed that you are running this on the same site as well, sessions will not be accessable from outside of your site easily (not impossible, just not pleasant).
Good luck mate, I'm sure you'll find what you are looking to do!