I have a login script and a functions.php script to check if the session username and cookie are set. When the user logs in, if they select the remember me, the cookie is supposed to set. But the problem is that, the script works, but the cookie doesn't set so the user is not being logged in. I've searched through so many topics on here and tried as many solutions as possible, but I still either get the same result or end up giving me more errors.

> 
> 
> ```
>                           if (isset($_POST['rem']) && $_POST['rem'] == 'on') > {
>                               setcookie('MCLatestUser', $token, 31622400, > '/');
>                                 session_regenerate_id(true);
>                             }
> 
> ```
> 
> This is the part of the code that should set the cookie if remember is checked.
> 
> 
> 

Log.php (Since I use an ajax login script, the url is set to this):

```
<?php
include_once 'db.php';
include_once 'functions.php';
error_reporting(-1);

if(isset($_POST['email'])) {
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
    $password = $mysqli->real_escape_string($_POST['password']);
    try {
        $check = mysqli_query($mysqli, "SELECT * FROM users WHERE email='$email'");
        $res = mysqli_num_rows($check);
        if($res > 0) {
            while($run = mysqli_fetch_array($check, MYSQLI_ASSOC)) {
                $blocked = $run['blocked'];
                $deactivated = $run['deactivated'];
                $paused = $run['paused'];
                $verified = $run['verified'];
                $rank = $run['rank'];
                $token = $run['token'];
                $pass = $run['password'];
                $pbackup = $run['pbackup'];
                if($verified == 'true') {
                    if($blocked == 'true') {
                        echo 'Looks like your account was blocked. If you think this is an error, please contact an admin via support@mclatest.com';
                    } else if($deactivated == 'true') {
                        echo 'Looks like your account has been deactivated. If you think this is an error, please contact an admin via support@mclatest.com';
                    } else if($paused == 'true') {
                        echo 'Looks like your account is frozen. If you think this is an error, please contact an admin via support@mclatest.com';
                    } else {
                        if(password_verify($password, $pass)) {
                            $timestamp = time();
                            // Authenticated, set session variables
                            $_SESSION['username'] = $run['username'];
                            if (isset($_POST['rem']) && $_POST['rem'] == 'on') {
                                setcookie('MCLatestUser', $token, 31622400, '/');
                                session_regenerate_id(true);
                            }
                            $sql = mysqli_query($mysqli, "UPDATE users SET Ip = '$ipaddress', login_ip = '$ipaddress', latest_login_date = '$now', login_date = '$date', login_time = '$time', login_day = '$day', login_month = '$month', login_year = '$year', status = '$timestamp' WHERE email = '$email'");
                            if($sql) {
                                echo "Success!";
                            } else {
                                echo "Error login in";
                            }
                            // do stuffs
                        } else if(password_verify($password, $pbackup)) {
                            $timestamp = time();
                            // Authenticated, set session variables
                            $_SESSION['username'] = $run['username'];
                            if (isset($_POST['rem']) && $_POST['rem'] == 'on') {
                                setcookie('MCLatestUser', $token, 31622400, '/');
                                session_regenerate_id(true);
                            }
                            $sql = mysqli_query($mysqli, "UPDATE users SET Ip = '$ipaddress', login_ip = '$ipaddress', latest_login_date = '$now', login_date = '$date', login_time = '$time', login_day = '$day', login_month = '$month', login_year = '$year', status = '$timestamp' WHERE email = '$email'");
                            if($sql) {
                                echo "Success!";
                            } else {
                                echo "Error login in";
                            }
                            // do stuffs
                        } else {
                            echo "<h4 style='font-weight:bold;font-family:arial;margin:8px'>Your password is incorrect, please try again. If you still get this error after using your backup password, please <a href='https://mclatest.com/community/reset.php?r=password'>reset</a> your password</h4>";
                        }
                    }
                } else {
                    echo "<h4 style='font-weight:bold;font-family:arial;margin:8px'>You need to verify your account. Please click this link to <a href='https://mclatest.com/community/confirm.php?email=".$email."&token=".$token."'>verify your account</a></h4>";
                }
            }
        } else {
            echo 'No records of that user have been found!';
        }
    } catch(PDOException $e){
        echo $e->getMessage();
    }

} else {
    echo "Invalid email";
}

```

Login.php (the html and ajax form):

```
<form id="login_form" style="text-align:center" method="post">
                                <script>
                                    $(document).ready(function() {
                                        $("#login").click(function(e) {
                                            e.preventDefault();
                                            var email = $("#email").val();
                                            if (email = "") {
                                                $("#error_msg").html("<h4>Email cannot be empty</h4>");
                                            } else {
                                                var data = $("#login_form").serialize();
                                                $.ajax({
                                                    type: "POST",
                                                    url: "../inc/log.php",
                                                    data: data,
                                                    beforeSend: function() {
                                                        $("#error_msg").fadeOut();
                                                        $("#login").val('sending ...');
                                                    },
                                                    success: function(data) {
                                                        if (data == "Success!") {
                                                            // alert("Works"); //for testing purposes
                                                            window.location.href = "index.php";
                                                        } else {
                                                            $("#error_msg").fadeIn(1000, function() {
                                                                $("#error_msg").html('<div style="border:1px solid: red; background:rgba(255,0,0,0.9;)">'+data+'!</div>');
                                                                $("#login").val('Login');
                                                            });
                                                        }
                                                    },
                                                    error: function(data) {
                                                        alert("Process Failed!");
                                                    }
                                                });
                                                return false;
                                            }
                                        });
                                    });
                                </script>
                                <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label ">
                                    <label for="input_email" class="mdl-textfield__label">Email</label>
                                    <input type="email" name="email" class="mdl-textfield__input" maxlength="255" id="input_email" />
                                </div>
                                <br>

                                <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
                                    <label for="input_password" class="mdl-textfield__label">Password</label>
                                    <input type="password" name="password" class="mdl-textfield__input" maxlength="255" id="input_password" />
                                </div>
                                <br>

                                <label style="width:auto !important" for="remember_me" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" >
                                    <input name="rem" type="checkbox" id="remember_me" class="mdl-checkbox__input" checked />
                                    <span class="mdl-checkbox__label">Stay logged in?</span>
                                </label>
                                <br>

                                <nav style="width:auto !important;display:-webkit-box;-webkit-box-pack:center" class="mdl-navigation">
                                    <a class="mdl-navigation__link" href="forgot.php?ftype=password">Forgot Password?</a>&nbsp; | &nbsp;
                                    <a class="mdl-navigation__link" href="register.php">Register?</a>
                                </nav>
                                <br>

                                <input type="submit" id="login" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" name="login" value="Login"/>
                            </form>

```

functions.php (this is the portion of the script to check the session and cookie variables):

```
function loggedIn() {
    if (isset($_SESSION['username']) && !empty($_SESSION['username']) && isset($_COOKIE['MCLatestUser'])) {
        return true;
    } else {
        return false;
    }
}

```

Script works but cookies aren't being sent. I'm at my wits end here, been working on this for over 4-5 hours now, had over 35 chrome tabs open just to figure this out. I am probably overlooking a minor detail. [Login Page Link](https://mclatest.com/community/login.php "Link to test it out")

It works if i remove the `&& $_COOKIE['MCLatestUser']` from the function script