Hi,
I am trying to do something simple - store the $user->name value in a session variable and retrieve that variable for use in another php page's script.
The code I have written instantiates a session and stores the username of whoever logs in to Drupal. Since I developed this code outside of Drupal I used a "session_start()" command and hard-coded a value into the session variable for username 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 fills the variable. I know this because I can echo/print the variable at the top of the page. When run outside of Drupal, other php pages are able to access the session variable and run their scripts correctly. Many of the scripts are AJAX and are working so long as a valid username value is passed. 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. I thought the $user->name pulls the login username and so that is what goes into the session variable(and that is what prints on screen).
below is snippet:
<?php
# PHP CONFIG
session_start();
ini_set("display_errors",1);
ini_set("register_globals",1);
error_reporting(E_ALL);
global $user; //enable in Drupal
$_SESSION['username']=$user->name; //enabled when in Drupal
//$_SESSION['username']="SHOGIKISHI"; //disable when in Drupal
echo "Your username is ".$_SESSION['username']; //Print session variable on screen to confirm - disable in production
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 run outside of 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 a snippet of the other PHP page (test.class.php) retrieving the session variable for use in other PHP scripts:
<?php
if (!isset($_SESSION)) {
// session is not started.
session_start();
}
# include ajaxcore class
require_once("AjaxCore.class.php");
###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 requiring 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?
Comments
I haven't checked all the
I haven't checked all the details of all your code above but I'd do it like this:
In myscript.php (put it in the main Drupal folder where index.php lives):
Strictly speaking you might get away with DRUPAL_BOOTSTRAP_SESSION instead of DRUPAL_BOOTSTRAP_FULL - the former would start the Drupal session and instantiate and populate the global $user object whereas the latter, which would take slightly longer (if you can notice the difference - depends on your server), also loads common.inc etc. and so makes all Drupal built-in functions available to you.
If you really must put myscript.php in a different folder then IIRC you need to make sure you set the $base_url in settings.php since the $base_url is used when determining the session name.
gpk
----
www.alexoria.co.uk
gpk
----
www.alexoria.co.uk
Passing session variables from Drupal to external scripts
I am also suffering from a similar problem. What I actually need is to pass variables on Drupal to and from an external script. In my base Drupal installation directory where index.php is located, I put up a test.php and it simply contains:
But I get nothing as an output. The array $user is never populated. I am using D7 and I've read the contents of index.php, /includes/bootstrap.inc, /includes/common.inc and a few other include files to get additional information but all they have are functions. Surprisingly, only the line
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);causes the empty output as the server dishes out something if I comment it out.The funny thing is that I tried putting in a /includes/test.inc:
and I can verify that the function can indeed be referenced and is reachable.
Any insights on this?
Ah OK silly me, solved it!
I am putting it here as reference for others in the future who may be having the same problem.
We really need to put the correct definition for the base Drupal install:
define('DRUPAL_ROOT', getcwd());In the case above,
getcwd()points to my actual base Drupal install because my test.php sits in the same directory as index.php, but if your script is located elsewhere, say for instance at/includes, you would need to define it as something like:Gadzooks! Nearly 30 years of programming experience and I still make newbie mistakes!
this is just what I needed
this is just what I needed for a clever integration between our new drupal based intranet and some old asp pages that I don't want to rewrite just yet