问题
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??
回答1:
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;
}
回答2:
My problem, as discussed HERE was due to localhost!!! if i use my loopback(127.0.0.1) it works perfectly, when doing as i stated. I read it has something to do with no "."'s in localhost and some browsers(like chrome) not liking it!!
Part of the problem was that jquery would read the php cookie then set its own second cookie with the extra details in path as edited in my question. Php would then read this one as it should but when setting a cookie again would "edit" its old cookie yet still read from Jquery cookie!!!
来源:https://stackoverflow.com/questions/15178473/php-sets-cookie-changed-with-jquery-cookie-plugin-cannot-be-edited-with-php