gibby
04-20-2005, 03:53 PM
I'm experimenting with the $_SESSION variable and I'm trying to get a session to end. The only function I know of that comes close to ending a function that I've seen is session_write_close(), but that doesn't seem to work for me. I've got 2 files - session.php and clear.php - that look like this:
session.php
<?php
session_start();
echo '<html><head><title>session.php</title></head>';
echo '<body>';
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else $_SESSION['count']++;
echo 'session[count] = '.$_SESSION['count'].'<br>';
echo '<a href="session.php">increment count</a><br>';
echo '<a href="clear.php?">clear</a><br>';
echo '</body></html>';
?>
and...
clear.php
<?php
session_start();
session_write_close();
echo '<script language="javascript">location.href="session.php"</script>';
?>
So, when you go to session.php the first time, it will display session[count] = 0, and every time you click "increment count" it will increment the value of $_SESSION['count']. The part that's not working is the clear link. I'd like it to go to clear.php, end the session, return to session.php, and display session[count] = 0, as it would do if the session had just started again. It doesn't do this though. If you click "clear", it simply increments $_SESSION['count'] again.
If you want to check it out for yourself, you can go to http://www.shahspace.com/PHP/session.php.
How do I close a session such that starting it up again gives a fresh $_SESSION array without any previous values?
session.php
<?php
session_start();
echo '<html><head><title>session.php</title></head>';
echo '<body>';
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else $_SESSION['count']++;
echo 'session[count] = '.$_SESSION['count'].'<br>';
echo '<a href="session.php">increment count</a><br>';
echo '<a href="clear.php?">clear</a><br>';
echo '</body></html>';
?>
and...
clear.php
<?php
session_start();
session_write_close();
echo '<script language="javascript">location.href="session.php"</script>';
?>
So, when you go to session.php the first time, it will display session[count] = 0, and every time you click "increment count" it will increment the value of $_SESSION['count']. The part that's not working is the clear link. I'd like it to go to clear.php, end the session, return to session.php, and display session[count] = 0, as it would do if the session had just started again. It doesn't do this though. If you click "clear", it simply increments $_SESSION['count'] again.
If you want to check it out for yourself, you can go to http://www.shahspace.com/PHP/session.php.
How do I close a session such that starting it up again gives a fresh $_SESSION array without any previous values?