Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Maintain session using fsockopen()

Posted on 2006-03-22
3
Medium Priority
?
1,414 Views
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".
              $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');
0
Comment
Question by:AndreiPociu
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
sjohnstone1234 earned 800 total points
ID: 16262278
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
 
LVL 7

Expert Comment

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

Author Comment

by:AndreiPociu
ID: 16264360
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 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