使用php中的curl创建会话一个​​应用程序到另一个应用程序

时间:2016-10-28 10:45:02

标签: php session curl session-cookies

我在codigniter文件夹中放置了一个codeigniter应用程序。这是我的文件夹结构。

application1
 application/
 system/
 assets/
 html/
 application2/
   application/
   system/
   assets/
   html/

我的要求是,一旦我登录到application1,我想自动将会话设置为另一个应用程序(如上所述的application2)。为此,我使用curl。这是我的代码。

   $url = "http://example.com/application2/home/login";

    //$requesturi = preg_replace('/[.|?|&]PHPSESSID=[^&]+/', "", $_SERVER['REQUEST_URI']);
    $curl_PHPSESSID = 'PHPSESSID='.$_COOKIE['PHPSESSID'].'; path=/sitespy/cookies.txt';
    $postdata       = "username=".$email."&password=".$userPassword."&apiRequest=1&PHPSESSID=".$_COOKIE['PHPSESSID'];
    //set session for sitespy
    $ch = curl_init();

    session_start();

    session_write_close();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");

    $curl_cookies = session_name().'='.session_id().'; path=/'.base_url()."sitespy/cookies.txt";
    $cokkie_path  = base_url()."sitespy/cookies.txt";

    //curl_setopt($ch, CURLOPT_COOKIE, $curl_PHPSESSID);
    curl_setopt($ch, CURLOPT_COOKIE, $curl_cookies);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cokkie_path);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_POST, 1);
    echo $result = curl_exec($ch);

    curl_close($ch);

现在我能够在application2的登录页面上设置会话,但是当我尝试访问另一个页面时,我无法从另一个部分获得该会话。请有人告诉我有可能。

0 个答案:

没有答案