Accessing User Information

korbyn - November 9, 2009 - 18:01

Newbie question here, I'm sure that I'm just overlooking something simple.

I'm trying to add some code to a module to determine if a user is logged in or not. From what I've read here, this should work:

<?php
  
global $user ;
   if (
$user->uid) {
     
// {insert code here}
  
}
?>

Every time I try to run the module though, $user is not getting populated and the if statement does not get triggered. I've tried clearing my cache and still no dice. Moved the code to one of my theme files and the if statement will execute.

Did more reading and thought that perhaps I needed to load the bootstrap include file first, so I added code to do that and it's still not working.

Are there any other files that I should be including but just don't know about yet since I'm just starting out, or am I totally going about this all wrong?

Not sure why thats not

rschwab - November 9, 2009 - 18:10

Not sure why thats not working, but you could try the alternate:

<?php

if (user_is_logged_in()) {
// do stuff for logged in users
}
else
{
// do something different for anonymous users
}
?>

- Ryan

Some other things I've tried

korbyn - November 9, 2009 - 18:41

Tried that too and the if doesn't get triggered.

I also tried the following and did not have any luck either:

<?php
  
if ( user_access("view uploaded files") ) {
     
// do stuff
  
}

   if (
$GLOBALS['user']->uid ) {
     
// do stuff
  
}
?>

Some context for your code

nevets - November 9, 2009 - 19:01

Some context for your code would help. What function does the code sit in, under what conditions do you expect the function to be called?

RE: Some context for your code

korbyn - November 9, 2009 - 19:23

No problem. What I'm trying to do is secure the file upload capability of the FCKeditor (v 2.6.5). I've installed it two ways (just in case the problem has to do with where the files reside), using the FCKeditor module (v. 6.x-1.4) and with the WYSIWYG module (6.x-2.0). With the FCKeditor module all the editor files are within sites/all/modules and with the WYSIWYG module, the FCKeditor editor settings are in the sites/all/libraries. Either way, I'm not able to access the user variable information.

To enable the file upload capability, I went into the FCKeditor editor files and modified the following line in:
fckeditor/editor/filemanager/connectors/php/config.php

<?php
  
// SECURITY: You must explicitly enable this "connector". (Set it to "true").
   // WARNING: don't just set "$Config['Enabled'] = true ;", you must be sure that only
   //     authenticated users can access this file or use some kind of session checking.
  
$Config['Enabled'] = false ;
?>

I've set the value to true to turn it on. What I'm attempting to do now is follow the advice in the WARNING statement. I'm placing the code that I'm trying to insert right after this line of code (line 30 in the original file I downloaded), so what I have is:

<?php
  
// SECURITY: You must explicitly enable this "connector". (Set it to "true").
   // WARNING: don't just set "$Config['Enabled'] = true ;", you must be sure that only
   //     authenticated users can access this file or use some kind of session checking.
  
$Config['Enabled'] = false ;

   global
$user;
   if (
$user->uid ) {
     
$Config['Enabled'] = true;
   }
?>

I'm guessing that it must be more difficult to secure this functionality than simply doing the above or it would have already been added into the module's code instead of the warning message, but I'm new at this and am hoping that it's possible lock this down to only authenticated users.

I do not have a copy of the

nevets - November 9, 2009 - 19:46

I do not have a copy of the file but I would guess it does not do any Drupal bootstrapping which would be needed for $user to be available.

RE: I do not have a copy of the

korbyn - November 9, 2009 - 21:20

I thought that it might need the bootstrapping so I found some code to find it and then load it like this:

<?php
   $drupal_path
= "../../../../../../../../..";
  
$cwd = getcwd();
  
chdir($drupal_path);
   require_once
"./includes/bootstrap.inc";
  
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  
chdir($cwd);
?>

I'm able to call the functions found in bootstrap.inc without any errors, but the user info still does not populate the variable.

The same issue

thensel - November 12, 2009 - 16:52

I wrote a short script to show an image. It should make some user-checks. So basically the same:
bootstrap and then use the global $user structure.
Seems since i upgraded to 6.14 the $user->uid is allways 0

 
 

Drupal is a registered trademark of Dries Buytaert.