LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-11-2005, 06:26 PM   #1
verbatim
Member
 
Registered: Apr 2005
Posts: 31

Rep: Reputation: 15
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?
 
Old 05-11-2005, 09:52 PM   #2
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is this php automatic form submit post wriiten correctly? fsock error.. verbatim Programming 1 05-11-2005 10:24 AM
Submitting a POST Form with Links HappyDude Programming 0 10-24-2004 02:07 AM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 03:01 PM
PHP and HTML Form Post Savahn Programming 12 06-30-2004 03:15 PM
Post information not coming through PHP form. m3kgt Programming 5 04-02-2004 01:42 PM


All times are GMT -5. The time now is 09:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration