dongque3797 2017-07-28 18:34
浏览 12
已采纳

PHP登录页面不会重定向到下一页[重复]

This question already has an answer here:

I am writing a login/register system for my first php project and I am facing problems in login.php. Here is my login.php:

<?php
   include("db.php");
   session_start();
   if(isset($_SESSION['login_user'])){
      header("Location: welcome.php");
   }

   $error = '';
   if (isset($_POST['submit'])) {
      if (empty($_POST['username']) or empty($_POST['password'])) {
           $error = "Please enter your login details";
     } else {

        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $password = md5($_POST['password']);



        $query = mysqli_prepare($conn, "SELECT password FROM workers WHERE user_name=?");
        mysqli_stmt_bind_param($query,"s",$username);
        mysqli_stmt_execute($query);
        mysqli_stmt_bind_result($query,$pass);
        if(mysqli_stmt_fetch($query)){
            if ($password == $pass){
                header('Location:welcome.php');
                $_SESSION['login_user'] = $username;

            }else {
                $error = "You typed the wrong password";
                unset($username, $password);
            }
        }else{
            $error = "User Login doesn't exists";
            unset($username,$password);
        }

    }
}
?>

The problem is that after entering existing username/password it just refreshes the page instead of heading to the welcome.php. I checked with an incorrect username/password and it displayed an error as expected, so I don't think the problem is in the MySQL queries. How can I find the problem with a code and explain why it happened?

</div>
  • 写回答

2条回答 默认 最新

  • duange2971 2017-07-28 18:46
    关注

    no code should be executed after header was sent.

    $_SESSION['login_user'] = $username;
    header('Location:welcome.php');
    exit();
    

    well, and consider those comments good people took time to post ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数