注销要求再次单击两次

时间:2018-06-26 07:12:32

标签: php session

早上好。

构建一个用于管理汽车的网络应用,如您所见in this image

在上图中看到的是文件index.php,该文件配置为显示用户是否登录的不同内容:

<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>

我面临的问题与链接“注销”有关,如下所示:

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

如果我单击一次,this is the result i get

这将按预期注销用户,但是我希望它立即reach this page(这是没有登录的index.php),要实现这一点,我必须在链接中单击两次...

Logout.php:

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

我该怎么办?

血压

3 个答案:

答案 0 :(得分:1)

我已将 Logout.php 更改为:

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

和“登出”链接到:

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

现在工作正常!

答案 1 :(得分:0)

将您的“?logout”替换为logout.php页面的URL,希望它可以解决问题

android.view.InflateException
Binary XML file line #75: Error inflating class 
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at mypageName.getView(MyClass.java:165)

答案 2 :(得分:0)

我在最后添加 session_destroy(); 解决了这个问题

<?php
session_start();
unset($_SESSION['loggedin']);
unset($_SESSION['email']);
session_destroy();
?>