php question

Posts: 78
Joined: 01/22/2008

I'm trying to fix some PHP code so that refers the user to the same page he/she is on if he is already logged in. Here's the code:

<?php
global $user;
// Are they logged in?
if ( $user->uid ) {
 
// Yes, take them to the new page
  
drupal_goto ("same page user is on or do nothing";
}
else {
 
// No, take them to the login page
 
drupal_goto("user");
}
?>

Can anybody tell me what to put into line 6 so that it either does nothing or, in effect, causes the goto fucntion to go to the current page (i.e. refresh)

Dan

Posts: 332
Joined: 08/07/2007
Administrator

One solution is to use the PHP suberglobal $_GET. The value of $_GET['q'] contains the Drupal path currently displayed. You could pass that as the argument into drupal_goto.

--

-Shawn Conn: If the Name Don't Rhyme It Ain't Mine

Posts: 78
Joined: 01/22/2008

Thanks.

That works . . . except it puts the page in an infinite loop. Maybe there's a way to stop it?

<?php
global $user;
global
$get;
// Are they logged in?
if ( $user->uid ) {
 
// Yes, take them to the same page they are on
drupal_goto($_GET['q']);
}
else {
 
// No, take them to the login page
 
drupal_goto("user");
}
?>

Posts: 2357
Joined: 08/07/2007
AdministratoreLiTe!

I'm confused about what is supposed to happen. If a logged in user goes to this page, it gets reloaded? Why?

I can understand sending anonymous users to the login page, but maybe you shouldn't do anything for logged in users. If they're on the page they need to be, just let them be there.

<?php
global $user;

if (!
$user->uid) {
 
drupal_goto('user');
}
?>

Posts: 78
Joined: 01/22/2008

I guess I was trying to accommodate a user who was logged in already who might happen to click on the link (not understanding he was already logged in). It's probably very simple, I just don't know PHP.