How can you check if a PHP session exists?

前端 未结 11 1317
孤独总比滥情好
孤独总比滥情好 2021-02-04 06:11

Just wondering how to check if a PHP session exists... My understanding is that no matter what, if I am using sessions, I have to start my files with session_start() to even acc

相关标签:
11条回答
  • 2021-02-04 06:41

    I solved this three years ago, but I inadvertently erased the file from my computer.

    it went like this. 3 pages that the user had to visit in the order I wanted.

    1) top of each php page enter code heresession start();enter code here 2) first page: a) enter code here$_session["timepage1"] = a php date function; time() simple to use b) enter code here$_session["timepage2"]= $_session["timepage1"]; b) enter code here$_session["timepage3"]=$_session["timepage1"]; 3) second page: a) enter code here$_session["timepage2"] = a php date function; time() simple to use b) enter code here$_session["timepage3"]= $_session["timepage3"]; 3) third page: a) enter code here$_session["timepage3"] = a php date function; time() simple to use

    the logic: if timepage3 less than timepage3 on page 2 {the user has gone to page 3 before page 2 do something}

    if timepage2 on page 2 less than timepage1 {the user may be trying to hack page two we want them on page 1 do something}

    timepage1 should never equal timepage2 or timepage3 on any page except page1 because if it is not greater on pages two or three the user may be trying to hack "do something"

    you can do complex things with simple arithmetic with the 3 timepage1-2-3 variables. you can either redirect or send a message to say please go to page 2. you can also tell if user skipped page 2. then send back to page 2 or page one, but best security feature is say nothing just redirect back to page1.

    if you enter code hereecho time(); on every page, during testing, you will see the last 3 digits going up if you visit in the correct order.

    0 讨论(0)
  • 2021-02-04 06:44

    You can call session_id before session_start. http://www.php.net/manual/en/function.session-id.php - read the id param

    0 讨论(0)
  • 2021-02-04 06:46

    In PHP versions prior to 5.4, you can just the session_id() function:

    $has_session = session_id() !== '';
    

    In PHP version 5.4+, you can use session_status():

    $has_session = session_status() == PHP_SESSION_ACTIVE;
    
    0 讨论(0)
  • 2021-02-04 06:46
    isset($_SESSION)
    

    That should be it. If you wanna check if a single session variable exists, use if(isset($_SESSION['variablename'])).

    0 讨论(0)
  • 2021-02-04 06:55

    I find it best many times (depends on the nature of the application) to simply test to see if a session cookie is set in the client:

    <?php
    
    if (isset($_COOKIE["PHPSESSID"])) {
        echo "active";
    } else {
        echo "don't see one";
    }
    
    ?>
    

    Of course, replace the default session name "PHPSESSID" with any custom one you are using.

    0 讨论(0)
  • 2021-02-04 06:57

    switch off the error reporting if noting is working in your php version put top on your php code

    error_reporting(0);

    0 讨论(0)
提交回复
热议问题