colap Posted March 14, 2017 Share Posted March 14, 2017 <?php if(session_id() == '') { session_start(); } $_SESSION['msg']="Updated."; psession($_SESSION['msg']); function psession($msg){ echo $msg; unset($msg); } ?> This doesn't unset $_SESSION['msg']. How can I unset it? Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/ Share on other sites More sharing options...
requinix Posted March 14, 2017 Share Posted March 14, 2017 You can't really do it. Not like that. What problem are you trying to solve? Why do you need to unset a value this way? Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/#findComment-1544220 Share on other sites More sharing options...
colap Posted March 14, 2017 Author Share Posted March 14, 2017 I want to unset session variable from inside function which is passed as function parameter. In this way: [codeunset($_SESSION['msg']); [/code] Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/#findComment-1544221 Share on other sites More sharing options...
requinix Posted March 14, 2017 Share Posted March 14, 2017 Then that's the way you have to do it: unset whatever in $_SESSION directly. When you unset($msg) all you're doing is unsetting that variable. Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/#findComment-1544223 Share on other sites More sharing options...
gizmola Posted March 14, 2017 Share Posted March 14, 2017 Ok, so first things first -- why? if(session_id() == '') { session_start(); } Do you want sessions or not? If so, you should just have session_start(). $_SESSION is a super global. In PHP superglobals are variables that are available everywhere and thus visible inside functions. So, in this case, your function has little to no value in terms of the passing of a parameter. Quite frankly, it would be really bad practice to do what you were trying to do even if it did work. One purpose of a functions is to hide details from the rest of the program, so setting/unsetting a variable inside a function that is otherwise global really goes against the idea of "information hiding". Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/#findComment-1544235 Share on other sites More sharing options...
requinix Posted March 15, 2017 Share Posted March 15, 2017 Ok, so first things first -- why? if(session_id() == '') { session_start(); } Do you want sessions or not? If so, you should just have session_start(). I've done that in a handful of cases where I needed sessions but wasn't sure if it had been started yet (and there wasn't anywhere that would authoritatively start them). It could be an indication of poor design, however. Quote Link to comment https://forums.phpfreaks.com/topic/303455-how-can-i-unset-a-session-variable-that-is-passed-as-function-parameter/#findComment-1544238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.