FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


This is how you set cookie in another domain





badai
OK, my point is running low. So another tutorial to boost my point so I never need to ask to reset my point.

Let say you have 2 domains and you want to save cookie on both while user browse any one of it.

All you need in the first domain that user is browsing is these client side script:

Code:

var c = some_value;
var temp=new Image;
temp.src="http://my_second_domain.com/cookie.php?agmr2bXM=" + c;


the agmr2bXM is some random string so that people won't accidentally type it and set the cookie.

on the second domain, inside file cookie.php you need this php script:

Code:
if ($_GET['agmr2bXM']) { //here you can also add checking for $_SERVER['HTTP_REFERER']
       
   setcookie('cookie_name', $_GET['agmr2bXM']);
   die();
}


of course you can just check for $_SERVER['HTTP_REFERER'], but in PHP manual, it said the value totally dependent on browser, that is why I just check for the $_GET['random_string']

Please take note, the client side part can be view by user if they view source.

Demo here:
http://badai.frih.net/cookie.php and http://hensem.mooo.com/cookie.php
Marcuzzo
I don't really like $_GET at all, even if it is a random string, it still shows up in the url.

PS: if you need points then don't write tutorials but instead start a discussion or write a review.
chances are that the mods will pick this thread and dump it in the tutorial forum where it belongs.

EDIT:
badai wrote:
Please take note, the client side part can be view by user if they view source.


you can also view cookies with bookmarklets like this one:
Code:
javascript: (function () {
    if (!window.hhhgewrt34frdki) {
        window.hhhgewrt34frdki = true;
        $c = document.createElement('DIV');
        $c.id = 'cookieDisplayDiv';
        $c.style.cssText = 'position:absolute;border:4px solid#6A8080;        display:block;        color:black;        background:#FFF;        z-index:1000000;        height:auto;        width:auto';
        document.body.appendChild($c);
        oldscroll = window.onscroll;
        window.onScroll = function () {
            $c.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + 5 + 'px';
            if (oldscroll) {
                oldscroll();
            }
        };

        function dismissd() {
            document.getElementById('cookieDisplayDiv').parentNode.removeChild(document.getElementById('cookieDisplayDiv'));
            window.onscroll = oldscroll;
            window.hhhgewrt34frdki = null;
        };
        var scr = document.createElement('script');
        scr.type = 'text/javascript';
        scr.text = dismissd;
        if (!scr.text) {
            scr.appendChild(document.createTextNode(dismissd));
        }
        document.getElementsByTagName('head')[0].appendChild(scr);
        $tbl = '<table style=\'background:#A9CCCC;font-family:sans-serif;font-size:0.9em;margin:0;padding:0\'cellspacing=\'1\'cellpadding=\'4\'border=\'0\'>';
        if (document.cookie) {
            $tbl += '<tr><th style=\'background:#6A8080;color:#FFF;\'>Cookie</th><th style=\'background:#6A8080;color:#FFF;\'><div id=\'cookieDisplayCloser\'style=\'display:block;float:right;cursor:pointer;width:auto\'onclick=\'dismissd();\'>x</div>Value  </th></tr>';
            $cc = document.cookie.split(';');
            for ($i = 0; $i < $cc.length; $i++) {
                $c0 = $cc[$i];
                $ci = $c0.indexOf('=');
                if ($ci < 0) {
                    $ci = $c0.length;
                }
                $tbl += '<tr><td style=\'background:#FFF;text-align:left\'>' + $c0.substr(0, $ci) + '</td><td style=\'background:#FFF;text-align:left\'>' + unescape($c0.substr($ci + 1)) + '</td></tr>';
            }
        } else {
            $tbl += '<tr><th style=\'background:#6A8080;color:#FFF;width:200px\'><div style=\'display:block;float:right;cursor:pointer;width:auto\'onclick=\'dismissd();\'>x</div>Cookies</th></tr>' + '<tr><td style=\'text-align:center;background:#FFF\'>None</td></tr>';
        }
        $tbl += '</table>';
        $c.innerHTML = $tbl;
        $c.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + 5 + 'px';
        $c.style.left = '5px';
        void(x = 3);
    }
})()
Related topics
Domain setup woes
Introduction To Cookies :
CAN ANYBODY HELP ME PLEASE ?!!
[*READ THIS FIRST!*] Commonly asked questions
How to set up another domain?
pop3 Accounts And Domain Names
How to add a new domain to my site
Need this script
How to get and set up a free domain.
Do I need 2 domain names?
Weird cookie msg up top
How Do I Transfer an SQL Database from one domain to another
adding another domain that points to my frihost hosting
Redirect from one domain to another domain
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.