• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php session_destroy() – Delete Session File Where Session Data is Stored

php session_destroy() – Delete Session File Where Session Data is Stored

April 1, 2022 Leave a Comment

When working with sessions in php, you can use the session_destroy() function to destroy all data associated with a current active session by deleting the session file. The session variables will still be available, but will not be connected with the session.

session_start();

// do things with your session

session_destroy();

If you want to kill a session completely, you need kill a session in php completely, you need to unset all of the session variables and delete the session cookie.

Below is taken from the php manual for session_destroy().

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();

Session handling is a big concept in php that allows us to handle and store user information across an entire website or web application.

When working with sessions, the ability to delete session data can be useful.

The php session_destroy() function allows us to destroy all data associated with a particular session in our php code.

session_destroy() deletes the session file which is created and used after starting a session with session_start(). session_destory() does not unset any of the global variables associated with sessions or unset the session cookie.

When calling session_destroy() you are deleting the data associated with the session, but you will still be able to access the variables stored in ‘$_SESSION’.

This can be a little confusing but makes sense – you may want to keep using the certain variables from ‘$_SESSION’ in your code.

How to Completely Kill a Session in php

If you want to completely kill a session, there are a few additional steps you need to take before calling session_destroy()

To kill a session completely in php, you need to unset all of the session variables, and also delete the session cookie.

From the php manual, to destroy a session completely, you can use the following code.

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();

Hopefully this article has been useful for you to learn how to destroy a session in php with session_destroy().

Other Articles You'll Also Like:

  • 1.  How to Create an Empty Array in php
  • 2.  PHP Variables Are Preceded by $
  • 3.  php is_numeric – Check if Variable is a Number
  • 4.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 5.  php str_replace – Replace String in php
  • 6.  PHP Numerically Indexed Array Begin With Position 0
  • 7.  php pi – Get Value of pi Using php pi() Function
  • 8.  Using php to Copy Files Easily from One Directory to Another
  • 9.  php array_shift() – Remove the First Element from Array
  • 10.  php Print Array – How to Print All Elements in Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy