I have the following code in a drupal node (page)

<php?
echo '<h3>Welcome , '.$user->name.'!</h3>';
echo '<a href="'.$base_url.'/user/'.$user->uid.'">'.$user->uid.' profile</a> |';
echo '<a href="'.$base_url.'/user/'.$user->uid.'/edit/Personal+Info"><font size="1">edit</font></a><hr />';
?>

The page has more code after this, but i realized that whenever I request the $user->uid or the $user->name variable, the node doesn't even load if the user is anyone but the administrator.

I thought it might be the $user->name bit, but even the $user->uid doesn't work. Only the title shows and no other node element is rendered, and no error or warning is shown (i have error display ON in the settings).

Any ideas? Or maybe I'm missing something I should know.

Comments

lemonidas’s picture

I forgot to mention, that i have

global $base_url;
global $user;

above this code snippet

and that this happens if I try to echo the $user->uid or name, (because when I use $user->uid in queries they work no matter who the user is)

lemonidas’s picture

lol

No I have '<?php' (but maybe that's why the code highlighting didn't work and i used the < code > tags :)

pobster’s picture

Well I can tell you now that it doesn't break anything as I use it myself.

It would have been more helpful if you'd posted the actual code, but meh anyways - here's what I noticed from what you wrote;

You wrote <php? rather than <?php, also echo doesn't tend to work very well (don't know why) I'd stick with print - plus, I can't remember if the rest of $user is null, but certainly $user->uid is when the user is anonymous, why not try something more like this;

  global $base_url, $user;
  if ($user->uid) {
    print '<h3>Welcome , '.$user->name.'!</h3>';
    print '<a href="'.$base_url.'/user/'.$user->uid.'">'.$user->uid.' profile</a> |';
    print '<a href="'.$base_url.'/user/'.$user->uid.'/edit/Personal+Info"><font size="1">edit</font></a><hr />';
  }
  else {
    print '<h3>Welcome to the site</h3>';
  }

Pobster

--------------------------------------------
http://www.justgiving.com/paulmaddern
--------------------------------------------

lemonidas’s picture

Thanks for your reply.

I check for $user->uid before this code block (the page is a members only page).
I think that this block causes the thing to break, because when I comment it out, the rest of the page works for all users.

I'll try with print (but it's strange since I always use echo and the only page to cause problems is this one..)

Leonidas

pobster’s picture

Yeah I've noticed it caused me a problem before, yet I've no idea why? Anyways, I thought I'd post my own snippet to show how I do it;

global $user;
if ($user->uid) {
  profile_load_profile($user);
  $DOB = $user->profile_DOB{month}.$user->profile_DOB{day};
  $num = date(Y)-$user->profile_DOB{year};
  if (($DOB == date("md")) and ($num > 5)) {
    print "Happy ";
       if ($num % 100 >= 11 and $num % 100 <= 13)
               print $num."th";
       elseif ( $num % 10 == 1 )
               print $num."st";
       elseif ( $num % 10 == 2 )
               print $num."nd";
       elseif ( $num % 10 == 3 )
               print $num."rd";
       else
               print $num."th";
    print " Birthday {$user->profile_name}! Welcome to the site,";
  }
  else {
    print "Hello {$user->profile_name}! Welcome to the site,";
  }
  print "<ul><li>Change your <a href='/user/$user->uid/notify'>notify</a> settings</li><li><a href='/user/$user->uid/edit/newsletter'>Subscribe</a> to the newletter</li><li>View your <a href='/store/history/$user->uid'>Order History</a></li></ul>";
}
else {
  print "Welcome to the site,<p>As a registered member you can;</p><ul><li>Vote on the polls</li><li>Post in the forums</li><li>Subscribe to the newsletter</li><li>Bookmark pages (on site) for easy access to your favourite pages and products</li><li>Be notified when any new content is added</li><li>Save the contents of your shopping basket</li></ul>Please note that the store section of the site is not open yet, but will be coming shortly.";
}

Pobster

--------------------------------------------
http://www.justgiving.com/paulmaddern
--------------------------------------------

lemonidas’s picture

Multiple strange things happen.

I tried 'print' instead of echo and nothing happened.

I tried to place the block in an if ($user->uid) block and nothing showed up.

Then I placed only this line print '<a href="'.$base_url.'/user/'.$user->uid.'/edit/Personal+Info"><font size="1">edit</font></a><hr />'; in the if block (and all other commented out)) and strangely it worked!

I tried the profile_load_profile($user); to print some user info and it worked (outside the if block).

I don't know where the problem might be. (and it's also strange that this happens to everyone but the administrator!)

PS: Thanks for your code and answers :)

UPDATE:

This works

if ($user->uid){
	global $base_url,$user;
	profile_load_profile($user);
echo $user->profile_sex;

 print '<h3>Welcome , '.$user->name.'!</h3>';
// print '<a href="'.$base_url.'/user/'.$user->uid.'">'.$user->name.' profile</a> |';
 print '<a href="'.$base_url.'/user/'.$user->uid.'/edit/Personal+Info"><font size="1">edit</font></a><hr />';
}

This doesn't (and nothing is shown)

if ($user->uid){
	global $base_url,$user;
	profile_load_profile($user);
echo $user->profile_sex;

 print '<h3>Welcome , '.$user->name.'!</h3>';
 print '<a href="'.$base_url.'/user/'.$user->uid.'">'.$user->name.' profile</a> |';
 print '<a href="'.$base_url.'/user/'.$user->uid.'/edit/Personal+Info"><font size="1">edit</font></a><hr />';
}

No new variables are used. I don't know wth is wrong with this.

AjK’s picture

why is your global $user; on the inside of you if ($user->uid) { } ?

lemonidas’s picture

yes, sorry about that.
it is also outside of the if block.

pobster’s picture

Do you mean that you've got two separate php sections in your block? I don't think they 'carry over'? I think you'll have to redeclare global $user at the start of that section.

...As I said before, if you'd actually posted the complete code you're trying to use - most of this thread wouldn't exist as your problem would be solved by now...

Pobster

--------------------------------------------
http://www.justgiving.com/paulmaddern
--------------------------------------------

lemonidas’s picture

It works now (as shown above).

Thanks for your patience, i didn't want to post the code, since i think it is very poorly written (bad programming practices) and i would be ashamed to show it :/

pobster’s picture

Heh heh! ;o) All the more reason to post it to make sure it doesn't contain some gaping security hole!!!

Glad you got it working,

Pobster

--------------------------------------------
http://www.justgiving.com/paulmaddern
--------------------------------------------

pbland’s picture

Where is the profile_load_profile() function located? I searched every file in drupal and no method was found. I'm using 5.3 if that matters. Thanks.

---------------------------------------------------------------
My drupal site: Vacation-Places

pbland’s picture

Thanks fidot. I didn't setup my search to include .module's in Dreamweaver.

---------------------------------------------------------------
My drupal site: Vacation-Places