如何在第一次加载时通过htaccess文件设置cookie [英] How to get cookie set by htaccess file on first load

查看:74
本文介绍了如何在第一次加载时通过htaccess文件设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在第一次加载页面时获取cookie值,我知道可以在第二次加载时获取cookie.

I need to get cookie value on first load of page, I know cookie can be retrieved on second load.

我需要这个,因为我想根据服务器htaccss文件设置的cookie值进行重定向.

I need this as I want to redirect based on cookie value set by server htaccss file.

我以这种方式在htaccess文件中设置cookie.

I set cookie this way in htaccess file.

RewriteRule ^ - [CO=fullsite:yes:.example.com]

我想获取php中的值.我只知道这种方式,但这在第二次加载时给了我cookie值.

I want to fetch the value in php. I know only this way but this give me cookie value on second load.

if(isset($_COOKIE['fullsite'])&&$_COOKIE['fullsite']=='yes'){
    echo $_COOKIE['fullsite'];
}

服务器第一次发送cookie时,是否有办法从服务器获取cookie值?

Is there a way around to get cookie value from server the first time the server sends cookie please suggest.

谢谢.

推荐答案

您可以设置cookie并对URL进行完全重定向,以确保运行PHP代码时cookie可用:

You can set the cookie and do a full redirect of the URL to make sure cookie is available when PHP code runs:

RewriteCond %{HTTP_COOKIE} !^.*fullsite=yes [NC]
RewriteRule ^ %{REQUEST_URI} [L,R,CO=fullsite:yes:.example.com]

这篇关于如何在第一次加载时通过htaccess文件设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆