doudou6050 2017-04-21 11:49
浏览 99
已采纳

注销 - 必须按两次按钮才能工作?

Trying to perform a logout from current page. Basically if you hit the logout, it will just show the login form again. I do not want to call a page doing this. When I execute the "Logout" button, it still shows the user logged in, but if I hit the "Logout" button a second time it works correctly. Or if I refresh the page it works also. Just seems the initial submitting of "Logout" does not refresh.

<?
  session_start();

$subtitle="Login";
ob_start();
// require("header2.php");
//Get any form data.
$football->WhoOnlineDelete;

$username=$_POST['username'];
$password=$_POST['password'];
global $conn;
$conn = mysqli_connect("localhost","","", "");

function logOut()
{
    unset($_SESSION['user']);
    unset($_SESSION['uname']);
    session_destroy();
    ob_start();
    exit();
}

if ($_POST)
{
//Make sure cookies are enabled.
 // if ($_COOKIE["football"]=="")
//  {

//      $football->ErrorMessage("You must use a browser that supports cookies and<br> have them enabled in order to access this site.");
//  }
//    else
//  {
//Check input.
    if ($username=="")
    {
                echo "Please enter a username.";
    }
        elseif ($password=="")
    {
                echo"Please enter your password.";
    }
      else
    {
//Verify the password and redirect to default page if correct.
     $sql=mysqli_query($conn, "select * from phpfb_users where user = '".$username."'");
         $row = mysqli_fetch_object($sql);
         $rows = mysqli_num_rows($sql);
      if($rows == 0)
      {
                echo "User '".$username."' not found.";
      }
      elseif (md5($password) != $row->password)
      {
                echo "Incorrect password, please reenter.";
      }
       else
      {

        $user=$row->user;
        if ($row->name =="") {
        $uname=$row->user;
        } else {
        $uname=$row->name;
        }
        $_SESSION['uname'] = $uname;
        $_SESSION['user'] = $user;
        header("Location: loginJERRY.php");
      }

    }


  }


//}
  else
{
//Set test cookie.
  setcookie("football","peanutbutter",0,"/",$football->domain,0);
}
?>
<div>
<div style="display:block;margin:0px auto;" background-color="lightblue;">
<?php if(empty($_SESSION["user"])) { ?>
<form name="loginform" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">

    <div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>  
    <div class="field-group">
        <div><label for="login">Username: </label>
        <input name="username" type="text" class="input-field">
    
<label for="password">Password:</label>
        <input name="password" type="password" class="input-field"> 
<input type="submit" name="login" value="Login" class="form-submit-button"></span></div>
    </div>       
</form>
<?php 
} else { 
    $result = mysqli_query($conn,"SELECT * FROM phpfb_users WHERE user='".$username."' and password = '".$password."'");
    $row  = mysqli_fetch_array($result);
?>
<br><br>
<form action="" method="post" id="frmLogout">
<div class="member-dashboard">Welcome <?php echo $user; ?>, You have successfully logged in!
 <input type="submit" name="logout" value="logout" class="logout-button"></div>
</form> 
<?
if (isset($_POST['logout'])) {
    if ($_POST['logout'] == 'logout') {
logOut();
    } else if ($_POST['logout'] != 'logout') {
    }
    
}
?>
</div>
</div>
<?php } ?>
</body>
<script type='text/javascript'>
      document.loginform.username.focus();
      document.loginform.username.select();
</script>

Am I missing something?

</div>
  • 写回答

2条回答 默认 最新

  • doufei8250 2017-04-21 11:55
    关注

    You need to check logout functionality before you html page renders, so add you logout key in $_POST after logout() function like,

    <?
        ..........    
        $conn = ....
    
        function logOut()
        {
            unset($_SESSION['user']);
            unset($_SESSION['uname']);
            session_destroy();
            ob_start();
            exit();
        }
        if (isset($_POST['logout']) && $_POST['logout'] == 'logout') {
             logOut();
        } // not required else part here
    
        if($_POST) {
             ....
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀