php post必须传数据,关于POST方式传递数据的有关问题

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

最近在研究怎么用POST方式传递数据

我想得到的效果是,打开网页a,向一个网页b传递一个字符串

然后在网页b上显示这个字符串。

在网上找到一份代码,是这样的

网页a的代码:

$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的代码

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上显示该怎么做?

------解决思路----------------------

可以用數據庫或文件保存。

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);

?>

handle.php

$name = isset($_POST['name'])? $_POST['name'] : '';

file_put_contents('tt.txt', $name, true);

echo 'success';

?>

b.php

echo 'this is the posted data';

if(file_exists('tt.txt')){

$name = file_get_contents('tt.txt');

if(!empty($name)){

echo '您好,',$name.'!';

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值