Support » Plugins » Hacks » session problem using file_get_contents

  • hello everyone,

    I hope someone can help me with this drama.

    I have made a test.php page and used file_get_contents option to output the the target URL. The URL and test.php both are on same domain and server. how ever the URL content are nicely displayed but It doesnt show me as logged in. But I am logged in my website

    Below are the scripts in my test.php file that I managed to put together after doing some Google search

    $home = file_get_contents("". "?" . session_name() . "=". session_id());
    echo $home;;

    Thats it. I dont know what else should i do to make the log in issue work. should i type sth session_name(HERE) or anything ?

    Thanks in Advance

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try this. If you login as normal to in your usual browser, try accessing the same URL in a different browser, but with your session name and value as you have them in your question. Unless your site is specifically set up to recognise this, I don’t think this will log you on.

    Questions: is the site (mytargeturl) a WordPress site? Is the site from which you are making this call a WordPress site?

    What you are trying to do is possible, but not completely trivial. I suspect you will need to pass to the external site your cookie(s) that you get upon logging in – using file_get_contents() is effectively like using a new browsing session, and is disconnected from you being logged in elsewhere.

    Take a look at the third param to file_get_contents(), which is $context. You’ll need to create a stream context containing the cookies in the header, using stream_context_create(). The php docs are quite good here.

    Let us know how you get on!

    Thanks for jumping in. Yes the targeturl and test.php both are on same WordPress site and domain in public_html folder

    I am not a coder and dont really know much about codes. I copied and pasted the above codes in test.php from google search.

    Can you please type stream_context_create() related code in here and i will paste in my test.php to see if it works?

    The codes in my test.php file is exactly as below except MytargetUrl which is real url,, and there is nothing in other brackets of codes like session_name() session_id() etc

    $home = file_get_contents(“”. “?” . session_name() . “=”. session_id());
    echo $home;;

    Sorry usernsw, that would be a fair bit of hacking, and would require setting up a test system. But if you google around the problem, you may find similar code snippets – search for “send cookies with file_get_contents” or similar.

    One of the things you’ll find with WP development is that, as flexible as plugins are, there comes a point where you just have to learn PHP. But it’s worth it, I promise 🙂

    hi halferd,
    followed your link and tried this code with only inserting my target url instead of Then refereshed test.php. Result was blank page.
    Then I changed $file to $home and in the next line typed echo $home; to see what happens. Page loaded great but not logged in.

    // Create a stream
    $opts = array(
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
    $context = stream_context_create($opts);
    // Open the file using the HTTP headers set above
    $file = file_get_contents('', false, $context);

    Heh, yeah; you need to send your cookie(s), not a random one called ‘foo’. That’s just an example.

    I would be inclined to send all cookies in $_COOKIE for the site, so you’ll need to loop through (foreach ($_COOKIE as $key => $value)) and send them all in one go. I think they are semicolon delimited, but the link should give you more info.

    Drop the Accept-Language header, you probably don’t need it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘session problem using file_get_contents’ is closed to new replies.