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!

how do i simulate a form post with cookies in it?

Status
Not open for further replies.

verbatim1

Programmer
Apr 18, 2005
58
US
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:
Code:
<?php 

  require_once "HTTP/Request.php";

include("postit2.php");

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


$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 unfurl="true"]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;$i< count($result); $i++) echo $result[$i]; 

  } 

?>

the postit2.php file referenced above:

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;
  }
?>


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/ 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?
 
verbatim1:
I don't know why you abandoned thread434-1058717, but okay.....


You posted in that other thread the output of the method sendRequest(). You said the method returns:

Array ( [0] => Array ( [expires] => [domain] => [path] => / [secure] => [name] => PHPSESSID [value] => 69d94ac6f601d8b8e63e4e3d82233ab5 ) )

which is an array. Yet you are trying to use object-reference operators on that array. this will cause the error, as in PHP an array and an object are two very different things.


What exactly are you trying to write here, some kind of proxying software?


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I am in the process of setting up a blogging site. Currently to setup there blog they must type in numerous variables in the sign up form. The only variables that will change from person to person though are the username and password.

This being the case, i have created a signup page where they type there name and password. I would like to forward those variables to a script that forwards them, along with all the other variables which never change to the signup form and submits them automatically.

Currently it works like this:

They input username/password in signup.php
signup.php posts to process.php
process.php sends info to a mysql database and sends out a confirmation email
when the user confirms the email [confirm.php]
a directory with all the neccessary files is created for them to blog.

from there they would have to go to an installation page to put there username and password in again, then click install.

i would like to simulate the installation form so the username and password only have to be typed in once.

i dont want the user to have to even see the install screen.

Now when i send all the neccessary variables need in the install form via the php script, i get the error i spoke of earlier:

Fatal error: Call to a member function on a non-object in /var/ on line 15

I believe the only thing i am missing is how to submit the cookie that is set when the page loads with the other variables.

zend.com has a mimic form post tutorial on their site but it says nothing about cookies.

what would you suggest???
 

what would you suggest???

Abandon this method which seems to be causing so much grief for you. Go for a simpler method! Why not just create a whole bunch of hidden form fields containing the data for these "numerous variables"... and ride on the back of their username/password POST form? Seems like a lot less hassle to me... a lot less maintenance... a lot easier to maintain for someone not necessarily as skilled as you.

Cheers,
Jeff

 
BabyJeffy:
Amen.

verbatim1:
The code you have posted is several orders of magnitude more complicated that you need. Your questions led me to believe that you were creating some kind of proxy system, which is something better handled in compiled code, not interpreted code like PHP.

In additiona to BabyJeffy's advice on hidden form fields, I want you to think about PHP's session-handling mechanism[link].


Want the best answers? [url=http://www.catb.org/~esr/faqs/smart-questions.html]Ask the best questions!


TANSTAAFL!!
 
If you download a standard PHP forum and strip away the code you dont need, you could be well off. Thats what I did for a friend.

Try XMBForum.com and download one of their forums. The coding contains everything you need, but uses SQL.

It is GPL, so you should be fine altering it.

Thank you!!!

Mike Kovacic

&quot;&quot;&quot; &quot;&quot;&quot;
(o) (O)
\____/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top