PHP设置COOKIE,更改与JQUERY COOKIE插件,不能使用PHP编辑? [英] PHP sets COOKIE, changed with JQUERY COOKIE plugin, cannot be edited with php?

查看:147
本文介绍了PHP设置COOKIE,更改与JQUERY COOKIE插件,不能使用PHP编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一些简单的代码作为测试,因为我的页面不工作,也许我错过了一些东西?我有google的这个问题,在这里搜索,但没有人似乎提到它!即使在我已经阅读的cookie tuts!



我有一个简单的setcookie php代码行:

 <?php 
if($ _ COOKIE ['PHP1']!=='php'){
$ blah = setcookie('PHP1','php',time ()+(1000 * 120),'/','',false,false);}

它在任何html之前的页面的顶部,并将cookie PHP1设置为php只是罚款; / p>

然后我在身上有一些代码:

 <? php 
if($ blah){echo'PHP1已设置';}
else {
echo'cookie php1 ='。 $ _COOKIE ['PHP1'];}
?>

告诉我是否正在设置cookie,如果设置了,值是什么。直接前进和工作正常...



(页面有jquery和jquery插件:COOKIE:linked;)
然后,使用控制台检查cookie的值,并使用cookie插件更改值,代码如下:

  $。cookie('PHP1'); 
- php
$ .cookie('PHP1','javascript',{expires:7,path:'/'});
- PHP1 = javascript; expires = Sat,09 Mar 2013 19:00:57 GMT; path = /
$ .cookie('PHP1');
- javascript

页面和php告诉我,正如预期的PHP1设置;然后再刷新希望看到php1 = php但它只是说,PHP1设置!



如果我编辑PHP代码只是为了告诉我PHP1的值,它告诉我的PHP1的值是javascript?



我在这里做错了吗?或者是它只是我不能编辑一个cookie与php后javascript已被篡改? (我想它可能是安全的)



Cookie本身不是用于任何登录或安全功能,它只是用于辅助功能 - 文本大小 - 色盲设置。



提前感谢



EDIT



确定我已经查看了Chrome中的本地主机的Cookie,并且有两个PHP1 Cookie:

 名称:PHP1 
内容:php
域名:localhost
路径:/
发送:类型的连接
脚本无效:是
创建日期:2013年3月2日星期六19:01:21
到期日:2013年3月4日星期一04:21:21


名称:PHP1
内容:javascript
域名:localhost
路径:/ Cookie_test
发送:任何类型的连接
访问脚本:是
创建日期:2013年3月2日星期六18:50:08
到期时间:当浏览会话结束时

我认为第二个,/ Cookie_test路径,是javascript的一个!所以如果这是问题,我该如何让javascript写路径为/而不是dir aswell?你可以看到从我的代码我给它的路径为/?



实际上是因为我havent添加第5选项像我在php ??

解决方案

要展开@ MIIB的注释,PHP setcookie()函数 $ _ COOKIE superglobal不直接交互。 >

正如手册在常见错误下所述:


Cookie不会显示


有效地, $ _ COOKIE 基于从浏览器接收的cookie 在PHP脚本的开头创建; setcookie()另一方面定义当脚本发送其输出时将发送到浏览器的cookie



您可能希望将 setcookie 调用包含在覆盖 $ _ COOKIE 或者更好的方法是使用 getCookie setCookie 方法创建自己的对象。



EDIT:作为直接写入 $ _ COOKIE 的函数的一个非常简单的例子:

  function set_cookie_and_superglobal($ cookie_name,$ cookie_value)
{
//为了简单起见,参数作为问题中的代码,只是概括了名称和值
setcookie($ cookie_name,$ cookie_value,time()+(1000 * 120),'/','',false,false)
$ _COOKIE [$ cookie_name] = $ cookie_value;
}


i'm using some simple code as a test as my page isn't working, maybe i'm missing something?! I have google for this problem and searched here but no one seems to have mentioned it! even on cookie tuts i have read!

i have a simple setcookie php line of code:

<?php 
if($_COOKIE['PHP1'] !== 'php'){
$blah = setcookie('PHP1','php',time() + (1000 * 120),'/','',false,false);}
?>

Its at the top of the page before any html and sets the cookie PHP1 to php just fine;

I then have some code on the body:

<?php 
if($blah){echo 'PHP1 has been set';}
else {
    echo 'cookie php1 = ' . $_COOKIE['PHP1'];}
?>

to tell me if the cookie is being set or, if set, what the value is. straight forward and works fine...

(The page has jquery and jquery plug-in :COOKIE: linked;) I then, using the console check the cookie for its value and change the value with the cookie plugin, code below:

$.cookie('PHP1');
--"php"
$.cookie('PHP1','javascript', { expires: 7, path: '/' });
--"PHP1=javascript; expires=Sat, 09 Mar 2013 19:00:57 GMT; path=/"
$.cookie('PHP1');
--"javascript"

all is good up to here, so then i refresh the page and php tells me, as expected PHP1 is set; Then refresh again hoping to see that php1 = php but it just keeps saying PHP1 is set!

if i edit the PHP code just to show me the value of PHP1 it tells me that PHP1's value is javascript?

am i doing something wrong here? or is it just that i cannot edit a cookie with php after javascript has tampered? (i guess it could be security?)

The cookie itself is not for any log-in or secure functions, it is merely going to be used for accessibility - text size - color blind settings. i would like to be able to use both incase javascript is/gets disabled for any reason!

Thanks in advance

EDIT

ok i have looked at the cookies for my localhost in chrome and there are two PHP1 cookies:

Name:   PHP1
Content:    php
Domain: localhost
Path:   /
Send For:   Any kind of connection
Accessible to Script:   Yes
Created:    Saturday, 2 March 2013 19:01:21
Expires:    Monday, 4 March 2013 04:21:21


Name:   PHP1
Content:    javascript
Domain: localhost
Path:   /Cookie_test
Send For:   Any kind of connection
Accessible to Script:   Yes
Created:    Saturday, 2 March 2013 18:50:08
Expires:    When the browsing session ends

I think the second one, /Cookie_test path, is the javascript one! so if this is the problem , how can i make it so that javascript writes the path as "/" and not the dir aswell? as you can see from my code i gave it the path as "/"?

actually is it because i havent added the 5th option like i did in php??

解决方案

To expand on @MIIB's comment, the PHP setcookie() function and $_COOKIE superglobal do not directly interact.

As the manual states under "Common Pitfalls":

Cookies will not become visible until the next loading of a page that the cookie should be visible for.

Effectively, $_COOKIE gets created at the very beginning of the PHP script based on the cookies received from the browser; setcookie() on the other hand defines which cookies will be sent to the browser when the script sends its output.

You might want to wrap your setcookie call in something which also overwrites $_COOKIE (or, even better, have an object of your own with getCookie and setCookie methods).

EDIT: As a really trivial example of such a function that writes directly to $_COOKIE:

function set_cookie_and_superglobal($cookie_name, $cookie_value)
{
    // For simplicity, this hard-codes the same parameters as the code in the question, and just generalises the name and value
    setcookie($cookie_name, $cookie_value, time() + (1000 * 120),'/','',false,false);
    $_COOKIE[$cookie_name] = $cookie_value;
}

这篇关于PHP设置COOKIE,更改与JQUERY COOKIE插件,不能使用PHP编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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