Connecting Tech Pros Worldwide Forums | Help | Site Map

Using sendtohost for redirect

Newbie
 
Join Date: Feb 2007
Posts: 4
#1: Feb 28 '07
Hi,
Very new at PHP...

I'm trying to use sendtohost to do a redirection to another page, passing it post variables. I see a lot of forum posts elsewhere that say you can do this, but none of them have full examples.

So, here are my very basic questions.

If I have page1.php and I'm trying to redirect to page2.php, passing post variables, how exactly do I call the sendtohost function from page1, and how do I redirect?

Not having an example, I did this:

page1.php contains:

<?php
(send to host function)
?>
<?php sendToHost('www.mydomain.com','post','/page2.php','utm_nooverride=1'); ?>


But I just get a blank page.

So,
1) Is this syntax correct for calling the function, or do I put the call to the function inside a body tag? Do I put the function inside a head tag, or just before any html tags?
2) I was expecting to be redirected to my new page, but all I get is a blank screen. Is there something more I have to do to make the redirect happen?

Thanks.

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Feb 28 '07

re: Using sendtohost for redirect


In order to help you or make an educated guess, show us all code, especially the sendtohost function. Without the latter we cannot help you.

And when you show code: show it within code or php tags, like stated in the Posting Guidelines at the top of this forum!

Ronald :cool:
Newbie
 
Join Date: Feb 2007
Posts: 4
#3: Mar 1 '07

re: Using sendtohost for redirect


Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. /* sendToHost
  3.  * ~~~~~~~~~~
  4.  * Params:
  5.  *   $host      - Just the hostname.  No http:// or
  6.                   /path/to/file.html portions
  7.  *   $method    - get or post, case-insensitive
  8.  *   $path      - The /path/to/file.html part
  9.  *   $data      - The query string, without initial question mark
  10.  *   $useragent - If true, 'MSIE' will be sent as
  11.                   the User-Agent (optional)
  12.  *
  13.  * Examples:
  14.  *   sendToHost('www.google.com','get','/search','q=php_imlib');
  15.  *   sendToHost('www.example.com','post','/some_script.cgi',
  16.  *              'param=First+Param&second=Second+param');
  17.  */
  18.  
  19. function sendToHost($host,$method,$path,$data,$useragent=0)
  20. {
  21.     // Supply a default method of GET if the one passed was empty
  22.     if (empty($method)) {
  23.         $method = 'GET';
  24.     }
  25.     $method = strtoupper($method);
  26.     $fp = fsockopen($host, 80);
  27.     if ($method == 'GET') {
  28.         $path .= '?' . $data;
  29.     }
  30.     fputs($fp, "$method $path HTTP/1.1\r\n");
  31.     fputs($fp, "Host: $host\r\n");
  32.     fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
  33.     fputs($fp, "Content-length: " . strlen($data) . "\r\n");
  34.     if ($useragent) {
  35.         fputs($fp, "User-Agent: MSIE\r\n");
  36.     }
  37.     fputs($fp, "Connection: close\r\n\r\n");
  38.     if ($method == 'POST') {
  39.         fputs($fp, $data);
  40.     }
  41.  
  42.     while (!feof($fp)) {
  43.         $buf .= fgets($fp,128);
  44.     }
  45.     fclose($fp);
  46.     return $buf;
  47. }
  48. ?>
  49. <?php sendToHost('www.squirrelfreebirding.com','post','/downloads/test.html','utm_nooverride=1'); ?>
  50.  
Reply