Maintain session using fsockopen()

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".
              $data;

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

   if($read_response){
        $headers = '';
        while (!feof($fp)) {
            $line_cur = fgets($fp, 4096);
            $headers .= $line_cur;
            if (($line_cur == "\r\n") || ($line_cur == "\n")) {
                break;
            }
        }
        $body = '';
        while (!feof($fp)) {
            $line_cur = fgets($fp, 4096);
            $body .= $line_cur;
        }
   }
   @fclose($fp);
   return ($read_response ? array('headers'=>$headers, 'body'=>$body) : true);
}
auto_post('http://www.website.com/login.php', 'User=something&Pass=1234');
// Right here the log in is successful, but the session is not set / not kept
auto_post('http://www.website.com/submitarticle.php', 'title=something&author=1');
LVL 2
AndreiPociuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sjohnstone1234Commented:
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:

http://uk.php.net/manual/en/ref.curl.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sjohnstone1234Commented:
(you're probably aware, but generally sessions are maintained using cookies - so any approach that supports cookies should maintain your sessions as well)
0
AndreiPociuAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.