How to Unset Session Variables in PHP

Pre-Hypertext Processor (PHP) is a programming language designed for Web development. PHP supports browser sessions, which enable a user to authenticate with a server and stay logged into a site while browsing Web pages. These sessions can contain specific variables that are unique only to the session and expire automatically when the user logs out or the session expires. You will sometimes want to destroy information while a user's session is still active, such as credit card data once a purchase is completed. You can destroy this type of information using the unset command.

Things You'll Need

  • Text editor
Show More

Instructions

    • 1

      Identify the session variable you wish to unset. This is contained in the PHP file where it was first defined. All session variables begin with "$_SESSION" and can be identified by searching your PHP files in a text editor.

    • 2

      Open the PHP file containing the logical point where the session variable should be unset. As an example, if you use a function called "transaction_finished" to redirect a user to a thank-you page once a purchase is completed, this is a good place to remove a session-stored credit card. Open the file containing this part of your code.

    • 3

      Remove the stored session variable with the following command: "unset($_SESSION['<variablename>']);" replacing <variablename> with the name of your session variable. For example, if credit-card information is stored in the variable "$_SESSION['cc']", type "unset($_SESSION['cc']);" on its own line to unset it.

    • 4

      Save the document in your text editor.

Tips & Warnings

  • Use the command "session_unset()" at any point in your code to unset all session variables.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured