PHP Login Public View Problem

Hey guys, so I’m new in PHP and i just started learning and I’m having a problem on PHP code for Log in.

So my question is, when there is a log in button or just text on the all the webpages and when you log in (ex. placed on the top right of the site), how do you make the log in button as log out, when an admin or a user logs in? or make the main navigation menu change (ex. adding a User Home or Menu, Profile, etc…)?

Thanks for the help. :slight_smile:

[php]if (logged in) {
// show logged in stuff
} else {
// show guest stuff
}[/php]

ie

[php]if (!empty($_SESSION[‘user’])) {
// show logged in stuff
} else {
// show guest stuff
}[/php]

Here’s an example of my navigation menu:
[php]


  • Home

  • About

  • Forums

  • Contact


  • <?php
    if ($pageTitle == “HTML5, CSS3, jQuery and PHP Forums” || preg_match("/ Forum/i", $pageTitle)) {
    if ($user) {
    echo ‘Logout’;
    } else {
    echo ‘Login’;
    }
    }
    ?>

  • <?php echo (!($user) && ($pageTitle == "HTML5, CSS3, jQuery and PHP Forums" || preg_match("/ Forum/i", $pageTitle))) ? 'Register' : NULL; ?>

  • Gallery


[/php]

Notice the if ($user) statement? Basically it’s the same as JimL explained.

Oh, I almost forgot here’s a neat little trick, put this in your utility (config) script:
[php]// Check for a user in the session:
$user = (isset($_SESSION[‘user’])) ? $_SESSION[‘user’] : NULL;[/php]

This way you don’t have to keep retyping $_SESSION[‘user’] all the time, a way to make code more efficient in my opinion. :wink:

I love OOP, sorry, just had to let it out.

In a normal environment one should be able to use something like $this->user everywhere in the app ^^

Just want to add when you code the login script you just have initialize the $_SESSION[‘user’];

an yes this is one step below OOP…I got this from Larry Ullman’s Advanced PHP and Object-Oriented Programming book. 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service