 |
| PHP How-To Post your "How do I do this with PHP?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 6th, 2007, 11:58 PM
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Passing Post Variables
Hi,
I'm fairly new to php and know only basics.
I want to redirect to a php page from a php page, passing post variables. I see a lot of forum posts saying you can use the sendToHost function, but none of them give examples on how to call the function so that you actually do the redirect.
If the first page has the sendtohost function, which sets up post variables, how do you redirect to the second php page so that you can access those variables? I set it up as shown below, but all I get is a blank page, not a redirect to page 2.
Also, what is the php syntax so that you call the function? Do you put the function first, then a call to the function? Not having specific examples, this is what I have:
page1.php contains:
Code:
<?php
/* sendToHost
* ~~~~~~~~~~
* Params:
* $host - Just the hostname. No http:// or
/path/to/file.html portions
* $method - get or post, case-insensitive
* $path - The /path/to/file.html part
* $data - The query string, without initial question mark
* $useragent - If true, 'MSIE' will be sent as
the User-Agent (optional)
*
* Examples:
* sendToHost('www.google.com','get','/search','q=php_imlib');
* sendToHost('www.example.com','post','/some_script.cgi',
* 'param=First+Param&second=Second+param');
*/
function sendToHost($host,$method,$path,$data,$useragent=0)
{
// Supply a default method of GET if the one passed was empty
if (empty($method)) {
$method = 'GET';
}
$method = strtoupper($method);
$fp = fsockopen($host, 80);
if ($method == 'GET') {
$path .= '?' . $data;
}
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data) . "\r\n");
if ($useragent) {
fputs($fp, "User-Agent: MSIE\r\n");
}
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') {
fputs($fp, $data);
}
while (!feof($fp)) {
$buf .= fgets($fp,128);
}
fclose($fp);
return $buf;
}
?>
<?php sendToHost('www.squirrelfreebirding.com','post','/downloads/test.php','utm_nooverride=1'); ?>
?>
|
|

March 10th, 2007, 01:32 AM
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gave up trying to get this to work. I found lots of forum posts where other people had the same question.
However, I used curl to do what I want. I know next to nothing about PHP, but enought to find examples online. So for those of you who also know next to nothing about PHP but want to redirect, passing parameters, but not having them be part of a query string, here's a very simple example:
page1.php:
Code:
<?php
$url = "http://www.mydomain.com/page2.php";
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "test=hello&test2=there"); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);
echo $result;
?>
page2.php:
Code:
<html>
<head></head>
<body>
<?php echo $_POST['test']; ?> <?php echo $_POST['test2']; ?>
</body>
</html>
If it won't work, put this in page1.php before curl_close, which will display the contents of the variables and any error messages to your screen:
Code:
print_r(curl_getinfo($ch));
echo "\n\ncURL error number:" .curl_errno($ch);
echo "\n\ncURL error:" . curl_error($ch);
// ...close cURL handle ($ch) below
|
|

March 22nd, 2007, 10:38 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Try this:
$id = @$_GET['id'];
if (!isset($id))
{
// redirect:
header('Location: http://www.example.com');
exit();
}
|
|

April 1st, 2007, 02:07 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
nice code jmaronilla.....
Been There Done That
__________________________
http://www.crystalplot.com
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Passing variables |
karlirvin |
Beginning PHP |
3 |
December 9th, 2005 04:37 PM |
| Passing variables |
acko |
ASP.NET 1.x and 2.0 Application Design |
1 |
December 23rd, 2003 09:40 AM |
| Passing variables |
jesseleon |
Access VBA |
4 |
October 24th, 2003 01:45 PM |
|
 |