PHP logging out
<p>I am doing the Lynda.com learning PHP 2 videos and have run into a problem, in that the instructor seems to have neglected to tell us one of the steps he does in the video. I have uploaded the relevant video here <a href="http://www.youtube.com/watch?v=fFKgAa7RAjo" rel="nofollow">http://www.youtube.com/watch?v=fFKgAa7RAjo</a>  but will also describe the problem. At 6:40 of the video, after logging in to our application, he arrives at public/admin/index.php which has two links on it. one link allows him to "view log file" which takes him to public/admin/logfile.php and the other link allows him to log out. He doesn't tell us how to make these links. I can obviously make a link to view logfile</p>

<pre><code> &lt;a href="logfile.php"&gt;View Logfile&lt;/a&gt;
</code></pre>

<p>but I don't know how to make the link that will log me out, because that will obviously involve some PHP.</p>

<p>I have included below the login.php file, the index.php file (it's redirected to index.php after logging in) and the functions.php file.  Do you know how I would logout from this?</p>

<p>This is the login.php file</p>

<pre><code>&lt;?php

require_once("../../includes/initialize.php");

if($session-&gt;is_logged_in()){
    redirect_to("index.php");
}

//Remember to give your form's submit tag a name="submit" attribute
if (isset($_POST['submit'])) {//Form has been submitted.

$username = trim($_POST['username']);
$password = trim($_POST['password']);

//Check database to see if username/password exist

$found_user = User::authenticate($username, $password);

if ($found_user) {
    $session-&gt;login($found_user);
    log_action('Login', "{$found_user-&gt;username} logged in.");
    redirect_to("index.php");
} else {
    //username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
} 
} else {//Form has not been submitted.
    $username = "";
    $password = "";
    }
?&gt;

&lt;?php include_layout_template('admin_header.php'); ?&gt;

        &lt;h2&gt;Staff Login&lt;/h2&gt;
        &lt;?php echo output_message($message); ?&gt;

        &lt;form action="login.php" method="post"&gt;
            &lt;table&gt;
                &lt;tr&gt;
                    &lt;td&gt;Username:&lt;/td&gt;
                    &lt;td&gt;
                        &lt;input type="text" name="username" maxlength="30" value="&lt;?php
                        echo htmlentities($username); ?&gt;" /&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Password:&lt;/td&gt;
                    &lt;td&gt;
                        &lt;input type="password" name="password" maxlength="30" value="&lt;?php
                        echo htmlentities($password); ?&gt;" /&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td colspan="2"&gt;
                        &lt;input type="submit" name="submit" value="login" /&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/form&gt;
        &lt;/div&gt;
        &lt;?php include_layout_template('admin_footer.php'); ?&gt;
</code></pre>

<p>Functions.php</p>

<pre><code>&lt;?php

function strip_zeros_from_date( $marked_string=""){
//first remove the marked zeros
$no_zeros = str_replace('*0', '', $marked_string);
//then remove any remaining marks
$cleaned_string = str_replace('*', '', $no_zeros);
return $cleaned_string;

}

function redirect_to( $location= NULL) {
    if($location != NULL) {
    header("Location: {$location}");
    exit;
    }

}

function output_message($message=""){
if (!empty($message)) {
return "&lt;p class=\"message\"&gt;{$message}&lt;/p&gt;";
} else {
    return "";
    }
}

function __autoload($class_name) {
  $class_name = strtolower($class_name);
  $path = LIB_PATH.DS."{$class_name}.php";
  if(file_exists($path)){
  require_once($path);
  } else {
  die("The file {$class_name}.php could not be found.");
  }
}

function include_layout_template($template=""){
include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template);
}

function log_action($action, $message=""){
$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
$new = file_exists($logfile) ? false : true;
if($handle = fopen($logfile, 'a')) { //apppend
$timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
$content = "{$timestamp} | {$action}: {$message}\n";
fwrite($handle,$content);
fclose($handle);
if($new) {chmod($logfile, 0755); }
} else {
 echo "Could not open log file for writing.";
}
}

?&gt;
</code></pre>

<p>Index.php</p>

<pre><code>&lt;?php

require_once('../../includes/initialize.php');

if (!$session-&gt;is_logged_in()) { redirect_to("login.php"); }
?&gt;

&lt;?php include_layout_template('admin_header.php'); ?&gt;

        &lt;h2&gt;Menu&lt;/h2&gt;

        &lt;/div&gt;


&lt;?php include_layout_template('admin_footer.php'); ?&gt;
</code></pre>

<p>Update</p>

<p>Initialize.php</p>

<pre><code>&lt;?php

//Directory_separator is a PHP pre-defined constant
// (\ for windows, / for Unix)

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null : 
 define('SITE_ROOT', DS.'hsphere'.DS.'local'.DS.'home'.DS.'c263430'.DS.'quoralist.com');
// define('SITE_ROOT', realpath(dirname(__FILE__).'/../'));

 //echo SITE_ROOT."&lt;br/&gt;";

 defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
// die(LIB_PATH);

 //echo LIB_PATH."&lt;br/&gt;";

require_once(LIB_PATH.DS."config.php");
require_once(LIB_PATH.DS."functions.php");
require_once(LIB_PATH.DS."session.php");
require_once(LIB_PATH.DS."database.php");
require_once(LIB_PATH.DS."database_object.php");
require_once(LIB_PATH.DS."user.php");

//echo("You die here");

?&gt;
</code></pre>

<p>User.php</p>

<pre><code>&lt;?php

require_once(LIB_PATH.DS.'database.php');

class User extends DatabaseObject{

protected static $table_name="users";
public $id;
public $username;
public $password;
public $first_name;
public $last_name;

public function full_name() {
if(isset($this-&gt;first_name) &amp;&amp; isset($this-&gt;last_name)) {
return $this-&gt;first_name . " " . $this-&gt;last_name;
} else {
  return "";
}
}

public static function authenticate($username="",$password="") {
global $database;
$username = $database-&gt;escape_value($username);
$password = $database-&gt;escape_value($password);
$sql = "SELECT * FROM users ";
$sql .= "WHERE username = '{$username}' ";
$sql .= "AND password = '{$password}' ";
$sql .= "LIMIT 1";
$result_array = self::find_by_sql($sql);
return !empty($result_array) ? array_shift($result_array) : false; 

}

//common database methods

public static function find_all(){
return self::find_by_sql("SELECT * FROM ".self::$table_name);

}

public static function find_by_id($id=0) {
global $database;
$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id={$id} LIMIT 1");
return !empty($result_array) ? array_shift($result_array) : false; 
}

public static function find_by_sql($sql=""){
global $database;
$result_set = $database-&gt;query($sql);
$object_array = array();
while ($row = $database-&gt;fetch_array($result_set)) {
$object_array[] = self::instantiate($row);
}
return $object_array;
}




private static function instantiate($record){

$object = new self;
//$object-&gt;id = $record['id'];
//$object-&gt;username = $record['username'];
//$object-&gt;password = $record['password'];
//$object-&gt;first_name = $record['first_name'];
//$object-&gt;last_name = $record['last_name'];

foreach($record as $attribute=&gt;$value) {
if($object-&gt;has_attribute($attribute)) {
$object-&gt;$attribute = $value;
}
}
return $object;
}

private function has_attribute($attribute) {
$object_vars = get_object_vars($this);
return array_key_exists($attribute, $object_vars);
}

}




?&gt;
</code></pre>

<p>Session.php</p>

<pre><code>&lt;?php


class Session {

    private $logged_in=false;
    public $user_id;

    function __construct() {
    session_start();
    $this-&gt;check_login();
    if($this-&gt;logged_in){
    //actions to take right away if user is logged in
    } else {
    //actions to take right away if user is not logged in
    }
    }

    public function is_logged_in() {
    return $this-&gt;logged_in;
    }

    public function login($user) {
    //database should find user based on username/password
    if($user){
    $this-&gt;user_id = $_SESSION['user_id'] = $user-&gt;id;
    $this-&gt;logged_in = true;
    }
    }

    public function logout(){
    unset($_SESSION['user_id']);
    unset($this-&gt;user_id);
    $this-&gt;logged_in = false;
    }

    private function check_login(){
    if(isset($_SESSION['user_id'])){
    $this-&gt;user_id = $_SESSION['user_id'];
    $this-&gt;logged_in = true;
    } else {
     unset($this-&gt;user_id);
     $this-&gt;logged_in = false;
    }
    }
}

$session = new Session();

?&gt;
</code></pre>
