如何在php中的cURL中维护会话? [英] how to maintain session in cURL in php?

查看:235
本文介绍了如何在php中的cURL中维护会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在cURL中维护会话?

how can we maintain session in cURL?

我有一个代码发送网站的登录详细信息并成功登录
i需要获取

i'am having a code the sends login details of a site and logs in successfully i need to get the session maintained at the site to continue.

这里是我用于使用cURL登录网站的代码

here is my code that used to login to the site using cURL

  <?php  
        $socket = curl_init();
        curl_setopt($socket, CURLOPT_URL, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_REFERER, "http://www.XXXXXXX.com");
    curl_setopt($socket, CURLOPT_POST, true);
    curl_setopt($socket, CURLOPT_USERAGENT, $agent);
    curl_setopt($socket, CURLOPT_POSTFIELDS, "form_logusername=XXXXX&form_logpassword=XXXXX");
    curl_setopt($socket, CURLOPT_COOKIESESSION, true);
    curl_setopt($socket, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($socket, CURLOPT_COOKIEFILE, "cookies.txt");
    $data = curl_exec($socket);
    curl_close($socket); 
   ?>


推荐答案

这是我发现做这个的最好的方法< a href =http://www.smooka.com/blog/2009/07/24/maintaining-php-session-when-using-curl/>链接:

Here is the best way i found to do this link :

下面的文本是 blogpost

 $useragent = $_SERVER['HTTP_USER_AGENT'];
 $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

session_write_close();

$ch = curl_init();
$ch = curl_init($rssFeedLink); 
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );

$response = curl_exec($ch); 
curl_close($ch); 

session_write_close()它结束当前会话并存储会话数据。显然,PHP不喜欢当多个脚本玩弄会话,所以,它锁定它。如果您不使用 session_write_close()

What does session_write_close() do? It, ends the current session and store session data. Apparently, PHP does not like when multiple scripts play around with the session, so, it locks it. Putting session_write_close makes sure that your current session is stored so you can retrieve it and use it.

,请放置session_write_close确保您当前的会话已存储,以便您可以检索并使用它。 / code>将生成新的会话ID,而不是使用当前会话ID。

if you don't use session_write_close() a new session id will be generated instead of using the current session id.

此外, PHPSESSID 会话变量的名称。根据 OWSAP推荐,它应该是更常见的 anId

Also PHPSESSID should be replaced by the name of the session variable. According to OWSAP recommandations it should be something more general like anId.

有时,您需要发送一个用户代理的帖子,因此我包括 CURLOPT_USERAGENT 参数。

Sometimes you will need to send a user agent with the post so i included the CURLOPT_USERAGENT parameter.

这篇关于如何在php中的cURL中维护会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆