Jack Billy
Jack Billy

Reputation: 7211

Session unset function not working

I have made this code(it is a part of my whole script) --

if(unset($_SESSION['USER_ID']))
{
     $success_code_array[] = '3111';
     $_SESSION['POPUP_SUCCESS_CODE'] = $success_code_array;
     session_write_close();
     header("Location: ".$_SESIION['PATH']."");
     exit();
}
else
{
     $_SESSION['ERROR_CODE'] = '177';
     session_write_close();
     header("Location: ".$_SESSION['PATH']."error.php");
     exit();
}

And error I got --

Parse error: syntax error, unexpected T_UNSET in C:\xampp\htdocs\mysharepoint\1.1\res\scripts\php\functions\log_out.php on line 21

Can anyone tell me why I'm getting this error and what could be done to overcome this error?

Upvotes: 0

Views: 3200

Answers (3)

Jan Zyka
Jan Zyka

Reputation: 17888

unset is void, it doesn't return anything. If this is kind of logout script you probably can destroy the whole session as follows:

if(session_destroy()) { ...

Of course you should save $_SESSION['PATH'] somewhere for further usage before you destroy the session ...

EDIT:

Beware you have also a typo there. It doesn't cause the problem but may save you few minutes later:

if(unset($_SESSION['USER_ID']))
{
     $success_code_array[] = '3111';
     $_SESSION['POPUP_SUCCESS_CODE'] = $success_code_array;
     session_write_close();
     header("Location: ".$_SESIION['PATH'].""); // <-- $_SESIION instead of $_SESSION
     exit();
}

Upvotes: 2

Flatlin3
Flatlin3

Reputation: 1659

unset() returns nothing (void). https://www.php.net/manual/en/function.unset.php

I'm not sure if the error comes from this but it doesn't make sence using it in an if-statement.

Upvotes: 0

fabrik
fabrik

Reputation: 14365

Don't unset session but destroy it. That's the proper way to unset it.

Upvotes: 0

Related Questions