duane9322 2018-05-18 04:26
浏览 87
已采纳

成功登录后重定向不起作用

I've creating a login form using PHP but I'm facing a problem where my home.php doesnt show its contents after loggin-in successfully. Error handling are showing, like both fields are required, Incorrect username/pass. But if I enter correct credentials then, it doesn't show my home.php.

login.php

<?php

session_start();
include("connection.php"); //Establishing connection with our database

$error = ""; //Variable for storing our errors.
if(isset($_POST["submit"])) {
    if($_POST["username"]== '' || $_POST["password"]=='') {
        $error = "Both fields are required.";
    } else {
        // Define $username and $password
        $username=$_POST['username'];
        $password=$_POST['password'];

        //Check username and password from database
        $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
        $result=mysqli_query($db,$sql);
        $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

        if(mysqli_num_rows($result) == 1) {
            $_SESSION['username'] = $login_user; // Initializing Session
            header("location: home.php"); // Redirecting To Other Page
        } else {
            $error = "Incorrect username or password.";
        }
    }
}

?>

home.php

<?php
    include("check.php"); 
?>

<!doctype html>
<html>
    <head>
    <meta charset="utf-8">
        <title>Home</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
    </head>
    <body>
        <h1 class="hello">Hello, <em><?php echo $login_user;?>!</em></h1>
        <br><br><br>
        <a href="logout.php" style="font-size:18px">Logout?</a>
    </body>
</html>

check.php

<?php
    include('connection.php');
    session_start();
    $user_check=$_SESSION['username'];

    $sql=mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");

    $row=mysqli_fetch_array($sql,MYSQLI_ASSOC);
    $login_user=$row['username'];

    if(!isset($user_check)) {
        header("Location: index.php");
    }
?>
  • 写回答

2条回答 默认 最新

  • doulangyu9636 2018-05-18 04:35
    关注

    Two important points

    first: Your codes will always continue to run and will not stop with the change of location. It means you should use

    exit;
    
    // or
    
    die();
    

    to finish running the program.

    second: for redirect in php you should use something like this:

    /* Redirect to a different page in the current directory that was requested */
    $host  = $_SERVER['HTTP_HOST'];
    $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $page  = 'home.php';
    header("Location: http://$host$uri/$page");
    exit;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建