>  기사  >  백엔드 개발  >  关于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으로 문의하세요.