Introduction to Sharing Cookies in Multiple Domains
When we develop web sites and web applications we can use single domain or we can use different domains or sub domains depending on the requirements. Normally web site and web application will be hosted on different sub domains for reasons such as security,performance and server load.
Google is the best example for understanding this concept. Google uses different sub domains for their applications such as mail,search,plus. Since Google uses same login information for all or their applications they need to keep the login data between all of the applications. Normally login data is kept in browser session. Since each sub domain creates a different session, its not possible to keep the login data using sessions. Thats where cookies comes to save us. We can set cookie values for multiple sites and keep the data we want to show in each application. PHP allows us to share cookies across multiple domains.
Even though we can set cookie values on multiple sites and access them easily, there are some limitations. We cannot set cookie values on different domains. We can only use cookies with multiple sub domains of the same site.
Assume you have a sites called innovativephp.com and www.innovativejs.com hosted on same server. Even though both sites are on same server, domain names are different hence you will see that the cookies will not be working in another top domain. So we cannot use cookies between those 2 sites. But if you have site called innovativephp.com and subdomains called www.app.innovativephp.com,www.demo.innovativephp.com, you can set cookie values for all 3 domains and communicate between each domain. It doesn’t matter whether they are hosted on the same server or not until all the domains belong to a single top domain.
Setting and Getting Cookies Using PHP
PHP provides a built in function called setCookie which can be used to save and destroy cookies on your web browser.Following section contains the syntax and various styles of it’s usage in different situations.
setcookie(cokie name,cookie value,cookie lifetime,cookie path,domain,connection type,http access);
Cookie Name – Name of the cookie.Once set you can access the value using $_COOKIE[‘name’].
Cookie Value – Value of the cookie.
Cookie Lifetime – Time before the cookie expires and destroys from the browser. normally sets the value using time()+3600 (Current time + one hour ).
Cookie Path – You can set the cookie values to entire application or part of the application. “/” will set the cookie to entire application.
Cookie Domain – You can set the cookie to specific sub domain or collection of sub domains using this parameter.
Connection Type – Can be set to TRUE or FALSE. TRUE means cookie values are available for only secure connections.
HTTP Access – Can be set to TRUE or FALSE. TRUE means cookie values are available for HTTP protocol only.
$_COOKIE['cookie_name'];
Cookies are destroyed using the same method for creating cookies. Only difference is you have to set the lifetime parameter to past value. Make sure to use all the parameters which were used in creation,when you want to delete the cookie.
If you create the cookie like this.
setcookie("Logged","True",time()+3600,"/");
Your destroy cookie method would be like this
setcookie("Logged","True",time()-3600,"/");
Examples for Using Cookies Correctly
setcookie("Logged","True",time()+3600);
setcookie("Logged","True",time()+3600,"/");
Following example code will set the cookie value only available to the files inside tutorials folder inside your blog folder.
setcookie("Logged","True",time()+3600,"/blog/tutorials/");
Following example code will set the cookie value available to all the sub domains and main domain of innovativephp.
setcookie("Logged","True",time()+3600,"/",".innovativephp.com");
Following example code will set the cookie value available to app.innovativephp.com only.
setcookie("Logged","True",time()+3600,"/",".app.innovativephp.com");
Following example code will set the cookie value available to any domain inside innovativephp which uses secure connection (https).
setcookie("Logged","True",time()+3600,"/",".innovativephp.com",1);
Following example code will set the cookie value available to any domain inside innovativephp and will be only available to HTTP requests.
setcookie("Logged","True",time()+3600,"/",".innovativephp.com",1,1);
I have explained in detail on sharing information between multiple domains using cross domain cookies. If you need more explanations or you have any ideas to add to this post, please use the comment section below.
August 29th, 2011 at 10:41 am
Nice post. I study something more challenging on different blogs everyday. It would always be stimulating to learn content from different writers and apply a little one thing from their store. I’d choose to use some with the content material on my blog whether you don’t mind. Natually I’ll provide you with a hyperlink in your net blog. Thanks for sharing.
September 1st, 2011 at 4:20 pm
Awesome, it’s very useful article. Thanks for sharing.I will post this article in my blog for Cambodian. here my blog http://www.blackkreak.com
September 3rd, 2011 at 1:24 pm
Awsome post and right to the point. I am not sure if this is truly the best place to ask but do you folks have any thoughts on where to employ some professional writers? Thx
September 3rd, 2011 at 1:24 pm
Thanks Sam
September 20th, 2011 at 1:33 am
Wow…..
I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again! “All that is gold does not glitter not all those that wander are lost.” by J. R. R. Tolkien….
September 24th, 2011 at 6:16 pm
Howdy, I wanted to ask you some thing. Is this site a wordpress site? We are considering moving my site from Blogger to wordpress, you think that is probable? Additionally did you construct the following template by yourself some how? Cheers for your help!
September 26th, 2011 at 3:46 am
I havent worked with blogger so i dont know the difficulty of moving into wordpress. I got free theme and edited according to my requirements. I hoping to create the complete theme by my self.