如何访问子域上的cookie [英] how to access cookies on subdomain

查看:101
本文介绍了如何访问子域上的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问子域上的cookies,

i want to access the cookies on sub domain,

cookie 在主域上运行良好但无法访问子域上的 cookie,我创建了两个文件 testing_1.php 和 testing_2.php,cookie 是通过 testing_1.php 设置的

the cookies working on main domain well but can't able to access the cookie on subdomain, i created two files testing_1.php and testing_2.php, the cookies is set through testing_1.php that is

$domain = '.localhost';
session_set_cookie_params(0, '/', $domain);
session_start();

$_SESSION['name'] = 'Jogesh';
setcookie("f_name", "jogesh", 0, "/", "$domain");

if( isset( $_COOKIE["f_name"] ) )
    echo "Set Cookie on 1: " . $_COOKIE["f_name"];
else
    var_dump(setcookie("f_name", "jogesh", 0, "/", "$domain"));

在上面的代码中,我也尝试访问会话,但刚刚获得了会话 ID,如果可能,我想访问子域上的 $_SESSION['name'] 其他明智的 cookie

in the above code i also try to access session, but just got the session id, i want to access $_SESSION['name'] on subdomain if possible other wise cookies

现在 testing_2.php 在这里

now the testing_2.php is here

session_start();

if( isset( $_SESSION['name'] ) )
    echo "Session Name: " . $_SESSION['name'];
else
    echo "Session not set!";

if( isset( $_COOKIE["f_name"] ) )
    echo "Cookie Name: " . $_COOKIE["f_name"];
else
    echo "Cookie not set!";

这两个文件在 localhost 上运行良好,但是当我试图从 demo.localhost 访问文件 testing_2.php 时,无法访问任何东西,请告诉我我犯了什么错误,

the both files working well on localhost but when i am trying to access the file testing_2.php from demo.localhost the can't access anything plz let me know what mistake i had done,,

推荐答案

如果您在 www.example.com 上创建它们,则无法从 cdn.example.com<读取它们/代码>.您必须在 example.com 上创建它们.您也无法使用 JavaScript 和 Visa-verse 读取 PHP 创建的 cookie.这也取决于浏览器.tsk.tr 存在问题.您必须查看浏览器的列表 http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1 可能是因为浏览器处理 localhostsubdomain.localhost 作为完全不同的域名.在真实环境中尝试代码或使用 hosts 文件.

If you are creating them at www.example.com then you cannot read them from cdn.example.com. You have to create them at example.com. Also you cannot read PHP created cookies with JavaScript and visa-verse. It also depends on the browser. there were issues with tsk.tr. You have to see the browser's list http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1 Might me because the browser handles localhost and subdomain.localhost as complitly different domain names. Try the code in a real envirement or use hosts file.

这篇关于如何访问子域上的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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