Storing Cookies on a Server

Discussion in 'PHP' started by Kennedy, Sep 4, 2007.

  1. #1
    I need to find a certain word on a page with
    @file_get_contents

    The problem is that the page needs a certain cookie to be enabled to open the page up correctly. How would I go about doing this?

    I can't just stick the cookie on the user's browser because its technically the server opening up the page. So how do I have the cookie permanently on the server so when I use file_get_contents, it reads the page correctly with the cookie enabled?

    Any help would be greatly appreciated. Thanks in advance.
     
    Kennedy, Sep 4, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    sea otter, Sep 4, 2007 IP
  3. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Now I'm just confused. stream_context_create doesn't even seem to work with file_get_contents. It won't even put the contents into a string now.
     
    Kennedy, Sep 4, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    :confused:

    Should work fine. Just threw this together, and it worked:

    
    <?php
    $opts = array
    (
    	'http' => array
    	(
        	'method'=>"GET",
        	'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
      )
    );
    $context = stream_context_create($opts);
    echo file_get_contents('http://localhost/cookietest.php',false,$context);
    ?>
    
    PHP:
    And in cookietest.php:

    
    <pre>
    <?php
    	print_r($_COOKIE);
    ?>
    </pre>
    
    PHP:
    All command line; no browser involved (although you can run the first script in a browser if you want).

    Run cookietest.php directly (from the command line) and nothing prints.

    Run cookietest.php from your browser, and you'll see any cookies that were already set by you on localhost. You will not see the "foo bar" cookie from the first script, because it didn't involve your browser.

    Run the first script, and it'll print out the "foo bar" cookie.

    Hope that helps?
     
    sea otter, Sep 4, 2007 IP
  5. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the help! I think the problem is me. I'm not quite good enough with PHP to grasp this kind of thing. :)

    So here's the deal. I'm going to put exactly what I want the script to do. The first person that can pull it off (no ifs. it has to do exactly what I want it to.) gets 10 dollars USD via paypal.

    Here's the example I want done. I want it to pull a specific URL off of a StumbleUpon page. Use my account as a test:

    http://kennedy0.stumbleupon.com/favorites/

    If you're not logged into StumbleUpon, you'll notice a message saying:

    After you click, it reloads the same page, but with a couple cookies turned on. Here's the script I am currently using:

    $udata = @file_get_contents("http://kennedy0.stumbleupon.com/favorites/");
    
    if (!preg_match('hyperdeathbabies.com', $udata)) {
    	print "Match not found.";
    }
    else {
    	print "Match found.";
    }
    Code (markup):
    As you can tell, it searches for the domain "hyperdeathbabies.com" and spits out whether or not it is on the page. The problem is that when the server connects, the R-rated cookie is not on, so it never finds the match.

    So its probably pretty simple. But I'm a moron. So the first person who can give the exact code to pull it off gets 10 dollars. Not negotiable since that's all I have in my paypal account. :)
     
    Kennedy, Sep 4, 2007 IP
  6. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok. I've done that cookie test like a million times and messed around with it a bunch. It just won't display anything but

    Array
    (
    )

    Is there something wrong with my server?
     
    Kennedy, Sep 5, 2007 IP
  7. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Scratch that. Got it working. But the original problem is still there: I have no idea what I am doing.

    This part of the code:

    echo file_get_contents('http://kennedy0.stumbleupon.com/',false,$context);

    will not echo. But if I change it to this:

    echo file_get_contents('http://kennedy0.stumbleupon.com/');

    it works. What am I doing wrong?
     
    Kennedy, Sep 5, 2007 IP
  8. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    MMJ, Sep 5, 2007 IP
  9. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Did that. Still doesn't output anything when $context is at the end.
     
    Kennedy, Sep 5, 2007 IP
  10. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ok, I'm back to the party :)

    I'll look at the stumble page and see what I can see.

    Meanwhile, what version of PHP are you running, and on what OS?
     
    sea otter, Sep 5, 2007 IP
  11. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Here you go. Screw file_get_contents() we need curl for this.

    Tested and works on my machine, both from a browser (obviously bypassing the browser's cookie settings and using it's own) and from the command line (this is the better way to test).

    Give it a whirl:

    
    <?php
    $username='kennedy0';
    $search_phrase = 'hyperdeathbabies.com';
    
    // first, get our "adult OK" cookie
    $url = 'http://www.stumbleupon.com/change_adult_filter.php?filter="+_a+"&url=' . 
    			urlencode("http://$username.stumbleupon.com/favorites/");
    $ch = curl_init($url);
    @curl_setopt($ch, CURLOPT_HEADER, 1);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $str = @curl_exec($ch);
    @curl_close($ch);
    
    // now strip the cookies from the header and save them
    preg_match_all('|Set-Cookie: (.*);|U', $str, $results);   
    $cookies = implode(';', $results[1]);
    
    // now retrieve the user's page
    $url="http://$username.stumbleupon.com/favorites/";
    $ch = curl_init($url);
    @curl_setopt($ch, CURLOPT_HEADER, 0);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIE,  $cookies);
    $page = @curl_exec($ch);
    @curl_close($ch);
    
    // now search for the desired phrase
    if (strpos($page,$search_phrase) === false)
    	echo "Match not found\n";
    else
    	echo "Match found\n";
    ?>
    
    PHP:
     
    sea otter, Sep 5, 2007 IP
  12. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Just realized you might not have curl installed (I'm running into that a lot lately :confused: ) so here's a version which uses fsockopen.

    Tested as above, works as above.

    
    <?php
    
    // SET THESE AS DESIRED
    $username='kennedy0';
    $search_phrase = 'hyperdeathbabies.com';
    
    //--------- DO NOT EDIT BELOW HERE ---------------
    
    // some paths
    $url_cookie_page = 'www.stumbleupon.com';
    $query_cookie_page = '/change_adult_filter.php?filter="+_a+"&url=' . 
    		urlencode("http://$username.stumbleupon.com/favorites/");
    $url_user_page = "$username.stumbleupon.com";
    $query_user_page = '/favorites/';
    
    // our request
    $out = '';
    $out .= "HEAD $query_cookie_page HTTP/1.0\r\n";
    $out .= "Host: $url_cookie_page\r\n";
    $out .= "Connection: Close\r\n\r\n";
    
    // our retrieved headers
    $headers='';
    
    // first, get our "adult OK" cookie
    $fp = fsockopen($url_cookie_page, 80);
    fwrite($fp, $out);
    while (!feof($fp)) 
        $headers.=fgets($fp, 2048);
    fclose($fp);
    
    // now cull the cookies from the header and save them
    preg_match_all('|Set-Cookie: (.*);|U', $headers, $cookies);   
    $cookies = implode(';', $cookies[1]);
    
    // our request
    $out = '';
    $out .= "GET $query_user_page HTTP/1.0\r\n";
    $out .= "Host: $url_user_page\r\n";
    $out .= "Cookie: $cookies\r\n"; 
    $out .= "Connection: Close\r\n\r\n";
    
    // our retrieved page
    $page = '';
    
    // now get our user page
    $fp = fsockopen($url_user_page, 80);
    fwrite($fp, $out);
    while (!feof($fp)) 
        $page.=fgets($fp, 2048);
    fclose($fp);
    
    // now strip the header from the retrieved page
    $pos = strpos($page,"\r\n\r\n");
    if ($pos !== false)
    {
    	$pos+=4;	// skip the double crlf, and strip the header
    	$page = substr($page,$pos);
    }
    
    // latsly, search for the desired phrase
    if (strpos($page,$search_phrase) === false)
    	echo 'Match not found';
    else
    	echo 'Match found';
    ?>
    
    PHP:
     
    sea otter, Sep 5, 2007 IP
  13. Kennedy

    Kennedy Peon

    Messages:
    994
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I checked it and I do have curl. Tested and working perfectly!

    We have a winner! If you PM me your paypal account, I'll send you $15 USD.

    Even though it seems like just a tiny problem, you have no idea how long I've been struggling with this. :)
     
    Kennedy, Sep 5, 2007 IP
  14. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ah, good. Glad it worked out.

    And it's not such a small problem either, so don't feel bad. Took a bit of fun sleuthing, and I happen to have been doing lots of socket/curl coding lately, so I've been knee-deep in headers, cookies, etc.

    PM sent, and thanks :)
     
    sea otter, Sep 5, 2007 IP