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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?