简体   繁体   English

PHP Curl POST / SESSION / COOKIE问题

[英]PHP Curl POST/SESSION/COOKIE Issue

I want to create a script that can login using POST info, then browses through afew pages, on the same time. 我想创建一个可以使用POST信息登录的脚本,然后同时浏览几个页面。 (After logging in) So I need to keep the session/cookie thing... (登录后)所以我需要保留会话/ cookie内容...

I've tried afew things, the logging in (POST) works fine. 我尝试了一些事情,登录(POST)正常。

But when I try to execute a second url, (the first being the login page) I lose the session/cookie and it the webpage says i'm no longer logged in: 但是,当我尝试执行第二个URL(第一个是登录页面)时,我丢失了会话/ Cookie,并且网页上显示我不再登录:

Where am I going wrong with my code? 我的代码哪里出问题了?

$url = 'http://www.example.com/authenticate.php';
$url2 = 'http://www.example.com/twourl.php';

$player = 'myuser';
$password = 'mypass';
$fields = array(
            'player'=>urlencode($player),
            'password'=>urlencode($password),
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_HEADER, 0);

//execute post
curl_exec($ch);

curl_setopt($ch, CURLOPT_URL,$url2);
curl_setopt($ch, CURLOPT_HEADER, 0);

//execute two url
curl_exec($ch);

//close connection
curl_close($ch);

Why is it not keeping me logged in on the second url? 为什么不让我登录第二个URL?

Check out http://www.php.net/manual/en/book.curl.php#97160 I don't see where you are trying to pass the session to the site. 查看http://www.php.net/manual/zh/book.curl.php#97160我看不到您尝试将会话传递到站点的位置。

EDIT: you can also check out http://www.phpbuilder.com/board/showthread.php?t=10346748 编辑:您还可以签出http://www.phpbuilder.com/board/showthread.php?t=10346748

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM