Can't change cookie value (php) - php

I'm setting a cookie like so:
setcookie ('myletter', "a", time()+60*60*24*1000, "/", ".me.com" );
Then I want to change the value so I do:
$_COOKIE['myletter'] = "b"
But the old values remains. I also tried using setcookie again, that failed too
setcookie ('myletter', "b", time()+60*60*24*1000, "/", ".me.com" );
Is there a reliable way I can actually change the value of an existing cookie?

Fire up a tool like Fiddler that lets you see the HTML traffic between your browser and your test web server. Then look for Set-Cookie: on the response from the PHP script that sets the cookie, and then look for Cookie: in the following request. The fastest way for you to get it working is to understand cookies in terms of HTTP requests, which is summed up, though not for PHP, here - the concept is the same.

Related

Setting cookies with $_SERVER info

I'm trying to set a cookie which tracks what page the user is on, so that I can forward this in a websocket header, however when I do this:
setcookie("PAGE", $_SERVER['PATH_INFO'], 0, "/");
Which should be setting a cookie to something like %2FCommission%2F1 (/Commission/1), creates the cookie, which shows in firefox developer tools for a split second, then disappears (it doesn't show at all in chrome developer tools).
But if I manually set the cookie value as such:
setcookie("PAGE", "%2FCommission%2F1", 0, "/");
The cookie works perfectly fine.
I have tried trimming $_SERVER['PATH_INFO'], along with replacing potentially problematic parts, but nothing seems to work, if $_SERVER['PATH_INFO'] is used in any capacity in the creation of the string passed into the cookie value, I get this behaviour. Am I missing something?

PHP get all cookies set in web application

Is there any way to get all cookies set in php application (with no request) ? I want to do something like this:
setcookie("cookieName", "test");
print_r($_COOKIE);
Above code of course doesn't work (work only when app is requested by browser)
You can use headers_list() which will give you an array of headers that are ready to be sent to the client,
headers_list(); //doesn't take any params
eg result:
Array ( [0] => X-Powered-By: PHP/5.5.9-1ubuntu4.9 [1] => Set-Cookie: cookieName=test )
Most of your question can be answered by just visiting the setcookie() php manual.
Cookies cannot be verified until the next page load. You can manually set $_COOKIE['test'] at the time you call setcookie although this value will not be persistent if the user's browser does not store the cookie.
Other than storing the values yourself, you can use headers_list() to return what headers will be, or have been, sent by PHP, but as stated earlier, this will not verify the cookie has actually been set on the user side.

Overwrite cookie failed PHP & ASP cross subdomain

I have problem in overwriting cookies value cross sub domains, a website running in ASP which is in www.domain.com and mobile site running in PHP with m.domain.com sharing same cookie
Cookie created in www.domain.com via asp as follow:
Response.Cookies("cookie_name")="value1"
Response.Cookies("cookie_name").Expires=DateAdd("m", 1, Date())
Response.Cookies("cookie_name").Domain = ".domain.com"
Response.Cookies("cookie_name").Path = "/"
Response.Cookies("cookie_name").Secure = false
When i tried to overwrite the value in PHP (m.domain.com) as follow:
setcookie("cookie_name",'value2',time()+60*60*24*30, "/", ".domain.com",false);
the execution return true but when i check the cookie the value wasnt change still "value1"
also had tried to set via header
header("Set-Cookie: cookie_name=value2; path=/; domain=.domain.com; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT",time()+60*60*24*30));
but still no efects, any ideas? big thanks.
Finally i made it work
header("Set-Cookie: cookie_name=value2; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT",time()+60*60*24*30)."; path=/; domain=domain.com");
Note the domain part (no dot), hope this helps others
PHP and JavaScript sometimes can't work together aswell so I recognise the problem.
I don't know how much you depend on Javascript, but you could use it to set the cookie values(echo-ing "document.cookie = "=;expires=;path="; ").
It's dirty but at least there will be one common divider to worry about; not two.....

Question on PHP cookies

I came across the snippet below:
setcookie('foo', 'v1', time() + 60*60*24, '/');
setcookie('foo', 'v2');
What is the effect of setting 2
cookies with same name but different
values?
Is it common in practice?
Where is it used?
The above example will simply overwrite the first cookie with the second one. If you want to update a cookie to store a newer value, you can overwrite its value.
Two cookies may have the same name if they were set for different domains or paths. example :
<?php
setcookie("testcookie", "value1forhost", time(), "/", ".domain.com", 0, true);
setcookie("testcookie", "value2forsubdom", time(), "/", "subdom.domain.com", 0, true);
?>
The v1 vs v2 part makes it look like a trick to detect a cookie handling bug in the browser: if foo equals v1, the browser did not process the value change.
It'd be interesting to know about the code context.
Edit
Will it set 2 cookies or will it
overwrite
It depends on where you call the script from. A setcookie() call without a path sets a cookie for current path (where path is an URL path, not the internal file system path). So a call from http://example.com/ would create a single cookie and a call from http://example.com/somewhere/inside/ would crate two separate cookies, one for / and one for /somewhere/inside/.
I think this is not intended. The second cookie call will overwrite the original set cookie. After the first call there is no knowing if browser support is available, as no input from the browser is received when processing a script. A cookie is sent as a HTTP header, and sent back by the browser on consecutive requests.

Semantics of setting cookies and redirecting without getting header error

I would like to do the following in php :
setcookie('name', $value, $Cookie_Expiration,'/');
then some action
header("location:http://www.example.com")
the problem is that I get :
warning: Cannot modify header information - headers already sent by (...etc )
could you please let me know what i am doing wrong and if there is a way to do this?
by the way , this code is before any output is made ...the cookie setting part works fine on its own and so does the redirection code....the combination fails
thank you
Cookies are sent in the header, and you can't set headers if any output is already sent to the browser (which is is when you set the cookie).
The easiest solution, mind you it is a bit sloppy is to use ob_start() and ob_clean(), for example:
ob_start();
setcookie('name', $value, time()+3600);
ob_clean();
header("Location:http://www.example.com");
Please note the upper case L in the Location header, it is very important.
A better solution might be to set the cookie on the page you are redirecting to, and pass the information to set that header through a session.
From the php manual:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This
requires that you place calls to this function prior to any output, including and tags as well as any whitespace.
basically saying what you already know from your warning; that the setcookie is itself sending a header. I'd probably wonder why you want to set a cookie on a page then redirect, why not just redirect and include the data in the URL then pick it up on the target page and use the data there and/or store it in a cookie then, or store in session data if you have a session set already.

Categories