0

I were working on drupal 7. In block configuration, under pages i went for an option "Pages on which this PHP code returns TRUE (experts only)"

Added the following code:

<?php
global $user;
$username=$user->name;
if($username=="admin")
{
   return TRUE;
}
else
{
  return FALSE;
}
?>

It's shows me an error message at the top of the screen:

Notice: Undefined property: stdClass::$name in eval() (line 3 of C:\xampp\drupal77\modules\php\php.module(75) : eval()'d code).

I don't know, how to fix it out. Please help me out.Thanks

1
  • I tested the code you show, and it works for me.
    – apaderno
    Oct 27, 2012 at 21:54

2 Answers 2

1

How the $username variable's value will be available there? If you want to use current logged in user, write it like global $user; and check $user->name instead of $username.

0

If I get you right you want to check for the admin user.

A possible code snippet would be:

global $user;
return $user->uid==1;

Of course you can check for any property of the $user object.

3
  • Hi, based upon user login...i needs to show the block (in sense return true or return false). Actual code, <?php global $user; $username=$user->user; checking with database, if count=1, returns true. Again am getting the same error. It's not solved.
    – jacksantho
    Jun 29, 2012 at 6:33
  • The varialble $username is not defined in this context. All about the currently logged in user is in the global $user object.
    – BetaRide
    Jun 29, 2012 at 7:06
  • hi thanks for your posting. See my error message "Undefined property: stdClass::$name" . The error message is not due to $username. please help me out.thanks
    – jacksantho
    Jun 29, 2012 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.