Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-11-2005, 07:26 PM   #1
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:

 <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();
now b4 i get beat up 4 putting a javascript code in php section:

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

require_once "HTTP/Request.php";


$req =& new HTTP_Request("");

$response $req->sendRequest();

if (
PEAR::isError($response)) {
} else {

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

$result post_it2($data" [url][/url]"); 

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

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


the postit2.php file referenced above:

PHP Code:


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".

//     echo $ReqHeader;

//  Open the connection to the host
$socket fsockopen($Host80, &$errno, &$errstr);
    if (!
$socket) {
$Result["errno"] = $errno;
$Result["errstr"] = $errstr;
$idx 0;
    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);

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, 10:52 PM   #2
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:

<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" />


Hope that helps


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 11:24 AM
Submitting a POST Form with Links HappyDude Programming 0 10-24-2004 03:07 AM
PHP:Unable to post variables from html form to mysql database vikram_cvk Linux - Software 1 09-29-2004 04:01 PM
PHP and HTML Form Post Savahn Programming 12 06-30-2004 04:15 PM
Post information not coming through PHP form. m3kgt Programming 5 04-02-2004 02:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 AM.

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