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.
