本文介绍了PHP卷曲和Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一些问题,PHP的卷曲和Cookies验证。

Hello everyone i have some problem with PHP Curl and Cookies Authentication.

我有一个文件的 Connector.php 这另一台服务器上验证用户身份,并返回的cookie当前用户。

I have a file Connector.php which authenticates users on another server and returns cookie with current user.

问题是,我想与卷曲验证成千上万的用户,但它验证只为一次一个用户保存的cookies。

The Problem is that i want to authenticate thousands of users with curl but it authenticates and saves COOKIES only for one user at a time.

在code为connector.php是这样的:

the code for connector.php is this:

    <?php
    if(!count($_REQUEST)) {
        die("No Access!");
    }


    //Core Url For Services
    define ('ServiceCore', 'http://XXX.com/core/');


    //Which Internal Service Should Be Called
    $path = $_GET['service'];


    //Service To Be Queried
    $url = ServiceCore.$path;

    //Open the Curl session
    $session = curl_init($url);

    // If it's a GET, put the GET data in the body
    if ($_GET['service']) {
        //Iterate Over GET Vars
        $postvars = '';
        foreach($_GET as $key=>$val) {
            if($key!='service') {
                $postvars.="$key=$val&";
            }
        }
        curl_setopt ($session, CURLOPT_POST, true);
        curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
    }


    //Create And Save Cookies
    $tmpfname = dirname(__FILE__).'/cookie.txt';
    curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname);
    curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname);

    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

    // EXECUTE
    $json = curl_exec($session);
        echo $json;
    curl_close($session);
?>

下面是认证的过程:


  1. 用户输入用户名和密码: Connector.php服务=登录&安培; USER_NAME = USER32&安培; user_pass = 123

  2. Connector.php?服务= logosessionInfo 这将返回有关用户基于与登录服务前面保存的cookies信息。

  1. User enters username and password: Connector.php?service=logon&user_name=user32&user_pass=123
  2. Connector.php?service=logosessionInfo this returns info about user based on cookies saved earlier with logon service.

的问题是,此code在一个文件中保存的Cookie对于一个用户和斜面处理多个用户认证

The problem is that this code saves Cookie in one file for one user and cant handle multiple user authentication.

如果有人为请帮助解决:)对不起我的英文不好

if someone has solution for that please help :) Sorry for my bad english

推荐答案

您可以指定一个卷曲的选择cookie文件。你可以使用一个唯一的文件为每个用户。

You can specify the cookie file with a curl opt. You could use a unique file for each user.

curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true );
curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, uniquefilename );
curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, uniquefilename );

来处理这将是坚持你的请求逻辑成卷曲的功能,只是在通过唯一的文件名作为参数的最好方法。

The best way to handle it would be to stick your request logic into a curl function and just pass the unique file name in as a parameter.

    function fetch( $url, $z=null ) {
            $ch =  curl_init();

            $useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';

            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
            curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );

            if( isset($z['post']) )         curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
            if( isset($z['refer']) )        curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );

            curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );
            curl_setopt( $ch, CURLOPT_COOKIEJAR,  $z['cookiefile'] );
            curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );

            $result = curl_exec( $ch );
            curl_close( $ch );
            return $result;
    }

我用这个快速争夺。它采用URL和一组选项。

I use this for quick grabs. It takes the url and an array of options.

这篇关于PHP卷曲和Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:46