We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Maintain session using fsockopen()

AndreiPociu asked
Medium Priority
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('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');
Watch Question

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:


Not the solution you were looking for? Getting a personalized solution is easy.

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


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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.