SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: if session_set

  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if session_set

    Hey,

    Im trying to create a navigation for my site, but i want two different ones,

    1 for people who are logged in
    1 for people who arent logged in

    I know you can do it with if/else but i dont know how to check if theres a session open.

    Any ideas?
    BKerr

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to check two things 1) if session was started and 2) if there is "login" variable in session

    PHP Code:
    if(isset($_SESSION) && isset($_SESSION['login']))
        
    // okay
    else
       
    // not logged in 

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same as a post or get method:

    PHP Code:
     session_start();
    if(isset(
    $_SESSION['loggedin']))
    {
     
    //do this
     

    Silly

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    You have to check two things 1) if session was started and 2) if there is "login" variable in session

    PHP Code:
    if(isset($_SESSION) && isset($_SESSION['login']))
    // okay
    else
    // not logged in 
    You dont really need to check if the session has started. You have to start it anyway with session_start() or it wont work. If the global $_SESSION['login'] is not there then that should be good enough.

    Silly

  5. #5
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesnt work:
    PHP Code:
    <?php
    if(isset($_SESSION['loggedin']))
    {
    echo 
    "LOGGED IN NAVIGATION";

    else
    {
    echo 
    "LOGGED OUT NAVIGATION";
    }
    ?>
    I have session_start() at the very top of the page with my other includes and stuff. Does every $_SESSION automatically have a loggedin, because i dont remember setting it.
    BKerr

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    on the login page, I usually have
    PHP Code:
    $_SESSION['loggedin'] == 'Y'
    Then use
    PHP Code:
    if($_SESSION['loggedin'] == 'Y') {
    # nav for logged in
    } else {
    # redirect or nav for not looged in


    S
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sillysoft
    You dont really need to check if the session has started. You have to start it anyway with session_start() or it wont work.
    This is more than disputable. A good practice is not to start session until you really need it. Just take look at this...

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spikeZ that seems like a good idea, i added session logged in under my others in accesscontrol.php which is the file the people login on:
    PHP Code:
    $_SESSION['uid'] = $uid

    $_SESSION['pwd'] = $pwd;

    $_SESSION['loggedin'] == 'Y'
    Now on my index, i have session_start(); at the top and in my table i have:
    PHP Code:
    <?php
            
    if($_SESSION['loggedin'] == 'Y') {
            echo 
    "LOGGED IN";
            } else {
            echo 
    "NOT LOGGED IN";
            }
    ?>
    But it always says NOT LOGGED IN, even after logging in and out seeing if it wasnt set, i dont know whats wrong.
    BKerr

  9. #9
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    help?
    BKerr

  10. #10
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive tried all codes posted here, and none are working. Please, i need help. Im on a deadline.
    BKerr

  11. #11
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your using double = when setting the loggedin session. Only use one =.

    Silly

  12. #12
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    This is more than disputable. A good practice is not to start session until you really need it. Just take look at this...
    Hmm Im baffled that if you have a session, how would you check it on a page without starting the session?

    Silly

  13. #13
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok that worked, but that it still thinks your logged in after you logout, on my logout.php page
    PHP Code:
    <?php

    $_SESSION 
    = array();
    session_destroy();

    ?>
    Should this kill $_SESSION['loggedin'] too?
    BKerr

  14. #14
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On the login page you have to start the session in order to destroy it.

    Silly

  15. #15
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you meant logout page, i do start it, just all the way at the top of the page.

    It logs you out and everything, but the navigation still thinks your logged in
    BKerr

  16. #16
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well what does your navigation code look like? How does it determine what to display?

    Silly

  17. #17
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if($_SESSION['loggedin'] = 'Y') {
            echo 
    "LOGGED IN";
            } else {
            echo 
    "NOT LOGGED IN";
            }
    ?>
    BKerr

  18. #18
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so here your problem is a single =, you need a double =

    Silly

  19. #19
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the trick, thanks.
    BKerr


Bookmarks

Posting Permissions

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