使用Javascript阅读Cookies [英] Read Cookies using Javascript

查看:80
本文介绍了使用Javascript阅读Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用php创建一个cookie,我也需要使用javascript.ie,
通过使用setcookie('mycookie','hello',time()+ 34000)在我的系统中创建一个cookie。现在,我需要阅读它使用javascript.Is这可能吗?如果有人知道,请给我一些idea.Please ....帮助我。我使用javscript如下

 < script language =javascript> 

函数Get_Cookie(check_name){
//首先我们将这个cookie分成名/值对
//注意:document.cookie只返回name = value,不是其他组件
var a_all_cookies = document.cookie.split(';');
var a_temp_cookie ='';
var cookie_name ='';
var cookie_value ='';
var b_cookie_found = false; // set boolean t / f default f

for(i = 0; i {
//现在我们将name = value pair
a_temp_cookie = a_all_cookies [i] .split('=');


//和修剪左/右空白,当我们在它
cookie_name = a_temp_cookie [0] .replace(/ ^ \s + | \s + $ / G, '');

//如果提取的名称匹配传递check_name
if(cookie_name == check_name)
{
b_cookie_found = true;
//我们需要处理cookie没有值但存在(no = sign,也就是)的情况:
if(a_temp_cookie.length> 1)
{
cookie_value = unescape(a_temp_cookie [1] .replace(/ ^ \s + | \s + $ / g,''));
}
//注意,在初始化cookie但没有值的情况下,返回null
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name ='';
}
if(!b_cookie_found)
{
return null;
}
}
if(Get_Cookie('__ gsb'))
{
alert('Found');
}
< / script>`


解决方案>

这是一个很好的帖子它演示了这种情况。为了通过脚本语言(如javascript)访问cookie,必须将 HttpOnly 属性设置为 false 。这是 setcookie 功能的最后一个参数。


I want to create a cookie using php,an also i need to read it using javascript.ie, by using setcookie('mycookie','hello',time()+34000) in php i am creating a cookie in my system.Now,i need to read it using javascript.Is it possible?If anybody knows please give me some idea.Please....help me.I am using javscript as follows

<script language="javascript">

    function Get_Cookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 )
            {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
}
 if (Get_Cookie('__gsb'))
   {
alert('Found');
}
</script>`

解决方案

Here's a nice post that demonstrates this scenario. In order to make a cookie accessible through scripting languages such as javascript it must have the HttpOnly property set to false. This is the last parameter of the setcookie function.

这篇关于使用Javascript阅读Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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