0

I've read some topics about this problem..and some people says to use cURL (althought I don't know how..)

I have two files, index.php and response.php.

index.php

/* After the body tag, in the middle of screen */
<?php echo file_get_contents('http://localhost/football/classes/response.php?type=clients'); ?>

This code was working perfectly until I realize that I need to retrieve the same info, but with cookies, when the page loads.

My old response.php file was this:

switch($_REQUEST['type']){
    case 'clients':
        $content = $load->clients();
        echo $content;
        break;
}

But now I need to do the same code but with a parameter, inside the function clients(). This parameter is a cookie.

switch($_REQUEST['type']){
    case 'clients':
        $display = 0;
        if(isset($_COOKIE['display'])){ 
            $display = 1;
        }
        $content = $load->clients($display);
        echo $content;
        break;
}

I do always receive $display = 0; because PHP doesn't detect the cookie. Although, this cookie is initialized in Chrome Cookies. Even if I do var_dump($_COOKIE); I still don't receive nothing.

I know that this problem is because of file_get_contents() How can I solve this problem?

Thanks.

Edit: Tried the @Martin solution, without success. file_get_contents receive cookies

<?php 
    $ckfile = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init ("http://localhost/football/index.php");
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);

    $ch = curl_init ("http://localhost/football/classes/response.php?type=clients");
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);

    echo $output;
?>

The page keeps on looping and doesn't stop. It shows nothing.

Community
  • 1
  • 1
user3065191
  • 145
  • 3
  • 17
  • 1
    file_get_contents() does exactly what it should, which is to get files...... it is not a web browser substitute, it does not support cookies or execute javascript or any of the other things that web browsers do.... if you want those, use curl (for cookies) or a headless browser tool such as phantomjs – Mark Baker Jan 03 '14 at 11:38
  • why do you need to use file_get_contents? Is response.php actually on a different server – Steve Jan 03 '14 at 11:39
  • @user574632 yes..and `file_get_contents` was the way I found to `echo` the content. Any other idea? – user3065191 Jan 03 '14 at 11:42
  • possible duplicate of [file\_get\_contents receive cookies](http://stackoverflow.com/questions/1797510/file-get-contents-receive-cookies) – Martin Jan 03 '14 at 11:42
  • `$_SESSION` should work, for more controll use curl – Shakil Ahamed Jan 03 '14 at 11:43
  • If its on another server, then why is the cookie set in chrome? Cookies are linked to a domain – Steve Jan 03 '14 at 11:48
  • Warning: `file_get_contents()` will fail on urls if `allow_url_fopen` is not enabled on the server, and there exists a lot of such servers in production environment. – bansi Jan 03 '14 at 11:49
  • I didn't understand for what you are using `file_get_contents()` but you can pass cookies with it. check the "Using stream contexts" example of [file_get_contents](http://in3.php.net/file_get_contents) – bansi Jan 03 '14 at 11:53
  • cURL doesn't work. At least the snippet that @Martin told. The page loops and doesn't end. See my edited post. – user3065191 Jan 03 '14 at 11:55
  • @bansi I don't want to set another cookies..I want to get the already ones. – user3065191 Jan 03 '14 at 11:55

1 Answers1

1

The super global $_COOKIE only contains the cookies that your visitor sent to your Web server when it requested your page, it will never contain the cookies that another server may send to your application when it loads another page.

But you can still retrieves all the response headers using $http_response_header.

More info in the PHP doc : $http_response_header

Martin
  • 1,868
  • 1
  • 15
  • 20