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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据