Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Check if SESSION exists after login

    Hello,

    I have a login page that redirects to my second page after login. The session is created in the login page. In my second page I need to check if there is a valid session, without creating a new one. Is it possible to check it, without adding session_start() in my second page, so that no new session is created?

    Here is the code of my login page:
    PHP Code:
    <?php

    session_start
    (); // Creates a session

    $_SESSION["username"] = $username;
    header("Location: secondpage.php");
    ?>
    Here is the code of my second page:

    PHP Code:
    <?php

    session_start
    (); //  How can I remove this line, so that it doesn't create a new session?

    if (isset($_SESSION)) {print "session exists";} // Checks if session exists
    else {print "session does not exist";}
    ?>
    ...

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The function session_start() does not create a "new" session every time it is called; it will check the HTTP request header for existing session data and make it available to your script if it's there. It's required for sessions to work properly.

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    why does session_start(); create a new cookie named PHPSESSID, when I execute the following code?

    PHP Code:
    <?php 
    session_start
    ();
    ?>
    ...

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    By default PHP sessions uses cookies and if the session does not exist then session_start() creates a cookie like you are seeing. If the session already exists session_start() uses that cookie.

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks,

    can I disable that session_start() creates a cookie if it is not on the login page?

    My goal is to only give a session cookie to the persons who log in, and those who don't log in shouldn't get one only because I want to check if they have one.
    Last edited by archipuir; 12-20-2010 at 09:10 PM.
    ...

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Not really, since the only way you can check $_SESSION is by starting session. But, you can destroy the session if there are no login credentials found. The best way to destroy a session is discussed in the PHP manual here:

    http://us2.php.net/manual/en/functio...on-destroy.php

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Location
    Florida
    Posts
    155
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Changing:
    PHP Code:
    if (isset($_SESSION)) 
    to
    PHP Code:
    if (isset($_SESSION["username"])) {
    // code
    }else{

    //


    Should do the trick.

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You'll still need session_start() up top which will create a cookie.


  •  

    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
    •