Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

unset session on leaving a certain page?

my session

 <div>
        <?php if(isset($_SESSION['message'])) :?>
        <span class=" session fixed bg-blue-500 text-white py-2 px-4 rounded-xl bottom-3 right-3">
            <?php echo $_SESSION['message'];?>
        </span>

        <?php endif ?>
        <?php  unset($_SESSION['message']) ?>
        
    </div>

by using unset() it is unset on the page on which i need to show it and when i comment the unset() it show the session data to all pages.. :(

0 likes
6 replies
jlrdw's avatar

Why not get the needed data in a controller and pass it to a view. Otherwise don't unset it there, unset in a condition somewhere.

1 like
Shivamyadav's avatar

@jlrdw yeah I am using php and understanding it .. In php how can i achieve to when i visit to another page session will unset at that time for the particular variable?

1 like
jlrdw's avatar

@Shivamyadav unset on the next page. Are you using a controller. Or don't unset, just don't display unless needed.

1 like

Please or to participate in this conversation.