Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要登录两次才能重定向php和mysql_Php_Redirect_Mysqli_Login - Fatal编程技术网

需要登录两次才能重定向php和mysql

需要登录两次才能重定向php和mysql,php,redirect,mysqli,login,Php,Redirect,Mysqli,Login,嗨,我试图在用户登录后将其重定向到其他页面,但在第一次登录时未重定向,在第二次登录时重定向。这是我的密码 index.php <?php include "config.php"; ?> <?php session_start();?> <?php if(isset($_POST['login'])) { $username=$_POST['user']; $password=$_POST['pass'];

嗨,我试图在用户登录后将其重定向到其他页面,但在第一次登录时未重定向,在第二次登录时重定向。这是我的密码

index.php

<?php include "config.php"; ?>
<?php session_start();?>
<?php
    if(isset($_POST['login']))
    {
        $username=$_POST['user'];
        $password=$_POST['pass'];
        $username=mysqli_real_escape_string($conn,$username);
        $password=mysqli_real_escape_string($conn,$password);
        $query="SELECT * FROM user WHERE username = '{$username}' and password='{$password}'";
        $select_user_query=mysqli_query($conn,$query);
        if(!$select_user_query)
        {
            die("Connection failed".mysqli_error($conn));
        }
        while($row=mysqli_fetch_array($select_user_query))
        {
            $db_user_id=$row['id'];
            $_SESSION['username']=$row['username'];
            $db_user_email=$row['email'];
            $db_user_password=$row['password'];
        }
        if($username ===  $_SESSION['username'] && $password ===$db_user_password)
        {
            echo "<script type=\"text/javascript\">
                 window.location = \"titles.php\"
            </script>";
        }else{
            echo "<p style='color:#FF7B81'> enter correct username and password <p>";
        }
    }
?>
<?php
    error_reporting(0);
    ini_set('display_errors', 0);
    if(isset($_SESSION['username'])){
        echo "<script type=\"text/javascript\">
            window.location = \"user_details.php\"
        </script>";
        /* header("Location: user_details.php");*/
    }
?>


将titles.php更改为:

<?php
session_start(); 
if(!isset($_SESSION['username'])) {
    header("Location:index.php");
    exit;
} else {
    header("Location:user_details.php");
    exit;
}

那么,第一次尝试登录时的输出是什么呢?它被刷新并停留在index.php中,只有在我再次登录之后,它才被重定向使用php重定向而不是javascript重定向
if($username ===  $_SESSION['username'] && $password ===$db_user_password)
{
    header("Location:user_details.php");
    exit;
}