Search code examples
phpsessionsession-cookiessession-state

Unsetting session in PHP


I have a form which is used to insert records into the database and if all goes right redirect the user to the index page with a message (actually set with a session) about the success or failure of the operation.

Now, am able to set the session in add_book.php and display the message in index.php but the message still persist after reloading the page without going away.

in add_book.php. I have this

$_SESSION['insert'] = "Record inserted into the database";
                        session_regenerate_id();
                        session_write_close();
                        echo '...succesfully add new book...';
                        header("Location: index.php");
                        exit();  

and in the index.php, I have this

<?php if(isset($_SESSION['insert'])){
?>
<p class="update"><?php echo $_SESSION['insert'];?></p>
<?php
}//end of isset $_SESSION['insert']
?>

Solution

  • Try deleting the session after displaying:

    <?php if(isset($_SESSION['insert'])){
    ?>
    <p class="update"><?php echo $_SESSION['insert'];?></p>
    <?php
    unset($_SESSION['insert']);
    }//end of isset $_SESSION['insert']
    ?>