首页  >  文章  >  后端开发  >  关于POST方式传递数据的有关问题

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

WBOY
WBOY原创
2016-06-13 12:11:57799浏览

关于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

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


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


b.php
<br /><?php<br />echo 'this is the posted data';<br />if(file_exists('tt.txt')){<br />	$name = file_get_contents('tt.txt');<br />    if(!empty($name)){<br />        echo '您好,',$name.'!';<br />    }<br />}<br />?><br />

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn