By jsethi on
How do I check if a user is logged in or not using php
Looking for an If/Else statement so I can display one link if a user is logged in and another if they arent.
Thanks so much,
Jay
How do I check if a user is logged in or not using php
Looking for an If/Else statement so I can display one link if a user is logged in and another if they arent.
Thanks so much,
Jay
Comments
Solved
Oh figured it out!
thank you for posting
thank you for posting this... found it 1+ year after you posted.
Thanks for this!
Real simple!
Cheers!
Thanks for this! Simple
Thanks for this! Simple solution to someone starting out seeing how PHP is integrated with Drupal.
I would STRONGLY advise
I would STRONGLY advise against the global $user.
Prefer rather $GLOBALS['user'] instead, it is much more secure.
You might want to throw up an
You might want to throw up an example snippet.
<?php if
This way, only authenticated users can access whatever you want them to with a secure variable.
Imagine some developer wants to alter your own code (where you already set $user).
The dude is going to add his own code below and, like you already did, he is going to need the $user object and (like you did!) call the bastard $user. And BOOM, the man got access to all of the previous $user rights and permissions...
$GLOBALS - "much more secure"? Why?
Why would
$GLOBALS['user']bethan using
global $user?global $user vs $GLOBALS['user']
By definition, using "global $user" is equivalent to $GLOBALS['user'] since they both point to the same memory space. Updating one will update the other at the same time. See http://php.net/manual/en/reserved.variables.globals.php
If I had to recommend one over the other, I would highly recommend "global $user" as it has better readability. Besides that, PHP 5.4 is making changes to how the $GLOBALS[] array behaves and I suspect this is just the start of such changes. I would not be surprised to see the array deprecated at some point.
The simplest way is to use user_is_logged_in() function!
The simplest way is to use
user_is_logged_in()function!Use it like so
The call is here
https://api.drupal.org/api/drupal/modules%21user%21user.module/function/...