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

时间:2017-04-21 11:49:45

标签: php html

尝试从当前页面执行注销。基本上,如果您点击退出,它将再次显示登录表单。我不想调用这样做的页面。当我执行" Logout"按钮,它仍然显示用户登录,但如果我点击"退出"按钮第二次正常工作。或者如果我刷新页面也可以。似乎最初提交" Logout"不刷新。



<?
  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>
&#13;
&#13;
&#13;

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

你需要在html页面渲染之前检查注销功能,所以在logout()函数之后在$ _POST中添加注销键,如

<?
    ..........    
    $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 :(得分:0)

从技术角度来看,用户已注销(因为会话变量已被销毁),但页面显示的内容不同。您需要在页面呈现之前将注销部分置于顶部,否则页面不知道用户已注销:

<?
  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 (isset($_POST['logout'])) {
    if ($_POST['logout'] == 'logout') {
logOut();
    } else if ($_POST['logout'] != 'logout') {
    }

}

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> 
</div>
</div>
<?php } ?>
</body>
<script type='text/javascript'>
      document.loginform.username.focus();
      document.loginform.username.select();
</script>

您也可以在调用注销功能后重新加载页面,但在我看来这不是很好。您始终必须记住,首先必须在呈现页面之前执行所有逻辑。