PHP 设置 cookie 无法从包含文件中工作

发布于 2024-09-27 20:31:57 字数 1506 浏览 6 评论 0原文

我在包含文件中使用以下代码。因为它在我的代码中的两个实例中使用,所以我想将其分离到另一个包含文件中,并在需要时与 require_once() 一起使用。然而,我注意到如果我这样做,cookies将不会设置。其他一切似乎都有效。这是一个错误还是不能以这种方式完成。

我只学习了两周的 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');
}

I use the following piece of code in an include file. Because it it used in two instances within my code, I wanted to separate it into another include file and use with require_once() where it is needed. However, I noticed that if I do that, the cookies won't set. Everything else seems to work though. Is this a bug or this just can't be done this way.

I have been learning PHP only for two weeks so please take it easy on me.

Thank you!

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');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-10-04 20:31:57

require()/include() 的文件的执行方式与将其内容嵌入到执行 require/include 的文件中完全相同。无论是直接在文件中还是通过包含完成,cookie 标头看起来都完全相同。

我会看看你是否真的在 require Once 行之前完成了 mysqli 查询,因为你已经用 if (mysqli_num_rows(... ) 业务包装了整个包含。也许你应该移动查询定义/执行业务也放入包含文件中。

A require()/include()'d file will execute exactly the same as if its contents had been embedded in the file doing the require/include. A cookie header looks exactly the same whether it's directly in a file, or done via an inclue.

I'd look at whether you've actually done a mysqli query before the require once line, since you've wrapped the entire include with that if (mysqli_num_rows(... business. Perhaps you should move the query definition/execution business into the include file as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文