www.webdeveloper.com
Results 1 to 4 of 4

Thread: how to check if user is logged in or not?

  1. #1
    Join Date
    Jun 2013
    Posts
    33

    how to check if user is logged in or not?

    Hi,
    i have created a simple register and login system using phpmyadmin. On index.html page there are 2 divs. Left and right. On the right div there are 2 forms login and register. Register and login works fine. when user logs in, he is directed to another page.

    Now, after login if user comes to index.html page, i need to show him different options in the right div.. like. his dashboard, and more links to his personal account. To do that i changed index.html to index.php and added this code.

    Code:
    <div id="left">
    left div content
    </div>
    
    <div id=-"right">
    
    <?php
    
    if(isset($_SESSION['email']) && isset($_SESSION['pass']))
    {
    
    ?>
    //show user some links to his account.
    <?php
    }
    else
    {
    ?>
    //show login and register forms
    <?php
    }
    ?>
    </div>
    But it didn't work. how should i do this?

  2. #2
    Join Date
    Dec 2013
    Location
    Sydney, NSW Australia
    Posts
    1
    Is that the entirety of the page? If so, you need to add the session_start() function to the top of the file in order to access the session.

    Also, it would help to see the login script as well.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    If $_SESSION['pass'] is the user's password, I would recommend not saving it in $_SESSION to begin with, and of course then not checking for it to see if they are logged in. That's one less place a hacker might be able to steal passwords from.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    #1 - I strongly agree with NogDog's suggestion of NOT posting a password in SESSION (!).

    MOst importantly thought - whatever do you mean by "it didn't work"? The script died? The php didn't perform as expected? No links showed up?


    BTW - try learning to write your code in separate html and php sections. Makes for much easier writing, reading, and understanding the code. Switching in and out of php mode is such a PIA when coding, when reading and when debugging. In this case, just create your html and place a php var in each div. Then in our php area, depending upon your if statements, assign content to the appropriate php var and then let your html (with embedded php vars) simply display the page.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles