1

I've looked everywhere, and can't seem to find an answer to my problem. I've written some code to toggle between a log in block and, once the user has logged in, a line of text that includes both a link to the logged-in user's profile (I have the Profile 2 module installed) and a log out link.

Both the log in block and the line of text display nicely on my site when users log in and out. The problem comes when I click on the user profile link, which takes me to the user's profile page (which is correct). But then, when I click the "log out" link while on the profile page, it throws an error, rolling the two URLs into one.

Here is the code I included on the page.tpl.php:

  <?php
    global $user;
    $var1 = $user->name;
    $var2 = 'Welcome back' . " " . '<a href="/users/' . 
    str_ireplace(' ', '-', $var1) .  '">' . ucwords($user->name) . '</a>' . " " . '|' . " " . '<a href="user/logout">Log Out</a>';
    if ($user->uid == false) {    
      print render($page['code']); 
    }
    else {
  ?>
</div>
<div id="style2">
  <?php print $var2; ?>
</div>           

(Not that it matters, I'm sure, but a few lines of this code are wrapped in div wrappers that this post wouldn't allow me to include.) When a user logs out from the profile page, this is the URL that results:

www.site.com/users/user/logout

Which obviously doesn't do the job of logging the user out. I want to emphasize that on every other page I've tested, the user can hit the log out link and be logged out with no problem; the issue only exists when trying to log out from the profile page. I suspect that I've coded something wrong with respect to the link HTML, but can't seem to figure out what the issue is. Any help would be much appreciated, thank you.

  • Beside the answer below there seem to be a missing the closing </div> of <div id="style2"> – Emil Orol Apr 24 '13 at 18:52
  • Yeah, for some reason the text editor wouldn't let me include those div tags, so I had to delete them for the purposes of this post. Thanks, redhatlab! – Drupguy Apr 24 '13 at 19:13
1

The issue is you have href="users/logout" not href="/users/logout", eg, it's a relative url, and thus building upon where you are in your site.

  • Yep, that did it! Thanks, Jimajamma. Except now, I'm getting an "Undefined property: stdClass::$name" error where I have included "$user->name". I'm not finding an explanation to why this is happening online. Anyone have any ideas? – Drupguy Apr 24 '13 at 19:12
  • if you are getting $name as the undefined property, check if you are doing $user->$name somewhere instead of $user->name :) – Jimajamma Apr 24 '13 at 19:37
  • That's not the problem, unfortunately, Jimajamma - my code is displayed in my original post, and I use $user->name twice - I didn't use $name at all. – Drupguy Apr 24 '13 at 19:51
  • If it was working before and the only thing you added was a / to the href, something else has been "fat fingered" for no better word :( – Jimajamma Apr 24 '13 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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