Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is this php script wriiten correctly? fsockopen error message:

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
US
I have a form on my website that creates cookies in the header when the page loads. I am attempting to mimic a form post.

I get the following 2 errors when tring to simulate the post:

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/ on line 60

Warning: fsockopen(): unable to connect to http::80 in /var/ on line 60
Error 0 Success

The initial code on my page with the data I want to automatically send to my form is

Code:
include("postit2.php");

require_once "HTTP/Request.php";

$req =& new HTTP_Request(" [URL unfurl="true"]http://www.abc.com/page.php");[/URL]


$data["dbType"] = "mysql";
  $data["dbHost"] = "localhost";
  $data["dbUser"] = "xxx";
  $data["dbPass"] = "letmein";
  $data["dbName"] = "me";

  $result = post_it2($data, " [URL unfurl="true"]http://www.abc.com/page.php");[/URL]

  if (isset($result["errno"])) { 
    $errno = $result["errno"]; 
    $errstr = $result["errstr"]; 
    echo "<B>Error $errno</B> $errstr"; 
    exit; 
  } else { 

    for($i=0;$i< count($result); $i++) echo $result[$i]; 

  } 

?>

the php file with the function info that should submit the form is:

Code:
<?php

function post_it2($data, $URL) {

//  Strip http:// from the URL if present
    $URL = ereg_replace("^[URL unfurl="true"]http://",[/URL] "", $URL);

//  Separate into Host and URI
    $Host = substr($URL, 0, strpos($URL, "/"));
    $URI = strstr($URL, "/");

//  Form up the request body
    $ReqBody = "";
    while (list($key, $val) = each($data)) {
      if ($ReqBody) $ReqBody.= "&";
      $ReqBody.= $key."=".urlencode($val);
    }
    $ContentLength = strlen($ReqBody);

//  Generate the request header
    $ReqHeader =
      "POST $URI HTTP/1.0\n".
      "Host: $Host\n".
      "User-Agent: PostIt\n".
      "Content-Type: application/x-[URL unfurl="true"]www-form-urlencoded\n".[/URL]
      "Content-Length: $ContentLength\n\n".
      "$ReqBody\n";

//     echo $ReqHeader;


//  Open the connection to the host
    $socket = fsockopen($Host, 80, &$errno, &$errstr);
    if (!$socket) {
      $Result["errno"] = $errno;
      $Result["errstr"] = $errstr;
      return $Result;
    }
    $idx = 0;
    fputs($socket, $ReqHeader);
    while (!feof($socket) && $Result[$idx-1] != "0\r\n") {
    if (substr($Result[$idx-1], 0, 2) == "0\r\n") echo "The End:".strlen($Result[$idx-1]);
      $Result[$idx++] = fgets($socket, 128);
    }
    return $Result;
  }
?>

why do I get the two errors above? Is it because I am suppose to send the cookies back in the form somehow?

The name of the cookie is PHPSESSID I believe

I have read an article on zend dot com about mimicking submission posts and am still lost. Any help given would be greatly appreciated.

Thanks

[URL unfurl="true"]http://www.zend.com/zend/spotlight/mimocsumissions.php?out=dynamicwebpages [/url]
 
The page with the form on it sets a cookie every time you go to the page. I believe this cookie is posted with the form when sent the normal way.

Since i am trying to simulate the form post i must figure out how to send the cookie through my script.

by going to i get the following results:


HTTP Request Header
Connect to 66.49.242.55 on port 80 ... ok

POST /userz/mel/ser_admin.php HTTP/1.1[CRLF]
Host: www.ABC.com[CRLF]
Connection: close[CRLF]
Accept-Encoding: gzip[CRLF]
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*[CRLF]
Accept-Language: en-us[CRLF]
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) Web-Sniffer/1.0.20[CRLF]
Referer: [CRLF]
Content-type: application/x-www-form-urlencoded[CRLF]
Content-length: 20[CRLF]
[CRLF]

HTTP Response Header
Name Value Delim
HTTP Status Code: HTTP/1.1 200 OK
Date: Wed, 11 May 2005 18:52:18 GMT CRLF
Server: Apache/2.0.50 (Fedora) CRLF
X-Powered-By: PHP/4.3.10 CRLF
Expires: Thu, 19 Nov 1981 08:52:00 GMT CRLF
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 CRLF
Pragma: no-cache CRLF
Set-Cookie: PHPSESSID=698df9499042a210dfe5bfb4e7c1d09c; path=/ CRLF
Connection: close CRLF
Transfer-Encoding: chunked CRLF
Content-Type: text/html; charset=ISO-8859-1 CRLF

Content (6.86 KiB)


which basically just tells the the value of the cookie upon connecting to the page.
Aso far the script is designed to forward variables to the form and submit the form.

how do i submit the cookie value also?
 
A "Set-Cookie" comes from the server and sets the cookie:

Set-Cookie: PHPSESSID=698df9499042a210dfe5bfb4e7c1d09c; path=/


A "Cookie" header is returned by the browser and reports the value of the cookie to the server:

Cookie: PHPSESSID=698df9499042a210dfe5bfb4e7c1d09c;


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
that being th case, i believe 'function addCookie' would be my best bet.

Code:
    function addCookie($name, $value)
    {
        $cookies = isset($this->_requestHeaders['Cookie']) ? $this->_requestHeaders['Cookie']. '; ' : '';
        $this->addHeader('Cookie', $cookies . $name . '=' . $value);
    }

$name will always be PHPSESSID but how do i get the cookie that is set everytime the page is loaded as the $value

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top