PHP会话在索引页面上不起作用

时间:2014-11-15 12:41:02

标签: php session

我的索引页面正在重定向,没有包含PHP会话。当我包含它并输入正确的用户名和密码时,它似乎无法通过重定向传递用户名会话。因此,它将会话视为空,并将我重定向回索引页。

这是我的索引页面

<?php
session_start();
?>


<!DOCTYPE html>
<head> 
    <meta charset = "utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <title> RBH-PAS system </title>

    <link rel = "stylesheet" type ="text/css" href = "pascss/index.css " >
    <script type="text/javascript">
    $(document).ready(function(){
            $(function() {
                $("#login1").on("submit", function(event) {
                    event.preventDefault();

                    $.ajax({
                        url: "login.php",
                        type: "post",
                        data: $(this).serialize(),
                        success: function(d) {
                        var r = d;
                           if(r==2){window.location ="patient.php"; }
                           else if(r==1){window.location="clinician.php";}
                           else{alert("incorrect user details entered");
                                window.location="index.php";
                                }

                        }
                    });
                });
            });
        });
        </script>

</head>

<body>

    <h1><CENTER> WELCOME TO PAS DASHBOARD</CENTER></h1>

    <div id="login">

        <h2><span class="fontawesome-lock"></span>Sign In</h2>


        <form id ="login1" name ="login1" method="post">
            <fieldset>

                <p><label for="username">Username</p>
                <p><input type="text" id ="username" name="username" placeholder="Enter username" required></p>

                <p><label for="password">Password</label></p>
                <p><input type="password" id="password" name="password" placeholder="Enter password" required></p>

                <p><input type="submit" id ="submit" value="Sign In" /></p>

                </fieldset>

</body>
</html> 

这是我的登录页面,我在

中启动会话
<?php

require_once"conn.php";
session_start();


if($_SERVER['REQUEST_METHOD']=='POST'){

//recieve credentials from the user 
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);


//check the variables recieved are not empty
    if($username != '' && $password != ''){

        $sql = "SELECT *FROM staff WHERE username ='$username' AND password = '$password' ";//create an sql statement 
        $result = $conn->query($sql);//run sqlm statement 
        $row = $result->fetch_assoc(); //fetch row of data

            if($result->num_rows == 1 ){//check if user exist as a doctor 



                    if($row["role"]== "clinician"){ //check if user exist as a clinician  
                    $send = 1;
                    echo $send;}

                        else if($row["role"]== "doctor"){ //check if user exist as a clinician  
                        $send = 2;
                        echo $send;}

            }else{//if user doesn't exist
                        $_SESSION["myusername"] = '';
                        $send = 3;
                        echo $send;}
    }
}
else{$conn=null;}

?>

以下是我在其他页面上查看用户登录的方式

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

&GT;

1 个答案:

答案 0 :(得分:2)

您永远不会将$_SESSION['username']设置为登录页面中的任何值。