moh_abk
moh_abk

Reputation: 2164

Delete Individual Sessions in PHP

I've been going at this for hours. I have a website and a blog.

I register the blog sessions with the code below;

    session_id("blog");
    session_start();
    $_SESSION["blog"]["username"] = $_POST['username'];
    $_SESSION["blog"]["password"] = $_POST['password'];
    $_SESSION["blog"]["firstName"] = $row ['user_firstname'];
    $_SESSION["blog"]["lastName"] = $row ['user_lastname'];

I register the website session with the code below;

    session_id("web");
    session_start();
    $_SESSION["web"]["email"] = $_POST['email'];
    $_SESSION["web"]["password"] = $_POST['password'];
    $_SESSION["web"]["firstName"] = $row ['firstName'];
    $_SESSION["web"]["lastName"] = $row ['lastName'];

I logout individual sessions with the code below;

    session_id("web");
    session_start();
    session_destroy();

And

    session_id("blog");
    session_start();
    session_destroy();

This is not working as logs out the website because only one PHPSESSID is created when I check in chrome.

Thanks :)

Upvotes: 3

Views: 143

Answers (2)

Hasan Tareque
Hasan Tareque

Reputation: 1741

//start session - have to be only once 
session_start();

//create session variable - in your word create individual session and assign variable 
$_SESSION['session_name'] = "name";
$_SESSION["session_name"]["variable1"] = $variable1_value;
$_SESSION["session_name"]["variable2"] = $variable2_value;

//unset individual session variable - in your word logout individual session
unset($_SESSION['session_name']);

Upvotes: 1

Jonas m
Jonas m

Reputation: 2734

You can simply use the unset method.

 // Lets start php sessions. This must go before any other headers
session_start();

// When setting
$_SESSION['web'] = array( 
                            "email" => "[email protected]",
                            "password" => "password",
                            "firstname" => "Jonas",
                            "lastname" => "M" 
                        );


// When unsetting
unset( $_SESSION['web'] );

// OR you can null it

$_SESSION['web'] = null;

Upvotes: 5

Related Questions