Pergunta

I want to include a link to my logout.php which includes code to destroy the session inside my php on this page how would I do this?

<?php
    if (isset($_SESSION['username'])) {
        echo 'Welcome';
        "<a href="logout.php">Logout</a>";
    } else {
        include 'loginform.php';
        echo 'Please Log In';
    }
?>
Foi útil?

Solução

Can you try this code, if the SESSION is empty:

<?
session_start();
if (empty($_SESSION['username'])){
    echo 'Please Log In';
    include 'loginform.php';
}else{
echo 'Welcome';
?>
<a href="index.php">Home</a>
<?
}
?>

Outras dicas

Before you use any $_SESSION variable, you have to call session_start() The logout should first open the session by calling session_start(), then destroy it by calling session_destroy

You aren't currently telling PHP to display the link to the logout page. It either needs to be included in the echo or outputted elsewhere. For example;

if (isset($_SESSION['username']))   {
    echo 'Welcome';
    echo '<a href="logout.php">Logout</a>';
}
else {
    include 'loginform.php';
    echo 'Please Log In';
}

In your logout.php put this code ,

<?php
 session_start();
 session_destroy();
 ?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top