Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read main cookie from the sub domain with an existing sub domain cookie in PHP?

I need to use main domain cookies for my sub domains as with higher priority when both sub and main domain cookies exists.

The problem is when I'm on sub.domain.com and there exist cookies for

  • sub.domain.com
  • .domain.com

The PHP global $_COOKIE contains $_COOKIE['data'] == 'sub.domain.com'. I would like to check if there is also a .domain.com cookie and use it.

How do I read the main cookie when I'm on a sub domain with an existing sub domain cookie?

like image 284
mvladk Avatar asked Mar 12 '12 14:03

mvladk


People also ask

Can you read cookies from subdomain?

That is, if the domain name in your cookie's domain parameter doesn't start with a period, then it will not let subdomains read that cookie. If it does start with the period, then all subdomains will have full access to that cookie's value. Can only be read by example.com.

Can I access cookies from different domains?

Cookies that are stored and accessed under a specific domain cannot be accessed from a page hosted on another domain. Therefore, the cookie data has to be passed along when leaving one domain and going to the other one.

Can subdomain access parent domain cookies?

If a cookie is scoped to a parent domain, then that cookie will be accessible by the parent domain and also by any other subdomains of the parent domain.

How do I transfer cookies from one domain to another domain?

As you may know, cookie can't be set in a different domain from another domain directly. If you're having multiple sites in where you need to set a cookie from a parent site, you can use basic HTML and JS to set the cookies. Google is using this same way.


2 Answers

It looks like the gist of your issue is reading a cookie set in domain.com from sub.domain.com.

Add

session.cookie_domain = .domain.com

to your php.ini to make this happen. If you're on a shared hosting enviroment and can't modify your ini file, try having this somewhere in your code:

ini_set("session.cookie_domain", ".domain.com");

You should now be able to access cookies set by domain.com on subdomain.domain.com.

like image 121
Jay Sidri Avatar answered Oct 06 '22 00:10

Jay Sidri


There is a $_SERVER ['HTTP_COOKIE'] variable that contains both sub domain and main domain cookie variables with the same name as one large string. In the following simple piece of code the $cookie_variable array will contain both values of specific variables:

if( 'sub.domain.com' == $_SERVER['HTTP_HOST']) {
  $var_name = 'somedata';
  $domains_counter = 0;
  foreach(explode(';', $_SERVER['HTTP_COOKIE']) as $cookie_variable_string) {
    if( false !== strpos($cookie_variable_string, $var_name.'=') ) {
      $cookie_variable[$domains_counter] = urldecode(
          trim(
              substr(
                  $cookie_variable_string, 
                  strpos($cookie_variable_string, $var_name) + strlen($var_name.'=')
              )
          )
      );
      $domains_counter++;
    }
  }
  var_dump($cookie_variable);
}

Here's a function that gets all variables:

public static function get_http_cookie_variables() {
  $domains_counter = [];
  foreach(explode(';', $_SERVER['HTTP_COOKIE']) as $cookie_variable_string) {
    $key_value = explode('=', $cookie_variable_string);
    $cookie_var_name = trim($key_value[0]);
    if(is_null($domains_counter[$cookie_var_name])) {
      $domains_counter[$cookie_var_name] = 0;
    }
    $http_cookie_variables[$cookie_var_name][$domains_counter[$cookie_var_name]] = urldecode(trim($key_value[1]));
    $domains_counter[$cookie_var_name]++;
  }

  return $http_cookie_variables;
}
like image 42
mvladk Avatar answered Oct 05 '22 23:10

mvladk