Home >Backend Development >PHP Tutorial >php模拟post模式打开页面

php模拟post模式打开页面

WBOY
WBOYOriginal
2016-06-13 10:36:261173browse

php模拟post方式打开页面
我登录了后台,想在后台模拟用post方式跳转到另一个页面并处理这些数据,可是总是弹出叫我重新登录的提示,我估计是模拟post方式跳转时,已经失去了$_SESSION值。请问该怎么处理这种情况?
代码如下:
$post_string='detail=addaction&subva=0&name=testadmin&password=123456&sex=1'
$context = array(
'http' => array(  
'method' => 'POST',  
'header' => 'Content-type: application/x-www-form-urlencoded\r\nUser-Agent : Jimmy\'s POST Example beta\r\nContent-length:'.strlen($post_string)+8,
'content' => 'mypost='.$post_string
)
);  
$stream_context = stream_context_create($context);  
$data = file_get_contents("http://xxxxxxxx/xxxx/main.php", false, $stream_context);  
echo $data;

------解决方案--------------------
1)先POST数据到到登陆画面
2)返回的结果里会有一个Cookie:SessionID=xxxxxxxxxxxx
3) 取出SessionID,然后按照你上面的方式POST

'header' => 'Content-type: application/x-www-form-urlencoded\r\nUser-Agent : Jimmy\'s POST Example beta\r\nContent-length:'.strlen($post_string)+8,

'header' => 'Cookie: SessionID=xxxxxxxxx;Content-type: application/x-www-form-urlencoded\r\nUser-Agent : Jimmy\'s POST Example beta\r\nContent-length:'.strlen($post_string)+8,

Cookie的格式记不大清楚,以上只是Sample.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn