FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


destroy\unset sessions?





ranzon
how can i do so for an example $_session['loggedIn'] is unset?
or an logout script for the sessions:
loggedIn
username
password
hexkid
ranzon wrote:
how can i do so for an example $_session['loggedIn'] is unset?
or an logout script for the sessions:
loggedIn
username
password


Remember $_session and $_SESSION are different variables!

I think you want (EDITed wrong tag)
Code:
<?php // logout.php
session_start();
unset($_SESSION);
// you may want to delete the session cookie
if (isset($_COOKIE[session_name()])) {
  setcookie(session_name(), '', time()-60);
}
session_destroy();
echo 'You have been logged out.';
?>
ranzon
when i use it i get:
Warning: Wrong parameter count for session_destroy() in logout.php on line 7
You have been logged out.

the script is like this:
Code:
<?php // logout.php
unset($_SESSION['loggedIn']);
// you may want to delete the session cookie
if (isset($_COOKIE[session_name('loggedIn')])) {
setcookie(session_name('loggedIn'), '', time()-60);
}
session_destroy('loggedIn');
echo 'You have been logged out.';
?>
hexkid
ranzon wrote:
when i use it i get:
Warning: Wrong parameter count for session_destroy() in logout.php on line 7
You have been logged out.

the script is like this:
Code:
<?php // logout.php
unset($_SESSION['loggedIn']);
// you may want to delete the session cookie
if (isset($_COOKIE[session_name('loggedIn')])) {
setcookie(session_name('loggedIn'), '', time()-60);
}
session_destroy('loggedIn');
echo 'You have been logged out.';
?>


session_destroy() takes no parameters. It is an error to do
Code:
session_destroy('something');


http://www.php.net/session_destroy wrote:
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.
ranzon
hexkid wrote:
ranzon wrote:
when i use it i get:
Warning: Wrong parameter count for session_destroy() in logout.php on line 7
You have been logged out.

the script is like this:
Code:
<?php // logout.php
unset($_SESSION['loggedIn']);
// you may want to delete the session cookie
if (isset($_COOKIE[session_name('loggedIn')])) {
setcookie(session_name('loggedIn'), '', time()-60);
}
session_destroy('loggedIn');
echo 'You have been logged out.';
?>


session_destroy() takes no parameters. It is an error to do
Code:
session_destroy('something');


http://www.php.net/session_destroy wrote:
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.


now where i did that i get this message:
Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in logout.php on line 7
You have been logged out.
hexkid
ranzon wrote:
hexkid wrote:
ranzon wrote:
the script is like this:
Code:
<?php // logout.php
unset($_SESSION['loggedIn']);
// ...
?>


session_destroy() takes no parameters. It is an error to do
Code:
session_destroy('something');


now where i did that i get this message:
Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in logout.php on line 7
You have been logged out.


Oops ... you left out the session_start() somewhere between all these quoting Smile

Before you can use session_destroy() (or almost any other session related function) you have to call session_start().
AftershockVibe
You must also have session_destroy() AFTER any output to the browser has finished.

Similarly, session_start() must go before anything sent to the browser (including headers).
hexkid
AftershockVibe wrote:
You must also have session_destroy() AFTER any output to the browser has finished.

Huh? I disagree Smile
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();
?>
<html>
<head>
  <title>session test</title>
</head>
<body>
<?php
if (isset($_GET['reset'])) {
  unset($_SESSION);
  if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-60);
  }
  session_destroy();
  for ($i=0; $i<100; ++$i) {
    echo $i, ' ';
    echo '<!-- session has been destroyed! -->';
  }
  exit('<br><br>session destroyed.<br><a href="sesstest.php">reload test page</a>.</body></html>');
}
if (!isset($_SESSION['count'])) $_SESSION['count'] = 0;
echo ++$_SESSION['count'], '<br><a href="sesstest.php">reload</a>.<br>';
echo 'Or <a href="?reset">reset session</a>.';
?>
</body>
</html>


AftershockVibe wrote:
Similarly, session_start() must go before anything sent to the browser (including headers).
With this I agree Smile
AftershockVibe
hexkid wrote:
AftershockVibe wrote:
You must also have session_destroy() AFTER any output to the browser has finished.

Huh? I disagree Smile


Sorry, my bad. I must have just assumed this was the case because I stuck it in my footer along with the GZIP stuff.
ammonkc
You could also use unset(). but you still need to call session_start() in the begining of your script. Also you have to unset() any session variables in the same order in which you set them.

unset($_SESSION['loggedin');
unset($_SESSION['uname');
unset($_SESSION['pass']);

Sometimes I use both unset() and session_destroy() just to be safe. I know its redundent, but I know they user is really gonna get logged out that way.
hexkid
ammonkc wrote:
... Also you have to unset() any session variables in the same order in which you set them.

Uh? I disagree.
Code:
<?php
session_start();
echo 'Curresnt SID is ', session_id(), '<br>';
if (!isset($_SESSION['active'])) {
  $_SESSION['active'] = true;
  $_SESSION['one'] = 1;
  $_SESSION['two'] = 2;
}
++$_SESSION['one'];
$_SESSION['two'] += 2;

echo $_SESSION['two'];
unset($_SESSION['two']);
echo $_SESSION['two'];

echo $_SESSION['one'];
if (rand(0, 1)) {
  session_destroy();
  echo 'session destroyed.<br>';
}
echo $_SESSION['one'];
?>


ammonkc wrote:
Sometimes I use both unset() and session_destroy() just to be safe. I know its redundent, but I know they user is really gonna get logged out that way.

They do different things.
unset($variable) removes the $variable from memory
session_destroy() deletes all session data in such a way that it will not be available in the next request for the same session.
ammonkc
hexkid wrote:

They do different things.
unset($variable) removes the $variable from memory
session_destroy() deletes all session data in such a way that it will not be available in the next request for the same session.

You're right. I wasn't even thinking about that. sorry. You can use unset() to delete a session variable, like $_SESSION[loggedin], but it wont kill the session.
Related topics
php sessions
Sessions
PHP: Sessions and Cookies
php sessions
is mutiple login in windows XP simultaneously possible?
FireFox and Cookies/Sessions
php sessions for different accounts
Sessions are deleted fast.
php SESSIONS question
Need Help : Dreamweaver sessions
Help with Dreamweaver sessions !!!!!!!!
I need some help please!
Sessions
phpBB sessions
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.