Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

Unable to access session variables from other scripts in php

I have a login page which upon successfully validating email and password against the values of the database creates 3 session variables. The problem is that I'm unable to access these 3 session variables from other scripts even though session_start() is called before trying to access the variables from other scripts. On the login page too session__start() is called in the beginning and I'm able to print these 3 session variables on the same login page upon page refresh too.

My php version is 7.4 and the server is apache. I'm on a windows machine.

question from:https://stackoverflow.com/questions/65644475/unable-to-access-session-variables-from-other-scripts-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is your code but simplified. Run, enter your email and password, click "login". Refresh the page a few times with F5

<?php
    session_start(); 
    var_dump($_SESSION);
    $LoginSenderEMAIL = @filter_var($_POST['email'],FILTER_SANITIZE_EMAIL);
    $LoginSenderPASS = @filter_var($_POST['password'],FILTER_SANITIZE_STRING);
    




    if(isset($_SESSION['stest']) )
    {
        echo '<br/><b style="color:red;"> Session is OK.</b>';
    }
    else if(isset($LoginSenderEMAIL) && !empty($LoginSenderEMAIL) && isset($LoginSenderPASS) && !empty($LoginSenderPASS))
    {

        echo '<br/><b style="color:red;"> Form send. Session is OK</b>';
        $_SESSION['stest'] = 'testsesion';
    }
    else{ ?>

<form method="post">
    <table style="padding-left: 50px; padding-right: 50px;">
        <tr>
            <td class="rightAlign fontSize22">
                <label for="email"> Email: </label>
            </td>
            <td>
                <input type="email" name="email" id="email" class="fontSize22">
            </td>
        </tr>
        <tr>
            <td class="rightAlign fontSize22">
                <label for="password">Password: </label>
            </td>
            <td>
                <input type="password" name="password" id="password" class="fontSize22">
            </td>
        </tr>
        <tr>
            <td>
                <a href="forgotPassword.php"><input type="button" value="Forgot Password?" class="fontSize22" style="background-color: #FF4F5A; border: none; color: white;"></a>
            </td>
            <td>
                <input type="submit" value="Login" class="fontSize22" style="width:100%; background-color: #FF4F5A; border: none; color: white;">
            </td>
        </tr>
    </table>
</form>
    <?php } ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...