Jump to content

Unable To Unset Session - Help Needed !


mungoss

Recommended Posts

Hi all,

 

I've been working with php for awhile, but unfortunately I cannot grasp php session and find a solution to my problem.

 

I have a situation where I set a session value on 1st script and trying to unset it on 2nd script, but unfortunately the session variable will not unset.

The session is used for displaying status or error values and after displaying status , session should be unset.

 

On 1st script I use code:

 

 

session_start();

...

 

$_SESSION['SESS_STATUS']='This is status msg. ';

session_write_close();

header("location: 2nd.php");

exit();

 

 

 

On 2nd script I display session value and unset the session:

 

 

session_start();

...

 

if(isset($_SESSION['SESS_STATUS'])) {

$status = $_SESSION['SESS_STATUS'];

echo $status;

unset($_SESSION['SESS_STATUS']);

}

 

 

 

 

Msg gets displayed but unfortunately session 'SESS_STATUS' will not unset, so when this page gets refreshed, or navigated trough browser again, the same msg gets displayed again. On every page i use session_start(); so session is continued. I have tried many different ways to unset the session variable but with no luck.

 

 

Any help or advice is appreciated.

 

Thanks very much !

Edited by mungoss
Link to comment
Share on other sites

using session_destroy() will destroy entire session, better use like this

 

 

if(isset($_SESSION['SESS_STATUS']) && $_SESSION['SESS_STATUS'] != '') {
$status = $_SESSION['SESS_STATUS'];
echo $status;
$_SESSION['SESS_STATUS'] = '';
}

 

Even i too have this issue and solved this issue like above...sometimes unset is not working on session...

Edited by sowna
Link to comment
Share on other sites

Unfortunately sowna-s solution will not do the trick for me because in this code session is not unset:

 

if(isset($_SESSION['SESS_STATUS']) && $_SESSION['SESS_STATUS'] != '') {

$status = $_SESSION['SESS_STATUS'];

echo $status;

$_SESSION['SESS_STATUS'] = '';

 

 

 

so, whenever i come back to this page if(isset(..)) will check that session still exists and will trigger javascript display bar which is hidden if there is no session 'SESS_STATUS',

so, the session really has to be unset. Also I tried to rewrite SESSION as suggested "$_SESSION['SESS_STATUS']=' '" but it keeps showing old value set on 1st.php page.

 

I even tried HenryC-s suggestion to use session_destroy(); , but session still gets displaying the old msg set in session on 1st.php page.

 

I'm really confused now. Is working with session such a buggy thing or am I missing something here.

I've done online research but unfortunately couldn't find a solution or explanation.

​Does anybody know what could be the reason for this. Any help, or idea is appreciated.

 

​Thank You all on answers.

Edited by mungoss
Link to comment
Share on other sites

Without all of your code that would be needed to reproduce the problem, it's just a guess what is causing it. The fact that you just mentioned that there's some javascript as part of the problem, opens up about 3 more possibilities, in addition to the original 3-4 things that could be causing the problem.

 

Post the exact code that we would need that reproduces the complete problem, less anything like database credentials.

 

And since you are just trying to clear one session variable that holds a status message, you would NOT want to use session_destroy.

Link to comment
Share on other sites

I'm really confused now. Is working with session such a buggy thing or am I missing something here.

 

No not really; the theory behind sessions is relatively simple and to the best of my knowledge there aren't any bugs. Its likely your logic is messed up. Why don't you abstract the problem into a simplified example and get it working, then integrate that example with your actual code.

Edited by CPD
Link to comment
Share on other sites

Yes, I would not want to use session_destroy() because that will kick of currently logged in user.

Also I must say that I'm not experienced programmer and the problem is likely in my logic, so thank You all on patience.

 

Here' s cleared code that I've cut and pasted to new files that makes me the same trouble (even without additional javascript with plain php I couldn't unset the session variable 'ERRMSG_ARR':

Here's the code:

 

1st.php page:

----------------------------------

 

 

<?php

//Start session

session_start();

 

require_once('auth.php'); //authenticated

 

//Include database connection details

require_once('config.php');

 

//Array to store validation errors

$errmsg_arr = array();

 

//Validation error flag

$errflag = false;

 

//Connect to mysql server

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link) {

die('Failed to connect to server: ' . mysql_error());

}

 

//Select database

$db = mysql_select_db(DB_DATABASE);

if(!$db) {

die("Unable to select database");

}

 

//Function to sanitize values received from the form. Prevents SQL injection

function clean($str) {

$str = @trim($str);

if(get_magic_quotes_gpc()) {

$str = stripslashes($str);

}

return mysql_real_escape_string($str);

}

 

//Sanitize the the values

$member_id = $_SESSION['SESS_MEMBER_ID'];

$fname = clean($_POST['fname']);

 

 

 

//Input Validations

if($fname == '') {

$errmsg_arr = 'Missing name !';

$fokus = 'fname';

$errflag = true;

}

 

 

if($errflag) {

$_SESSION['ERRMSG_ARR'] = $errmsg_arr;

session_write_close();

header("location: 2nd.php");

exit();

}

 

?>

 

 

 

 

2nd.php page:

----------------------------

 

<?php

 

//Start session

session_start();

require_once('auth.php');

require_once('globalne_variable.php');

require_once('get-refresh-exec.php');

 

<?php

 

 

<div id="infobar-kontener">

 

 

<html lang="en-GB">

<head>

 

</head>

<body>

 

<?php

 

if(isset($_SESSION['ERRMSG_ARR'])) {

echo '

<div id="infobar">

<div class="textbar">

'.$_SESSION['ERRMSG_ARR'].'

<a href="" title="Zatvori!" id="closebar"><!--Zatvori --></a>

</div>

</div>

';

unset($_SESSION['ERRMSG_ARR']);

}

 

?>

 

</div>

 

</body>

</html>

 

 

 

 

Now, I've tried to remove session_write_close(); and luckily now i can unset the session 'ERRMSG_ARR'.

It got me confused, I've looked in php manual for session_write_close says:

 

"End the current session and store session data. Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. ..."

 

So I've been using session_write_close() after every creation and insertion in session variables. I'm also using it on my login-exec.php right after the 1st connection to database, getting users data and populating users MEMBER_SESSION with member id and other users data - it works fine.

 

Since I'm not sure when to use , session_write_close();, I will not use it for now.

What would be good practices, when to use session_write_close() ?

Link to comment
Share on other sites

By using session_write_close() in your authentication logic, the $_SESSION variables after that point are just regular program variables and anything you do to them won't be saved to the session data file.

 

The only time you need to use session_write_close is if you have a page that takes a long time to generate (the php script is still running) and things you link to on that page also have a session_start statement that need to access the same session as the main page. These would be things like dynamically generated images, css, javascript, iframe, page requested by javasciprt/ajax, ... that use session variables.

 

If you do use session_write_close, you would use it ONCE on a page, after the last place where you assign or unset any session variables.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.