用户登录时,在我的PHP脚本中设置cookie,但没有设置任何设置



所以我从几天开始就在这里进行搜索,但找不到适当的解决方案,我的问题是我准备好登录系统并有效,但是问题是我试图设置检查我的复选框时,它没有设置cookie。我有三个文件

  1. studentlogin.php(用户界面)
  2. loginprocess.js(包含用于调用login.php的ajax方法的jQuery)
  3. login.php(实际逻辑)

  4. StudentLogin.php

            <?php
                session_start();
                if(isset($_cookie['eno']))
                    {
                        header('location:profile.php');
                        exit();
                    }
        ?>
        <!DOCTYPE html>
        <html lang='en'>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>LMS | Login</title>
        <link rel="stylesheet" href="css/style.css">
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/processlogin.js"></script>
        </head>
        <body>
        <div id="wrap">
        <?php include 'header.php';?>
        <div id="content">
        <section>
            <article id="logform">
                <form action="" id="log">
                    <fieldset>
                    <legend>Login Here</legend>
                        <table>
                        <tr>
                        <td>
                        <label for="eno">Enrollment No:<span class='err' id='err_eno'>*Require</span></label>
                        </td>
                        </tr>
                        <tr>
                        <td>  
                        <input type="text" name="eno" id="eno" value="" placeholder='Enter Enrollment No' required/>  
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <label for="pw">Password:<span class='err' id='err_pw'>*Require</span></label>
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <input type="password" name="pw" id="pw" value="" placeholder='Your Password' required/>
                        </td>
                        <tr>
                        <td>
                        <input type="checkbox" name="cb" checked="checked">
                        <label>Keep me signed in.</label>
                        </td>
                        </tr>
                        <tr>
                        <td colspan='2'>
                        <span id="err"></span>
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <input type="submit" name="submit" id="submit_btn" value="Login" />
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <input type="reset" name="reset" value="Reset" />
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <label>New user?<a href="studentregistration.php">Register here</a></label>
                        </td>
                        </tr>
                        <tr>
                        <td>
                        <label>Forgot your password?<a href="resetpassword.php">Click here</a></label>
                        </td>
                        </tr>
                        </table>
                    </fieldset>
                </form>
            </article>
        </section>
        </div>
        <?php include 'footer.php'; ?>
        </div>
        </body>
        </html>
    
  5. loginprocess.php

     $('#submit_btn').click(function(){
                var dataString = $('#log').serialize();
                //alert (dataString);return false;  
            $.ajax({
                        type:"POST",
                        url:"script/login.php",
                        data:dataString,
                        success:function(x)
                        {
                             if(x =='')
                            {
                                 //Handle Error Here
                                 $('#err').html("<p class='err'>Please provide right combination</p>");
                            }
                            else if(x == '1')
                            {
                                window.location='profile.php';
                            }
                        }
                    });
            return false;
            });
    
  6. login.php

            <?php
        error_reporting(0);
        session_start();
        include "../db/connect.php";
        $password = $_POST['pw'];
        $eno = $_POST['eno'];
        $password_hash = md5($password);
        if(!empty($eno) && !empty($password))
        {
            $query ="select * from users where eno = '$eno' AND password = '$password_hash'";
            $result = $db->query($query);
            if($result->num_rows)
                {
                    $_SESSION['login'] = $_POST['eno'];
                    //echo"<script>alert($_SESSION['login']);</script>"
                    if(isset($_POST['cb']))
                        {
                            setcookie("eno",$eno,time()+3600);  
                        }
                    echo '1';
                }
            else
                {
                    setcookie('eno','',time()-3600);
                    session_regenerate_id();
                    session_destroy();
                    echo '';
                }
        }
        else
        {
            session_regenerate_id();
            session_destroy();
        }
        ?>
    

没有看来没有错,但是您的$ _cookie应该是$ _COOKIE

session_start();
if(isset($_COOKIE['eno']))
{
    header('location:profile.php');
    exit();
}

希望是。

最新更新