使用PHP和cURL登录到远程受保护的站点

时间:2017-12-04 11:04:16

标签: php php-curl

我正在尝试登录我公司的内部网,该内部网受RSA令牌保护。我设法找到登录所需的所有数据,并使用此代码。

    <?php
//init curl
$ch = curl_init();

//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5");

// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);

//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $var);

//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute the request (the login)
$store = curl_exec($ch);

?>

登录后我会立即退出。会话最多设置为2小时。通常如何设定?我在哪里可以在原始站点代码中找到此信息?我想它会存储在一个cookie中?为了不在logIn之后立即注销,我该怎么做?

祝你好运, 迈克尔

1 个答案:

答案 0 :(得分:0)

我认为是由于SESSION和Cookie信息不可用。虽然您获得登录成功,但是下一个代码请求将检查通过登录生成的cookie值,并将检查会话值。

因此,如果他们不匹配,那么您将被退出。

我认为此代码不适用于您的目的。