Hello

I installed a Drupal website on http://www.mydomain.com and all is working fine. I can create users, they have roles...

I have developed a php script on http://www.mydomain.com/myscript/index.php and i want to allow authentified Drupal users to this script (only them !).

I found a drupal variable : $GLOBALS['user'] and all i need is to access to this variable on my script.

So, how to access to $GLOBALS['user'] on my personal script ? Any id ? cookie ? session ?
All i need is an example because i tried a lot of things but nothing is working...

Thank you. (and sorry for my English)

Jacques.

Comments

davemybes’s picture

WIthin a Drupal page, its usually its global $user; that gets used and then you do a check to see if $user->uid > 0. So I guess you can do something like this with the $GLOBALS variable:

  $user_ob = $GLOBALS['user'];
  if ($user_obj->uid > 0) {
    ....do your stuff here for authenticated users....
  }
  else {
    ....do your stuff here for anonymous users....
  }

That should work IMO.

______________________________________________________________________________________________________

skar’s picture

Humm, it doesn't work because my script is a web page. I mean, i'm trying to access to $GLOBALS['user'] on the page http://www.mydomain.com/myscript/index.php without include index.php on a drupal page, node, bock...

My problem is that $GLOBALS is available for the current webpage. My script is on a subfolder of my drupal website. So the "scope" is not the same.

I think that $_SESSION can be the answer but actually all my tests failed :/

I can give an example...
Imagine that you have your drupal website on http://www.mydomain.com and an old phpbb forum on http://www.mydomain.com/myPhpBBforum/

You want to modify your forum's index.php to show something like : "hello $GLOBALS['user']->name".
So to do this, you need to access to the var $GLOBALS['user'] on http://www.mydomain.com/forum/index.php.

How to do that ? I am looking for something secure (no arguments on the link).
I will try again with sessions...

[edit] thank :)

davemybes’s picture

Ah, I see what you mean about "scope". OK, so how about this: does your script need to be in its own file? Can it be embedded into a Drupal page, or put into a Drupal module? Either of those two options will give you access to $user.

______________________________________________________________________________________________________

skar’s picture

Thank for your answer.
For each pages on my script (.../myscript/index.php, .../myscript/dosomething.php ....) i will create a drupal page.

In this drupal page, i will put my php includes and to communicate between drupal pages, i will use a custom $_SESSION variable.

It seems to work, in my $_SESSION variable, i can serialize my custom class (from my script) and take it in my second drupal page.

something like :

Drupal page1

include('./myscript/class/class1.php');
$var = new class1('something1', 1, 2, 3);
$_SESSION['custom1'] = serialize($var);   //need to check if the session already exist...

Drupal page2

include('./myscript/class/class1.php');
$var = unserialize($_SESSION['custom1']);   //need to check if the session already exist...
echo $var->do_Something();      //its the same as the class object on the Drupal page1

so i can display the content to users who have the role (because i can access to $GLOBALS on drupal pages)

and i can use my custom script and do what i want with my classes...

I think it's not the better way to do that but it's working and it seems to be secure (i mean no $GET, $POST...)

Thank you !