Tolu Farotimi
Tolu Farotimi

Reputation: 25

how to end a session in php with logging out

I started a session $_SESSION['ProdID'] = $ProdID; earlier in my code and I started another ProdID session in another page of my script.

I want to end the first one while this new one will be active without logging out.

Upvotes: 1

Views: 10965

Answers (6)

Prem
Prem

Reputation: 707

Create a page with any name you want. For example you create a page named as logout.php and paste this code in it.

<?php
    session_start();    
    session_destroy();
    header('location:login_page.php');
?>

Upvotes: 2

Tigran Harutyunyan
Tigran Harutyunyan

Reputation: 1

Only type unset session end of code, Like this

unset($_SESSION['ProdID']);

Upvotes: 0

hakre
hakre

Reputation: 198204

First destroy the current session by regenerating a new session ID to create new cookies. You can then set your values in the new session, the old session is destroyed. Optionally delete all old session variables if you don't need them any longer:

/* generate new session id and delete old session in store */
session_regenerate_id(true);

/* optional: unset old session variables */
$_SESSION = array();

/* set new value(s) */
$_SESSION['name'] = 'value';

If you still want to keep the old session ("without logging out") you can remove the true parameter so the old session is kept in store:

/* generate new session id and keep old session in store */
session_regenerate_id();

The rest would remain the same.

Upvotes: 1

Soojoo
Soojoo

Reputation: 2166

Use

unset($_SESSION["ProdID"]);

Upvotes: 0

Ali
Ali

Reputation: 684

if you want to destroy all sessions , it's better to use session_destroy()

if you want to destroy specific session , you can use unset($_SESSION['']);

Upvotes: 1

devrox
devrox

Reputation: 110

Try using session_destroy(); to end your current session.

Upvotes: 0

Related Questions