Backend Development 2 min read

PHP session_destroy – Destroy All Data in a Session

The article explains PHP's session_destroy function, which deletes all data stored in the current session without resetting global variables or the session cookie, describes its return values, and provides a complete example showing how to start a session, clear variables, optionally remove the session cookie, and finally destroy the session.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP session_destroy – Destroy All Data in a Session

Function Definition

bool session_destroy(void)

The session_destroy() function destroys all data associated with the current session. It does not reset the global variables linked to the session nor does it delete the session cookie. To use session variables again after calling this function, you must invoke session_start() anew.

Parameters

None

Return Value

Returns TRUE on success, or FALSE on failure.

Example

<?php
// Initialize session.
session_start();

// Reset all session variables
$_SESSION = array();

// If you want to delete the session cookie as well
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();
?>
backendweb developmentphpsessionsession_destroy
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.