PHPBuilder - Implementing Cross-Domain Cookies Page 2

RSS Twitter

Implementing Cross-Domain Cookies - Page 2

by: Christopher Kings-Lynne
November 29, 2000

Step 1: Setting Up A Prepend Script

Add the following code to a prepend script (or a function that appears at the top of all scripts).


/* Support cross-domain cookies... */

// If the GET variable has been set, and it differs from the cookie
// variable, then use the get variable (and update the cookie) 
if (isset(
$sessionid) && isset($HTTP_GET_VARS['sessionid']) && ($HTTP_COOKIE_VARS['sessionid'] != $HTTP_GET_VARS['sessionid'])) { 
SetCookie('sessionid'$HTTP_GET_VARS['sessionid'], 0'/'''); 
$HTTP_COOKIE_VARS['sessionid'] = $HTTP_GET_VARS['sessionid']; 
$sessionid $HTTP_GET_VARS['sessionid'];

Once this code has been run, a global variable 'sessionid' will be available to the script. It will contain the value of the sessionid from the users cookie, or the value sent along with a GET request.

Step 2: Using Variables For All Cross-Domain Hrefs

Create a global configuration file that contains the base hrefs of the domains you are switching between. For example, if we have and, set the following:


['domain1'] = "$sessionid-";
$domains['domain2'] = "$sessionid-";

Now, if you do the following in your code...


echo "Click &lt;a href=\""$domains['domain2'], "/contact/?email=yes\"&gt;here&lt;/a&gt; to contact us.";

?> will produce the following output...
Click <a href="\">here</a> to contact us.
...where the sessionid has been inserted into the URL.
At this point, you are probably thinking "this will try to open a subdirectory on the webserver called dash, sessionid, dash?!?!?". However, the next step will provide the necessary magic to make it all work - mod_rewrite!

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Christopher Kings-Lynne



(Maximum characters: 1200). You have characters left.