Using if statement to check if a user is logged in or not? Display different things..

Discussion in 'PHP' started by Nuclear_Ice, Nov 19, 2009.

  1. #1
    Hello, so I've a login system and everything. I'm trying to make it so if you are not logged in and you go to the home page it will have a UserName and Password bar at top, and if you are logged in when you visit the page then I want to make it have a "Welcome, username!"

    I've successfully got the code to make it do both of those functions nicely but I'm having a bit of trouble displaying information.

    The code I'm using has a basis like this:

    
    
    <?php
    if($session->logged_in)
    {
    ?>
    
           And right here I have the whole document to say <p>Welcome, <b><?php echo $session->username; ?></b>!</p> and then display a few links like edit account, log out and My Account, followed by the whole homepage.  All this is inside html tags.
    
    <?php
    }else{
    ?>
    
           And right here I have the whole document again but it's changed to display a username and password text field and not the account information if they're not logged in followed by the whole homepage. All this is inside html tags.
    
    <?php
    } 
    ?>
    
    
    Code (markup):
    So here's my problem:

    When I go to the homepage to check it out LOGGED OUT, it will not display anything at all. If I goto the login page and login, then go back to the homepage while being logged in it will display all the information ($session->username, the links, all that stuff.)



    Any ideas? Sorry I'm trying to explain it really well.. If you need any code bits to help me out on this just say so I'll post what I can. :)
     
    Nuclear_Ice, Nov 19, 2009 IP
  2. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    simple do like this:
    //PHP Code...
    if($session->logged_in)
    {
    //show: logged in info like welcome bla bla bla
    }
    else{
    //show: you need to login form
    }
    //----------------------------------

    after the if/else statement put the code of the rest of the page.
     
    xenon2010, Nov 19, 2009 IP
  3. Nuclear_Ice

    Nuclear_Ice Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you. :D So far working great! +Rep
     
    Nuclear_Ice, Nov 19, 2009 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no problem mate :D
     
    xenon2010, Nov 19, 2009 IP