简体   繁体   中英

php session variable expiring

i am using a session variable to authenticate, acc to my knowledge the session variable is supposed to be stored at the server even when new pages are loaded. I am using the following code:

    <?php
session_start();
echo $_POST['path'];

if($_POST['path']=="index")
{
    $_SESSION['rightPath']=1;
    if(isset($_SESSION['rightPath']))
        echo "it is set";
    ?>
    <script type="text/javascript">parent.location='UI.php'</script>
    <?php   
}
else
{?>
<script type="text/javascript">parent.location='index.php'</script>
<?php   
}
?>

here this isset function tells me that the variable is set but in the next page ui.php is it not giving me the same result.

    <?php
    if(!isset($_SESSION['rightPath']))
    {
        echo "it not is set";?>

<?php   }
?>

this is the ui.php page snippet. here the if statement is executing.

what am i doing wrong ?

you need to start session here is well

<?php
        session_start();
        if(!isset($_SESSION['rightPath']))
        {
            echo "it not is set";?>

    <?php   }
    ?>

You are not starting the session in UI.php. The code should be like this, with session_start at the top:

<?php
session_start();

if(!isset($_SESSION['rightPath']))
{
    echo "it not is set";?>
}
?>

The session_start() creates a session or resumes the current one. So, while you are creating the session earlier, it is NOT resumed unless you do a session_start() again on every page where you intend to use the session variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM