Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Sharing Cookies Across Multiple Domains Hosted On Different Servers

on 2011/08/28 3:48 PM stored in: Uncategorized and tagged:

Introduction to Sharing Cookies in Multiple Domains

Why We need to Communicate Between 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.

Limitations of Cookies Between Multiple Sites

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

Saving 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.

Retrieving Cookies Using PHP
$_COOKIE['cookie_name'];
Destroying Cookies Using PHP

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

Create Simple Cookie with Name and Value
setcookie("Logged","True",time()+3600);
Set Cookie On Entire Application
setcookie("Logged","True",time()+3600,"/");
Set Cookie on Specific Folder Path of Application

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/");
Creating a Multi Domain Cookie

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");
Set Cookie on Specific Sub Domain

Following example code will set the cookie value available to app.innovativephp.com only.

setcookie("Logged","True",time()+3600,"/",".app.innovativephp.com");
Setting Cross Domain Cookie on Secure Sub Domains

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);
Setting Cross Domain Cookie and Allowing Only HTTP Requests

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.

7 Responses to “Sharing Cookies Across Multiple Domains Hosted On Different Servers”

  1. Denna Janofsky Says:

    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.

  2. Sok Sam Ath Says:

    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

  3. Rona Lagace Says:

    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 :)

  4. admin Says:

    Thanks Sam

  5. Macy Coupon Says:

    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….

  6. sanibel island resorts Says:

    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!

  7. nimeshrmr Says:

    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.

Leave a Reply

Follow Us On Facebook