170k views
3 votes
What code do I have to use in my php if i don’t want my $_SESSION[“variablex”] to destroy? But i already have a session_destroy() above the code that needs to be there. Or maybe there is an alternative?

So i want to destroy all my sessions EXCEPT $_SESSION[“variablex”]

User BigBalli
by
4.6k points

1 Answer

2 votes

Answer:

See Explanation Below

Step-by-step explanation:

Follow the steps below.

1. Start Session

2. Assign the session you want to preserve ( $_SESSION['variablex'] ) to a variable

3. Destroy all session (including the one you want, $_SESSION['variablex'])

4. Reassign the contents of the variable you created in (1) above to $_SESSION['variablex'].

The code is as follows (comments explain each line)

<?php

session_start(); // Start session

$variable = $_SESSION['variablex'] ; // assign $_SESSION['variablex'] to $variable

session_destroy(); // Destroy sessions

$_SESSION['variablex'] = $variable; // assign $variable back to $_SESSION['variablex'] to

// You can continue your code (if you have any), here

// End of solution

?>

User Sourabh Agrawal
by
4.9k points