Javascript 如果选中checkox,如何让用户保持日志记录

Javascript 如果选中checkox,如何让用户保持日志记录,javascript,php,jquery,ajax,jquery-cookie,Javascript,Php,Jquery,Ajax,Jquery Cookie,我正在使用Bootstrap3中的模式对话框创建一个登录系统,并且我添加了一个功能,如果用户选中模式对话框中的复选框,则可以保持用户登录。 我想让用户保持登录状态,即使他/她刷新或关闭浏览器 index.php <div class="container" id="myLogin"> <div class="row"> <div class="modal fade" id="myModal" tabindex="-1" role="

我正在使用Bootstrap3中的模式对话框创建一个登录系统,并且我添加了一个功能,如果用户选中模式对话框中的复选框,则可以保持用户登录。 我想让用户保持登录状态,即使他/她刷新或关闭浏览器

index.php

<div class="container"  id="myLogin">
    <div class="row">
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h5 class="modal-title">PLEASE ENTER YOUR EMAIL ADDRESS AND PASSWORD TO LOG IN.</h5>
                        </div>
                        <div class="modal-body">
                        <div id="show" class="lalert lalert-warning"></div> 
                            <div class="form-horizontal">
                                <div class="form-group">
                                    <label for="email" class="col-sm-2 control-label">Email</label>
                                    <div class="col-md-9">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-envelope-o fa-fw"></i></span>
                                            <input type="text" name="lemail" id="lemail" value="<?php echo $unm ?>" class="form-control" placeholder="Enter Email Address..." />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="password" class="col-sm-2 control-label">Password</label>
                                    <div class="col-md-9">
                                        <div class="input-group">
                                            <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
                                            <input type="password" name="lpassword" id="lpassword" value="<?php echo $pwd ?>" class="form-control" placeholder="Enter Password..." />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <input type="checkbox" name="chkbox" value="staylogged" class="checkbox-inline" />
                                        <label>  &nbsp; Keep me logged in</label>  &nbsp; <b>|</b>
                                        <a href="" style="text-decoration:none">  &nbsp; Forgot your password?</a>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        <button type="submit" id="login" name="login" class="btn btn-primary"><span class="glyphicon glyphicon-user"></span> Login</button>
                                        <button type="button" class="btn btn-info show-page modal-btn" data-page="Signup"  data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-list-alt"></span> Register</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </div>
</div>
php:


请确保您的cookies是否保存

另一件事是确定过期时间。 在PHP中设置coockies,如:

setcookie(
    'pageVisits',               // Name of the cookie, required
    $visited,                   // The value of the cookie
    time()+7*24*60*60,          // Expiration time, set for a week in the future
    '/',                        // Folder path the cookie will be available for
    'demo.tutorialzine.com'     // Domain to which the cookie will be bound
);
阅读JS中的coockies:

$(document).ready(function(){

    // Getting the kittens cookie:
    var str = $.cookie('pageVisits');

    // str now contains the value of $visited
});

为了让会话启动;工作,, 您需要将变量添加到$\u会话数组中。将PHP代码更改为:

<?php
//Start the session
session_start();
//ALWAYS check if a $_POST variable is set before using it.
if(isset($_POST['lemail'])){
    $unm = $_POST['lemail'];
} else {
   //Error Code Goes Here
}
if(isset($_POST['lpassword'])){
    $pwd = $_POST['lpassword'];
} else {
   //Error Code Goes Here
}
//Set SESSION vars
$_SESSION["unm"] = $unm;
$_SESSION["pwd"] = $pwd;

那么什么不起作用了具体来说,如果您对会话感兴趣,您可能希望以某种方式使用全局$\u SESSION变量。。。
$(document).ready(function(){

    // Getting the kittens cookie:
    var str = $.cookie('pageVisits');

    // str now contains the value of $visited
});
<?php
//Start the session
session_start();
//ALWAYS check if a $_POST variable is set before using it.
if(isset($_POST['lemail'])){
    $unm = $_POST['lemail'];
} else {
   //Error Code Goes Here
}
if(isset($_POST['lpassword'])){
    $pwd = $_POST['lpassword'];
} else {
   //Error Code Goes Here
}
//Set SESSION vars
$_SESSION["unm"] = $unm;
$_SESSION["pwd"] = $pwd;
session_start();
if(isset($_SESSION["unm"]) && isset($_SESSION["pwd"])){
    $loggedIn = true;
    //At this point you can also check your database if they are actually a user for extra security
} else {
    $loggedIn = false;  
}