LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do i simulate a form post with cookies in it? (https://www.linuxquestions.org/questions/programming-9/how-do-i-simulate-a-form-post-with-cookies-in-it-322526/)

verbatim 05-11-2005 06:26 PM

how do i simulate a form post with cookies in it?
 
I am attempting to mimic a form post or [simulate a form post]
I have a form on my website that sets cookies in the header when the initial form page loads.
I think i a\have the script set up correctly to send the form data.

My problem:

I am trying to figure out how to send the cookie that is set in the header when i
send other form variables via php.

the script that sets the cookies is below:

Code:

<script type="text/javascript">

function SetCookie(name, value) {
            var today  = new Date();
            var expire = new Date();
            expire.setTime(today.getTime() + (60*60*24*30));
            document.cookie = 'serendipity[' + name + ']='+escape(value) + ';expires=' + expire.toGMTString();
        }
        </script>

now b4 i get beat up 4 putting a javascript code in php section:

my php file [zend02.php] is:
PHP Code:

<?php 

  
require_once "HTTP/Request.php";

include(
"postit2.php");

$req =& new HTTP_Request("http://www.abc.com/userz/mel/ser_admin.php");


$response $req->sendRequest();

if (
PEAR::isError($response)) {
    echo 
$response->getMessage();
} else {
    
print_r($response->getResponseCookies());
}

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



  
$result post_it2($data" [url]http://www.abc.com/userz/mel/ser_admin.php[/url]"); 

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

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

  } 

?>

the postit2.php file referenced above:

PHP Code:


<?php

function post_it2($data$URL) {

//  Strip [url]http://[/url] from the URL if present
    
$URL ereg_replace("^[url]http://[/url]"""$URL);

//  Separate into Host and URI
    
$Host substr($URL0strpos($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-www-form-urlencoded\n".
      
"Content-Length: $ContentLength\n\n".
      
"$ReqBody\n";

//     echo $ReqHeader;


//  Open the connection to the host
    
$socket fsockopen($Host80, &$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], 02) == "0\r\n") echo "The End:".strlen($Result[$idx-1]);
      
$Result[$idx++] = fgets($socket128);
    }
    return 
$Result;
  }
?>


my php code to simulate the form on my website gives the following error when used:

Fatal error: Call to a member function on a non-object in /var/www/html/zend02.php on line 15


now i was told i am recieving the 'non object error' because sendRequest in the 1st code is returning an array.
how can i have the cookie variable sent with the form?

towlie 05-11-2005 09:52 PM

Not sure I understand what you're saying, trying to simulate
a POST request to your server?

If so, and you're having trouble sending it, you can just make
an html, use the input type="hidden" tag, type the exact data
that you want and send it to your server, like this:

Code:

<form method="post" action="myTestPhpFile.php">

    <input type="hidden" name="in1" value="hello world, my name is 'in1'" />

    <input type="submit" value="Send the test" />

</form>


Hope that helps


All times are GMT -5. The time now is 01:12 AM.