Maintain session using fsockopen()

Posted on 2006-03-22
Last Modified: 2012-08-13
My objective is to remotely submit a form using PHP. Using the function shown below, I managed to do just that.
But now I have a problem: there is another form I need to submit, but for submitting that form I first need to log in on the website using another form. It was very easy to do that, but the problem is my session is not kept, so when I move to the second form after logging in, it appears as if I'm logged out (session is null), and of course, it doesn't get submitted successfuly.

Thanks in advance for your help.
Below you can see the function which currently works without problems, but doesn't keep the session. Also, will your solution (if there is one) also work for cookies?

function auto_post($url, $data, $read_response = false)
   $url = parse_url($url);
   $request = "POST ".$url['path']." HTTP/1.1\r\n".
              "Host: ".$url['host']."\r\n".
              "Content-type: application/x-www-form-urlencoded\r\n".
              "User-Agent: Mozilla 4.0\r\n".
              "Content-length: ".strlen($data)."\r\n".
              "Connection: close\r\n\r\n".

   $fp = @fsockopen($url['host'], 80, $error_no, $error_msg);
     die("Couldn't Connect To {$url['host']} <br />\r\n Error Number: $error_no <br />\r\n Error Message: $error_msg.");

        $headers = '';
        while (!feof($fp)) {
            $line_cur = fgets($fp, 4096);
            $headers .= $line_cur;
            if (($line_cur == "\r\n") || ($line_cur == "\n")) {
        $body = '';
        while (!feof($fp)) {
            $line_cur = fgets($fp, 4096);
            $body .= $line_cur;
   return ($read_response ? array('headers'=>$headers, 'body'=>$body) : true);
auto_post('', 'User=something&Pass=1234');
// Right here the log in is successful, but the session is not set / not kept
auto_post('', 'title=something&author=1');
Question by:AndreiPociu
    LVL 7

    Accepted Solution

    You might find PHP's CURL extension useful - it supports cookies and much more (authentication, uploads, proxies, other protocols) and doesn't require you to speak raw HTTP:
    LVL 7

    Expert Comment

    (you're probably aware, but generally sessions are maintained using cookies - so any approach that supports cookies should maintain your sessions as well)
    LVL 2

    Author Comment

    Thanks Simon. I was looking into cURL even before using fsockopen(), since I used cURL when working with Authorize.Net and other merchant accounts. However, since I then saw fsockopen() examples, I decided it to use instead. Now I see that cURL does exactly what I want.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now