Nathan
Nathan

Reputation: 7855

Why is this PHP include not working?

I'm trying to create a logout link that gets included into an authorized area.

The authorized area looks like this:

<?php require_once '../../includes/initialize.php';
if(!$Session->is_logged_in()) { redirect_to('login.php'); }

$user = $User->find_by_id($Session->user_id);

?>


<!-------------- START OF LOGOUT LINK -------------->


<?php
if (isset($_GET['logout']))
{
    $Session->logout($user);
    $Logger->logout($user);
    redirect_to('login.php');
}
?>

<p style="float: right;"><a href="?logout=true">Logout</a></p>


<!-------------- END OF LOGOUT LINK -------------->


<p>Welcome <?php echo $user->first_name; ?>, you are now logged into the admin area.</p>

The above code works just fine!

But when I change it so the logout link is an include, I get an error. This is what I put in my include file called logoutlink.php:

<?php
if (isset($_GET['logout']))
{
    $Session->logout($user);
    $Logger->logout($user);
    redirect_to('login.php');
}
?>

<p style="float: right;"><a href="?logout=true">Logout</a></p>

I then include it into my authorized area as such:

<?php require_once '../../includes/initialize.php';
if(!$Session->is_logged_in()) { redirect_to('login.php'); }

$user = $User->find_by_id($Session->user_id);

?>


<!-------------- START OF LOGOUT LINK -------------->


<?php include '../../includes/logoutlink.php'; ?>


<!-------------- END OF LOGOUT LINK -------------->


<p>Welcome <?php echo $user->first_name; ?>, you are now logged into the admin area.</p>

I get the following errors:

Notice: Undefined variable: Session...

&

Fatal error: Call to a member function logout() on a non-object in...

Can someone explain what I'm missing? Thanks in advance for your help.

Upvotes: 0

Views: 442

Answers (2)

Ayman Safadi
Ayman Safadi

Reputation: 11552

Try setting global $Session; at the top of you logoutlink.php file.

Upvotes: 1

Gopal Nair
Gopal Nair

Reputation: 840

I noticed that you are using $Session, and not $_Session.

If $Session is a user defined variable in initialize.php, then, try putting

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

within your logoutlink.php

Upvotes: 1

Related Questions