Results 1 to 7 of 7

Thread: [RESOLVED] Setting cookie on remote host

  1. #1
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Resolved [RESOLVED] Setting cookie on remote host

    I'm trying to connect two sites together, one that uses JavaScript and one that uses PHP. I want to pass a JavaScript variable to the PHP site, and figured the best way to do this would be to use jQuery.

    I created a simple PHP page that creates a cookie with it's POST data. One the JavaScript side, I've inserted this code:

    Code:
    $.post('http://remotedomain/setcookie.php', {ProfileID: ProfileID}, function(){
      //successful ajax request
    }).error(function(){
      alert('Error setting cookie');
    });
    I keep getting the error alert and I have no idea how to debug it. I tried:
    Code:
    .error(function(jqXHR, textStatus, errorThrown) {
      alert(textStatus + " | | + errorThrown);
    });
    But all that returns is "ERROR". Any thoughts/suggestions?
    Last edited by Datacide; Nov 14th, 2012 at 06:25 AM.
    PHP in your FACE!

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,633

    Re: Setting cookie on remote host

    I'm not sure you can do it this way... cookies have to be set BEFORE any output is sent to the client... so by the time your jQuery runs, it's too late... The reason for this is because cookie information is sent as part of the header information when the page is sent down to the client.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Re: Setting cookie on remote host

    The cookie is set in the remote PHP file:

    Code:
    <?php
    if ($_POST['ProfileID']) {
      $ProfileID = $_POST['ProfileID'];
      setcookie("ProfileID", $ProfileID, time()+3600, "/");
    }
    ?>
    The error I'm getting is with the $.post
    PHP in your FACE!

  4. #4
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Re: Setting cookie on remote host

    I have also tried this, but it didn't work:

    Code:
    $.ajax({ url: 'http://remotedomain/setcookie.php',
             data: {ProfileID: ProfileID},
             type: 'post',
             success: function(output) {
                          alert(output);
                      }
    });
    Is it possible to execute a file on another server using jQuery? Maybe that's my problem - if so is there a workaround?
    Last edited by Datacide; Nov 13th, 2012 at 03:06 PM.
    PHP in your FACE!

  5. #5
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Re: Setting cookie on remote host

    Ok, I found a way, I think the issue was that it's a remote server. I found I can get around this using by claiming the remote file is JSON. It still returns an error if I check for it, but it works

    Code:
    callURL = 'http://remotedomain/setcookie.php?ProfileID=' + ProfileID;
    $.ajax({
      url: callURL,
      dataType: 'jsonp',
      success:function(response){
        alert('Yay!');
      },
      error:function(XMLHttpRequest, textStatus, errorThrown){
      alert('Nay!');
    }
    });
    PHP in your FACE!

  6. #6
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Exclamation Not working in ie

    Hmm, seems I jumped the gun - IE isn't setting the cookie.

    This is the code in setcookie.php:
    Code:
    <?php
    $ProfileID = $_GET['ProfileID'];
    setcookie("ProfileID", $ProfileID, time()+3600);
    $data = '{}'; // json string
    
    if(array_key_exists('callback', $_GET)){
    
        header('Content-Type: text/javascript; charset=utf8');
        header('Access-Control-Allow-Origin: http://www.example.com/');
        header('Access-Control-Max-Age: 3628800');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
    
        $callback = $_GET['callback'];
        echo $callback.'('.$data.');';
    
    }else{
        // normal JSON string
        header('Content-Type: application/json; charset=utf8');
    
        echo $data;
    }
    ?>
    This is the JavaScript code on the remote server:
    Code:
    var cnttime = new Date()
    callURL = 'http://remotedomain/setcookie.php?ProfileID=' + ProfileID + '&timout=' + cnttime.getTime();
    $.ajax({
                url: callURL,
                dataType: 'jsonp',
                cache: false,
                success:function(response){
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){                        
                }
        });
    It works in Firefox & Chrome, but not IE
    Any thoughts?
    Last edited by Datacide; Nov 14th, 2012 at 06:42 AM.
    PHP in your FACE!

  7. #7
    Hyperactive Member Datacide's Avatar
    Join Date
    Jun 05
    Posts
    309

    Re: Setting cookie on remote host

    Kay, I resolved it myself.

    Found answer here: http://stackoverflow.com/a/1211868

    Basically, since the cookie is being set on a different server it's view as a third party cookie, which is generally blocked by IE default settings. Adding this line to setcookie.php corrected the issue:

    Code:
    header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
    PHP in your FACE!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •