skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Found the solution. I noticed that the login page was opened through localhost in the url while the script2 trying to access the session variables was being opened up through 127.0.0.1. Sessions in php are unique to the domain, so localhost and 127.0.0.1 are treated differently.


  2. 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 } ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search