Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which methods destroy/unset session(s) variable(s) and work efficiently in most PHP version as well? [closed]

Tags:

php

session

I am trying to unset and destroy a specific session and I came across different methods and would like to know which is the efficient method to do the work and deals with most php versions as well.

First method:

unset($_SESSION['site1']);
session_destroy();

Second method:

session_unset();
session_destroy();

Update: [RESOLVED]

<?php
session_start();

unset($_SESSION['site1']);
?>
like image 481
Si8 Avatar asked Dec 27 '22 01:12

Si8


1 Answers

Just use unset to unset any array index. And Session itself is an array. So use unset to unset your particular session index. You are wrong in the case of session_unset and session_destroy functions as they don't accept any parameters.

Also the point to note is that, you should not be using session_destroy as it will unset all available sessions. For eg. while loggin out, you may not be wanting to lose your products in cart formed using session.

Edit

session_destroy — Destroys all data registered to a session

session_unset — Frees all session variables

session_unset just clears the $_SESSION variable. It’s equivalent to doing:

$_SESSION = array(); So this does only affect the local $_SESSION variable instance but not the session data in the session storage.

In contrast to that, session_destroy destroys the session data that is stored in the session storage (e.g. the session file in the file system).

Everything else remains unchanged.

like image 81
Sanjay Avatar answered Jan 05 '23 11:01

Sanjay