1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

FOREIGN DOMAIN COOKIES RETRIEVAL FAILS - NO OBVIOUS REASON ! - NEED SOME HELP...

Discussion in 'PHP' started by chrisnagios, Jul 22, 2014.

  1. #1
    consider having several domains, all parked on same vhost account,
    running the same php code and mysql db.

    the primary domain, primary-example.com does have set a cookie 'test'
    with value = 'value'

    I need, to read cookie when someone visits any secondary / parked domain,
    before I show the page on parked-example.com, so as this would be impossible
    as cookies are browser-side and we need the visitor to visit primary-example.com
    in order to have available the cookie.

    Using DB would not be feasible for this case, so I have made this:

    on main controler, I check at some point before page delivery:


    index.php

    ...
    
    // check if this is primary domain or not
    if(  ($_SERVER['SERVER_NAME'] != 'primary-example.com') && ($_SESSION['foreign_cookie'] != 'started') && ($_POST['cookie_remote'] == '')  )
    {
        $_SESSION['foreign_cookie'] = 'started';
      
        header('P3P: CP="HONK"');
      
        include 'path/to/template/foreign_cookies.php';
      
        exit;
    }
    
    PHP:
    ...

    the file foreign_cookies.php contains:

    ...

    <script type="text/javascript" src="http://<?php echo $_SERVER['SERVER_NAME']; ?>/lib/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({ type: 'POST', url: 'http://primary-example.com/set_remote_cookie.php', data: 'run=yes'})
            .done(function(response) {
                document.getElementById('cookie_remote').value=response;
                setTimeout('window.document.forms[0].submit()',1000);
            })
            .fail(function() {
                window.document.forms[0].submit();
            });
        });
    </script>
    Please Wait ...
    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
        <input type="hidden" id="cookie_remote" name="cookie_remote" value="">
    </form>
    Code (JavaScript):
    ...

    set_remote_cookie.php file contains:

    ...

    $cookie_value = $_COOKIE['test'];
    
    if( ($_POST['run'] == 'yes') || ($_GET['run'] == 'yes') )
    {
        $reply = "VALUE:$cookie_value";
      
        header('P3P: CP="HONK"');
      
        echo $reply;
    }
    PHP:
    --------------


    normally, this setup should work.

    you can check and see that if you visit parked-example.com page would stop load on first run (session is unset) and show the waiting html page,
    which then does AJAX post on the primary domain on a file that is called client side (via ajax), which normally can access, read and echo the cookie value.

    if you try to visit the cookie retriever file: http://primary-example.com/set_remote_cookie.php?run=yes, this would return: "VALUE:value"

    there seems that ajax runs ok, but the response it retrieves, is missing the value of the cookie ( result I get: "VALUE:" )



    please let me know how to resolve this issue!



    thanks for your time reading this.
     
    chrisnagios, Jul 22, 2014 IP
  2. chrisnagios

    chrisnagios Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    anyone have any idea why this does not work ???
     
    chrisnagios, Jul 23, 2014 IP
  3. DiZi

    DiZi Member

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #3
    I don't have a solution myself.. But I'd be very curious about the actual solution if anyone can come up with one and strongly empathise with your problem, because your problem parallels a problem I've had myself with a CMS project I'd been working on some time back (kind of on-going, I guess, but no set deadline and haven't touched it in over a year, so.. debatable, but.. not important) - Multiple domains for different "communities" and sub-domains for regional and language selection, whilst relying on a cookie for pulling session IDs - Session ID wouldn't carry across to different domains or sub-domains, which meant a user would be logged out any time they switched region or language. Not an operation breaking or site killing problem.. But tediously annoying at times.

    Subscribing to your thread, and, hopes to both of us that someone may come up with a good solution. :)

    Edit: Spelling error.
     
    DiZi, Jul 23, 2014 IP
  4. chrisnagios

    chrisnagios Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I am afraid that in my case, it is much more crucial,
    as this is a part needed to happen on advance of serving the final page.

    I hope someone expert with cross-domain cookie/session issues will post some solution soon !
     
    chrisnagios, Jul 23, 2014 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Sorry I am having a hard time understanding your question. Can you simply (without code) explain what is needed. Something like:

    I have domain x and y. I want to store a cookie on y sent by x.

    Either way, my suggestion would be for you to use jsonp instead of the post.
     
    ThePHPMaster, Jul 23, 2014 IP
  6. chrisnagios

    chrisnagios Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    I need to call an ajax request to a foreign domain,
    which will be a php file that runs / posts on foreign domain and would echo cookie values...

    Posting the form simply with an html form, returns echo message with cookie value,
    posting the form via ajax, cookie is not echoed back / unset.

    The file in both cases is posted client side (html form or ajax),
    so normally I expected the php file to echo back the cookie set on foreign domain,
    but for some reason, it does not work.

    Anyone know why ?
     
    chrisnagios, Jul 23, 2014 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    ThePHPMaster, Jul 23, 2014 IP
  8. chrisnagios

    chrisnagios Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    it does not work with jsonp either...

    any sample how to do this in my code to work fine ?
     
    chrisnagios, Jul 24, 2014 IP
  9. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #9
    How did you do it with jsonp?
     
    ThePHPMaster, Jul 24, 2014 IP
  10. chrisnagios

    chrisnagios Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #10
    I finally had to use DB, all other methods failed...
     
    chrisnagios, Aug 9, 2014 IP