This PHP code is supposed to write to a file in a folder specified by a cookie:
$user = $_COOKIE["username"];
if( $xml = file_get_contents("$user/docs.xml") ) {
But it just says the file /docs.xml (not specifying the folder) just the file and not the cookie value, apparently doesn't exist, because it's not getting the cookie but why?
Could it be that I'm trying to get it from a different domain?
Try setting the cookie like so
setcookie("Name", "Value", $time, "/");
The / at the end make sure the cookie works throughout the whole site, not just the folder where it was set.
Related
I'm trying to create a php cookie using
setcookie('usrid', $user_id, time()+3600);
When I checked this with the browser, the cookie is set with the correct value passed with the variable. But I couldn't retreive the value using $_COOKIE['usrid']
I tried to delete the cookie using setcookie("usrid", "", time()-3600);,
but its not getting deleted.
Now when I try to get the value of cookie, it shows the value.
Can anyone tell why is this happening?
Set Cookie
Reload page
Read Cookie
Finally I found the solution.
The path parameter was missing in setcookie function. When I set the path to "/", it worked.
Why it didn't work before is that I didn't provide the path parameter, so the cookie was only accessible from the path it was created. By setting the path parameter as "/", the cookie is accessible from any path of the domain.
Below is the code.
setcookie('usrid', $user_id, time()+3600, "/");
Check your php version, do something like this to get the value in your cookie:
$getCookie = ((int)phpversion() >= 5) ? $_COOKIE['usrid'] : $HTTP_COOKIE_VARS["usrid"];
I am facing some trouble with a conditional statement that uses cookies in PHP.
I would like to change the state of an image based on whether a cookie is set or not. Here is my code (in file MAIN.PHP):
$cookie = "d_content_vote_".$databaseArray['id'];
if(!isset($_COOKIE[$cookie])) {
// display image 1 if cookie is not set
}
else {
// display image 2 if cookie is set
}
The cookie value (of the timestamp) is set in ../INCLUDES/RATING.PHP, and I make an ajax call to that file. In order to debug, I did a print_r($_COOKIE) in RATING.PHP which gave me this:
Array
(
[d_content_vote_1] => 1402726678
[d_content_vote_4] => 1402727148
[PHPSESSID] => effa8778efbe1b3dfb5bb301e359997d
)
However, when I do a print_r($_COOKIE) in MAIN.PHP I do not get the d_content_vote_* cookies, only the session info.
How do I transfer the cookie that is set in rating.php so that I can use it in main.php. I have never faced this problem before.
[Additional info: I'm building the site on a MAMP server now]
I realised that my cookie was being set and the print_r was done in a file in a subdirectory (/includes) and therefore cannot be used in the root directory. In order to make it work in the root directory, I needed to add another attribute to the function:
setcookie($name, $value, $time, "/");
The last parameter "/" ensures that the cookie can be used in the root directory.
What about the cookie remove when a user make the log-out process? In case we use 4 parameters when setting the cookie, do we need to adopt the 4 parameters both in the log-out process?
setcookie($NomeCookieLogOut, "", time()-3600);
OR
setcookie($NomeCookieLogOut, "", time()-3600, "/");
set cookie is not setting the value for the following code.
<?php
session_start();
ob_start();
unset($_SESSION['adminname']);
session_destroy();
if(isset($_COOKIE['adminremember_me'])) {
$past = time() - 100;
setcookie('adminremember_me', gone, $past);
}
header("Location: login.php");
exit();
?>
Cookie is not deleting as setcookie donot works though an error message is not displayed.
Interesting part is that i have another file with same code structure but with different cookie name for normal user logout and that one works.
I moved the admin logout file which was in (htdocs/site/admin/)to (htdocs/site) and now logout works!!! seriously what change didit make?
You can have multiple cookies with the same name but different paths. So if you script is in /folder1/folder2/mypage.php, you can have 1 cookie with the path /folder1 and another with the path /folder1/folder2, and both cookies could have the same name.
My guess is the cookie you are trying to delete belongs to a different path (by default, if you don't specify a path, then it assumes the folder that the script is in). To delete it, you will have to manually set the path parameter to match that of the cookie. For example:
setcookie('adminremember_me', gone, $past, "/");
or
setcookie('adminremember_me', gone, $past, "/folder1/");
To see what the path is on the existing cookie, you need to use your browser's cookie viewer to see what path is set on it.
Edit: to answer the question in your edit, when you moved the location of your logout file, you moved it to be in the same folder as the path that was set on the cookie (so the default value was now the same). If you want to move the script back to the old location, just explicity set the path to whatever the folder was where it worked
I'm trying to create cookie from one folder that will also work in another.
Simply doing:
setcookie('favorite['.$id.']',1,time()+60*24*60*60,'/');
But it doesn't work. Cookies are visible in the created folder but empty in other.
Also I've tried:
setcookie('favorite['.$id.']',1,time()+60*24*60*60,ROOT);
setcookie('favorite['.$id.']',1,time()+60*24*60*60,HTTP_ADR);
Where ROOT = dirname(__FILE__) and HTTP_ADR is address of my site in http://example.com/site
Could it be due to problem of this array favorite['.$id.'] ?
UPDATE: using in this way echo count($_COOKIE['favorite'])
In case its not working you can store that cookie in the session and can use that cookie
<?php
session_start();
if(isset($_COOKIE['cookiename']))
{
$_SESSION['cookie_name']= $_COOKIE['cookiename'];
}
echo $_SESSION['cookie_name'];
?>
Strange.
This script which in /cookie/ foolder
<?
setcookie('foo[lol]', 1, time()+60*24*60*60, "/");
sets cookie which works even in root folder.
So make shure nothing deletes your cookies in another folder.
EDIT: php can't read cookies which contain brackets. But browsers can.
EDIT2: no, it can, but it thinks that it is array. So to read your cookie use this code:
var_dump($_COOKIE['favorite'][$id]);
Ok I have a cookie set, and I can clearly see it if I go to private data in Firefox... ok so when I echo it on one page in a certain directory it works, (www.example.com/dir), but on the index page of the site (www.example.com), it wont echo, it says the cookie is not set. Yes I have cookies enabled, yes I tried clearing cache and all that. Any ideas? PHP btw
Which directory are you in when the cookie gets set?
From the PHP manual on setcookie(), emphasis mine:
Path
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain . If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain . The default value is the current directory that the cookie is being set in.
Cookies can be bound to a specific domain, subdomain, path, and protocol (http/https). You need to specify the path when setting the cookie in PHP:
setcookie("TestCookie", "Value", time()+3600 , '/' );
The fourth parameter binds it to the root of the site and it will be available in any subdirectory of the main site.
If you want it available on the main domain and any subdomain, supply the fifth parameter like this:
setcookie("TestCookie", "Value", time()+3600 , '/', '.example.com' );
Now it will be readable at:
www.example.com
example.com/newdir
awesome.example.com/newdir
You need to check the path that the cookie is being set.
If it's not '/', there's your answer!
Yes try this, I was also facing this problem but resolved by below code.
setcookie("TestCookie", "Value", time()+3600 , '/' );
Set your path option; the default value is the current directory that the cookie is being set in. Because you're setting the cookie in the directory /dir , its only available within that directory or below it.
You get around this by explicitly setting the path, ie.
setcookie(name,value,expire,path,domain,secure)
Set the path to "/".
setcookie("Cookie_name", "Cookie_Value", time()+3600 , '/' );
fourth parameter ('/') will make your cookies accessible to pages in parent directories.
You need to set the $path to / in setcookie(), if you want to access it in all directories
Cookies Must Be Set Before Page Output !!!
Since cookies are sent by the script to the browser in the HTTP headers, before your page is sent, they must be set before you even send a single line of HTML or any other page output. The moment you send any sort of output, you are signalling the end of the HTTP headers. When that happens, you can no longer set any cookie. If you try, the setcookie() function will return FALSE, and the cookie will not be sent.
setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/"); // 86400 = 1 day, '/' denotes cookie available in entire directory.
and in another page:
$username = $_COOKIE['cookie_username'];
also make sure that browser is not blocking cookies.
If you want to use cookies in sub domain also:
setcookie('cookie_username', $cookie_username, time() + (86400 * 30), "/", ".subdomain.com"); // 86400 = 1 day, '/' denotes cookie available in entire directory.