关于POST方式传递数据的问题

pitlane929 2014-12-18 02:06:50
最近在研究怎么用POST方式传递数据
我想得到的效果是,打开网页a,向一个网页b传递一个字符串
然后在网页b上显示这个字符串。
在网上找到一份代码,是这样的
网页a的代码:
<?php
$uri = "http://localhost/handle.php";
// 参数数组
$data = array (
'name' => 'tanteng'
// 'password' => 'password'
);

$ch = curl_init ();
// print_r($ch);
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );

print_r($return);
?>

网页b的代码
<?php
echo 'this is the posted data';
if(isset($_POST['name'])){
if(!empty($_POST['name'])){
echo '您好,',$_POST['name'].'!';
}
}
?>

在我电脑上实验的结果是打开网页b只显示 this is the posted data
而网页a上显示 您好,tanteng!
有谁能给我解释下为什么吗?如果我想让这句话在网页b上显示该怎么做?
...全文
153 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲雪星枫 2014-12-18
  • 打赏
  • 举报
回复
可以用數據庫或文件保存。 a.php

<?php
$uri = "http://localhost/handle.php";
// 参数数组
$data = array (
        'name' => 'tanteng' 
// 'password' => 'password'
);
 
$ch = curl_init ();
// print_r($ch);
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );
 
print_r($return);
?>
handle.php

<?php
$name = isset($_POST['name'])? $_POST['name'] : '';
file_put_contents('tt.txt', $name, true);
echo 'success';
?>
b.php

<?php
echo 'this is the posted data';
if(file_exists('tt.txt')){
	$name = file_get_contents('tt.txt');
    if(!empty($name)){
        echo '您好,',$name.'!';
    }
}
?>
pitlane929 2014-12-18
  • 打赏
  • 举报
回复
算我话没说清楚…… 我是希望在另一个专门的页面输出我的字符串,而不是在我提交POST请求的页面上 我现在只是用php模拟POST请求,以后要变的 就比如,我用一个C程序向某个URL提交了一个POST请求,传了个字符串过去 然后我希望打开一个指定的页面,例如output.php,在这上面看到我程序传过去的字符串 刚开始学php,可能post请求的用法我理解错了……但我的需求应该说清楚了,希望各位指教我该怎么做?
一起混吧 2014-12-18
  • 打赏
  • 举报
回复
那把a与b的代码换一下不就行了。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧