5

Good morning.

Building a web app as to manage automobiles as you can see in this image.

What you see in the previous image is the file index.php, configured to show different things if the user has done login or not:

<body>
<div class="container">
    <h2>Welcome to the Automobiles Database</h2>
    <?php
if ( isset($_SESSION['error']) ) {
    echo('<p style="color: red;">'.htmlentities($_SESSION['error'])."</p>\n");
    unset($_SESSION['error']);
}
if ( isset($_SESSION['success']) ) {
    echo('<p style="color: green;">'.htmlentities($_SESSION['success'])."</p>\n");
    unset($_SESSION['success']);
}
?>


    <!-- without login -->
    <?php if(!isset($_SESSION['name'])) {
        echo '<p><a href="login.php">Please log in</a></p>';
        echo '<p>Attempt to <a href="add.php">add data</a> without logging in</p>';
    } ?>


    <!-- with login -->
    <?php if(isset($_SESSION['name'])) {
        echo '<table border="1"><thead><tr><th>Make</th><th>Model</th><th>Year</th><th>Mileage</th><th>Action</th></tr></thead>';
        $smtp = $pdo->query("SELECT autos_id, make, model, year, mileage FROM autos ORDER BY make");
        while ($row = $smtp->fetch(PDO::FETCH_ASSOC)) {
            echo("<tr><td><b>");
            echo($row['make']);
            echo("</b></td><td><b>");
            echo($row['model']);
            echo("</b></td><td><b>");
            echo($row['year']);
            echo("</b></td><td><b>");
            echo($row['mileage']);
            echo("</b></td><td><b>");
            echo("<a href=\"edit.php?autos_id=".$row["autos_id"]."\">Edit</a> / <a href=\"delete.php?autos_id=".$row["autos_id"]."\">Delete</a>");
            echo("</b></td><tr>\n");
        }
        echo '</table>';
        echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="?logout">Logout</a></p>';
        if(isset($_GET['logout'])) {
        session_unset();
        }
    } ?>



</div>
</body>

The problem i'm facing has to do with the link "Logout", which is as following:

echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="?logout">Logout</a></p>';

If i click once, this is the result i get.

This logs out the user as expected, but i want it to reach this page right away (which is the index.php without login) and to achieve this i'm having to click twice in the link...

Logout.php:

session_start();
unset($_SESSION['name']);
unset($_SESSION['user_id']);
header('Location: index.php');

How can I do it?

BP

Bárbara Peres
  • 585
  • 1
  • 9
  • 26

3 Answers3

4

I've changed Logout.php to:

<?php
session_start();
unset($_SESSION['name']);
unset($_SESSION['user_id']);
header('Location: index.php');
?>

and the Logout link to:

echo '<p><a href="add.php">Add New Entry</a></p> <p><a href="logout.php">Logout</a></p>';

Now works fine!

Bárbara Peres
  • 585
  • 1
  • 9
  • 26
0

Replace your "?logout" to the url of the logout.php page hope it may solve the problem

<html>
'<p><a href="add.php">Add New Entry</a></p> <p><a href="?logout">Logout</a></p>'; ?</html>
Atmiya Kolsawala
  • 475
  • 4
  • 12
0

I solved this adding session_destroy(); at the end

<?php
session_start();
unset($_SESSION['loggedin']);
unset($_SESSION['email']);
session_destroy();
?>
Spinstaz
  • 287
  • 6
  • 12