Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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 用于更新会话的Jquery登录名不起作用_Php_Jquery_Mysql_Session_Header - Fatal编程技术网

Php 用于更新会话的Jquery登录名不起作用

Php 用于更新会话的Jquery登录名不起作用,php,jquery,mysql,session,header,Php,Jquery,Mysql,Session,Header,我尝试使用Jquery创建一个登录系统,检查是否单击,然后将帖子发送到php文件。该文件获取结果,检查mysql数据库。如果用户名和密码不正确,它会回显。如果登录正确,那么它会在会话中放置一个用户id,并尝试调用header来更新开始文件中的会话,但这不会起作用 我必须手动重新加载页面才能更新会话。这有可能改变吗 这是index.php <?php session_start(); print_r($_SESSION); ?> <html> <head>

我尝试使用Jquery创建一个登录系统,检查是否单击,然后将帖子发送到php文件。该文件获取结果,检查mysql数据库。如果用户名和密码不正确,它会回显。如果登录正确,那么它会在会话中放置一个用户id,并尝试调用header来更新开始文件中的会话,但这不会起作用

我必须手动重新加载页面才能更新会话。这有可能改变吗

这是index.php

<?php
session_start();

print_r($_SESSION);
?>
<html>
<head>
    <title>Site</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="script.js?v=1"></script>
</head>
<body>
    <div id='user'> 
    <?php
        if (isset($_SESSION['user_id'])) echo "Logged in"; else echo "Not logged in"; 
    ?>
    </div>
    <center>
    <div id='login'>
        <div id='login-warning'></div>
        <input type='text' id='username' placeholder='Your name'>
        <input type='text' id='password' placeholder='Your password'>
        <input type='button' id='login-submit' value= 'Send'>
    </div>
    </center>
</body>
</html>
这是login.php文件:

<?php

if (empty($_POST) === false) {

    $U = ereg_replace("[^A-Za-z0-9._]", "", $_POST['u']);
    $P = ereg_replace("[^A-Za-z0-9._]", "", $_POST['p']);

    if(empty($U) === true AND empty($P) === true) {
        $error = " You need to enter a username and password";
    } else if (empty($U) === false AND empty($P) === true) {
        $error = " You need to enter a password";
    } else if (empty($U) === true AND empty($P) === false) {
        $error = " You need to enter a username";
    } else if (empty($U) === false AND empty($P) === false) {


        require_once 'db_connect.php';
        $query = "SELECT * FROM `users` WHERE user='$U'" or die("Error.." . mysqli_error($db));
        $result = mysqli_query($db, $query);

        while($row = mysqli_fetch_array($result)) {
            $id = $row['id'];
            $user = $row['user'];
            $pass = $row['password'];
                            $active = $row['active'];
        }

        if (!$id) {
            $error = "User not found";
        } else {
            if ($P == $pass AND $P != "") {
                if ($active == 1) {
                    session_start();
                    $_SESSION['user_name'] = $user;
                    $_SESSION['user_id'] = $id;
                    session_write_close();
                    //
                    header("Location: index.php");
                    //
                    ob_end_flush();
                    exit;
                } else {
                    $error = "You have not activated your account";
                }
            } else {
                $error = "Incorrect password";
            }
        }
    }

}

echo $error;

?>

您的
index.php
未使用新的注册会话更新,您不能在ajax调用中使用
标题(“location:index.php”)
,您必须通过javascript重新加载页面

login.php:

index.php:

<?php

if (empty($_POST) === false) {

    $U = ereg_replace("[^A-Za-z0-9._]", "", $_POST['u']);
    $P = ereg_replace("[^A-Za-z0-9._]", "", $_POST['p']);

    if(empty($U) === true AND empty($P) === true) {
        $error = " You need to enter a username and password";
    } else if (empty($U) === false AND empty($P) === true) {
        $error = " You need to enter a password";
    } else if (empty($U) === true AND empty($P) === false) {
        $error = " You need to enter a username";
    } else if (empty($U) === false AND empty($P) === false) {


        require_once 'db_connect.php';
        $query = "SELECT * FROM `users` WHERE user='$U'" or die("Error.." . mysqli_error($db));
        $result = mysqli_query($db, $query);

        while($row = mysqli_fetch_array($result)) {
            $id = $row['id'];
            $user = $row['user'];
            $pass = $row['password'];
                            $active = $row['active'];
        }

        if (!$id) {
            $error = "User not found";
        } else {
            if ($P == $pass AND $P != "") {
                if ($active == 1) {
                    session_start();
                    $_SESSION['user_name'] = $user;
                    $_SESSION['user_id'] = $id;
                    session_write_close();
                    //
                    header("Location: index.php");
                    //
                    ob_end_flush();
                    exit;
                } else {
                    $error = "You have not activated your account";
                }
            } else {
                $error = "Incorrect password";
            }
        }
    }

}

echo $error;

?>
if($active == 1) {
   session_start();
   $_SESSION['user_name'] = $user;
   $_SESSION['user_id'] = $id;
   $error = false;
}
$.post('login.php',
       {u: username, p: password},
       function(data) {
           if(!data) {
             location.reload(false)
           } else {
             $('#login-warning').text(data);
           }
       }
)