Jump to content

session destroy


mcloan

Recommended Posts

I am in the process of learning php and I am trying to get some concepts.  Below is some code for a logout script in the book I am reading.  I understand it all but one section.  I do not understand the line  if ($result_dest).  Can someone explain what is this check for?  The variable $result_dest was set to session_destroy(); so I would think it would have no value.  Also I am a little confused by an if statment without a condition.  For istance this is If ($result_dest), but normally I see ifs with a condition like if (!empty($result_dest)) or something like that.  Can some also explain what if statement without a condition is doing? 



[code]
<?php

// include function files for this application
require_once('bookmark_fns.php');
session_start();
$old_user = $_SESSION['valid_user']; 
// store  to test if they *were* logged in
unset($_SESSION['valid_user']);
$result_dest = session_destroy();

// start output html
do_html_header('Logging Out');

if (!empty($old_user))
{
  if ($result_dest)
  {
    // if they were logged in and are now logged out
    echo 'Logged out.<br />';
    do_html_url('login.php', 'Login');
  }
  else
  {
  // they were logged in and could not be logged out
    echo 'Could not log you out.<br />';
  }
}
else
{
  // if they weren't logged in but came to this page somehow
  echo 'You were not logged in, and so have not been logged out.<br />';
  do_html_url('login.php', 'Login');
}

do_html_footer();

?>


[/code]
Link to comment
Share on other sites

Well basically the session_destroy() will end a session (although not unset any global session variables or session cookies) and if, when you call this method, it is successful in ending the session, it will return TRUE - or FALSE if it can't for some reason.

So that line that you are asking about, is actually saying "if the variable $result_dest is equal to TRUE, then...". PHP is intelligent enough to determine the condition on its own without you writing it all out. Conversely, if you inlude an exclamation mark before the variable, then it is as if you are saying "if the variable $result_dest is equal to FALSE, then...". You could of course do it the long way round and type

[code]<?php
if ($result_dest == TRUE) {
}
?>
[/code]
but then why would you?
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.