Delete all cookie with same name and different path - php

i'm using codeigniter and i have all the routes like this
www.example.com/path1/
www.example.com/path1/path_/
In every page i do a control if SESSION['lang'] is set.
if it's set I also set the Cookie
setcookie("lang", $_SESSION["LANG"], time() + (60* 60 * 24 * 30 *12 *20), "./");
But i have a problem...
when I do Logout from admin area I must delete the session and also the cookie.
But if i Delete a cookie in this way
setcookie("lang",'', time() - (60* 60 * 24 * 30 *12 *20), "./");
it will delete only one cookie lang.
I must delete all cookie "lang" of different path.
Someone can help me?!

To set a global cookie for the entire site the path should be "/" without the dot.
"/" means the root of the website.
"./" means current directory.
In your case to set the global cookie it should be:
setcookie("lang", $_SESSION["LANG"], time() + (60* 60 * 24 * 30 *12 *20), "/");
To delete the global cookie:
setcookie("lang",'', 1, "/");
Regarding your actual question, "Delete all cookie with same name and different path" there is no official documented way to do it. There is a hack using the $_SERVER['HTTP_COOKIE'] variable like here.
But this variable is not documented and could be non existent on some servers.

The way which you use to set a cookie stores a separate cookie for each page/URL that you visit because:
setcookie(..., ..., ..., "./"); // the 4th parameter means "this directory"
So if the script get executed on every page then every directory and subdirectory that you visit will get a separate cookie. However, this is not necessary if you want to set the "lang" parameter for a whole part of the domain.
If you change it to something like "/foo/" then this cookie will be valid for any http://www.example.com/foo/ and any subdirectory of www.example.com/foo/, see http://php.net/manual/en/function.setcookie.php:
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.
Because this will store only one cookie for the whole part of the domain, you can easily invalidate it using your code with the 4th parameter changed:
setcookie(..., ..., ..., "/foo/");
I hope this helps.

Codeigniter has a method to delete cookies
delete_cookie()
Lets you delete a cookie. Unless you've set a custom path or other values, only the name of the cookie is needed:
delete_cookie("name");
This function is otherwise identical to set_cookie(), except that it does not have the value and expiration parameters. You can submit an array of values in the first parameter or you can set discrete parameters.
delete_cookie($name, $domain, $path, $prefix)
With this function you can delete the cookie of any path or domain. Perhaps you need to write this function so many times as "domain/path" you have
http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html

Related

the cookie disappear when I change page

(PHP) I set the cookie in my login.php page in this way:
setcookie('cookie_id',$id);
I print the cookie and I see the correct value but when I change page with:
header($login_url);
I lose the all cookie and I don't know why. Anybody can help me?
You have to specify / as path in setcookie() function, so cookie will be available on every path of your site. To do this:
setcookie('cookie_id', $id, 0, '/');
Note that third argument is expire time which is set to 0 as default. According to documentation it means that:
If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
If you have human urls or subfolders (like www.domain.com/path1/path2/), then you must set cookie path to / to work for all paths, not just current one.
setcookie('cookie_id', $cookie_id, time() + 60*60*24*30, '/');
From PHP manual:
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.

Keeping session/cookie separate in two folders

I have two folders on my domain. /builder and /viewer. Each one functions independently from another, so when I set a cookie or session variable on one, I don't want it to affect the other.
I tried to modify session_set_cookie_params to
session_set_cookie_params(0, "/viewer");
Then also changed setcookie to
setcookie(
"$name",
"$value",
time() + ($days * 24 * 60 * 60),
"/viewer"
);
Now my viewer script doesn't seem to be able to read the cookies set. I figured I'm doing something wrong, I didn't think I needed to specify the domain parameter, but I must be missing something.
memcache/memcached session handler
You can set a new prefix witch acts like a namespace:
ini_set('memcached.sess_prefix', 'memc.sess.builder.key.');
ini_set('memcached.sess_prefix', 'memc.sess.viewer.key.');
file session handler
mkdir 2 directories under /tmp: /tmp/sess_builder/ and /tmp/sess_viewer then
session_save_path('/tmp/sess_builder/');
session_save_path('/tmp/sess_viewer/');

PHP isset for cookie not working

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, "/");

setcookie from subdomain to domain

I have:
mydomain.com (which is the portal of the game, global setting and stuff)
game.mydomain.com (which is the the actual game)
The problem is that I want to set a cookie that is available globally, on game.mydomain.net, mydomain.net (and whatever subdomain i'm going to create in the future).
I've been trying to set the cookie from another subdomain as I've read that subdomains can set cookies to parent domains but not vice versa (which is wierd and I guess I've read it wrong). Whatever, so I've done another account.mydomain.com (from which I'm making an ajax call form mydomain.net so the user can authenticate) and I'm using
setcookie('session', $value, time() + 2592000 (one month), '/', '.tribul.net');
Then, return the success message and refresh the main page on mydomain.net so it can read the new cookie value.. problem is, there's no cookie set. I've also been trying to set the cookie from mydomain.com (as .tribul.net) so it can be avaialable on all subdomains but it's available only on the main domain. What's wrong?
I need to connect all subdomains and the domain to the same cookie, TO BE NOTICED, I am setting the cookie in a backend file named process.php (placed in account.domain.com) as result of an ajax request.
Try this setcookie('session', $value, time() + 2592000 , '', '.tribul.net');
In php.ini:
session.cookie_path = /
session.cookie_domain = ".mydomain.com"
Set Cookie:
setcookie('session', $value, time() + 2592000, '/', 'mydomain.com');
I used Klaus Hartl's jquery cookie plugin in order to use my problem since I haven't been able to set up a global cookie from the ajax backend.

Cookies not working on different pages

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.

Categories