我在包含文件中使用以下代码。因为它在我的代码中的两个实例中使用,所以我想将它分离到另一个包含文件中,并在需要的地方与 require_once() 一起使用。但是,我注意到如果我这样做,cookie 将不会设置。其他一切似乎都有效。这是一个错误还是不能以这种方式完成。
我学习 PHP 才两周,所以请放轻松。
谢谢!
if(mysqli_num_rows($checklogin) == 1)
{
// set variables
$row = mysqli_fetch_array($checklogin);
$email = $row['Email'];
// create login sessions
$_SESSION['UserName'] = $username;
$_SESSION['Email'] = $email;
$_SESSION['LoggedIn'] = 1;
$cbxRememberMe = $_POST['cbxRememberMe'];
// if remember me is checked
if(isset($cbxRememberMe) && $cbxRememberMe == '1')
{
$row = mysqli_fetch_array($checklogin);
// create cookies for autologin
$expire = time() + AUTO_LOGIN_DURATION;
$cookie_un = sha1(sha1($row['UserName']));
$cookie_pass = sha1(sha1($row['Password']));
setcookie('user', $cookie_un, $expire);
setcookie('pass', $cookie_pass, $expire);
}
// get user's IP address
$lastloginip = $_SERVER['REMOTE_ADDR'];
// DB QUERY: update database activity
// ------------------------------------------------------------------
$updateactivity = mysqli_query($conn,"UPDATE users SET LastLoginDate = NOW(), LastActivityDate = NOW(), LastLoginIP = '$lastloginip' WHERE UserName = '$username'")
or die($updateactivity_error);
// ------------------------------------------------------------------
// redirect back to login to refresh
header('Location: login.php');
}