33

PHP Curl と Cookie 認証に問題があります。

別のサーバーでユーザーを認証し、現在のユーザーの Cookie を返すファイルConnector.phpがあります。

問題は、curl を使用して何千ものユーザーを認証したいのですが、一度に 1 人のユーザーに対してのみ COOKIES を認証して保存することです。

connector.php のコードは次のとおりです。

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


    //Core Url For Services
    define ('ServiceCore', 'http://example.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?service=logon&user_name=user32&user_pass=123
  2. Connector.php?service=logosessionInfoログオン サービスで以前に保存された Cookie に基づいて、ユーザーに関する情報を返します。

問題は、このコードがユーザーごとに 1 つのファイルに Cookie を保存し、複数のユーザー認証を処理できないことです。

4

6 に答える 6

28

curl opt で Cookie ファイルを指定できます。ユーザーごとに一意のファイルを使用できます。

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

これを処理する最善の方法は、リクエスト ロジックを curl 関数に組み込み、一意のファイル名をパラメーターとして渡すことです。

    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 とオプションの配列を取ります。

于 2012-10-14T19:11:02.617 に答える
17

最初に tempnam() 関数を使用して一時 Cookie を作成します。

$ckfile = tempnam ("/tmp", "CURLCOOKIE");

次に、curl init を実行すると、Cookie が一時ファイルとして保存されます。

$ch = curl_init ("http://uri.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

または、一時ファイルに保存されている Cookie を使用してページにアクセスします。

$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);

これにより、ページの Cookie が初期化されます。

curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
于 2012-10-14T19:16:52.080 に答える
1

CURLOPT_COOKIEFILEと を使用して、ユーザーごとに異なる Cookie を定義できますCURLOPT_COOKIEJAR。ユーザーごとに異なるファイルを作成して、各ユーザーがリモートサーバー上で独自の Cookie ベースのセッションを持つようにします。

于 2012-10-14T19:12:22.150 に答える
1

上で説明したソリューションは、一意の CookieFile 名を使用しても、規模に応じて多くの問題を引き起こす可能性があります。

このソリューションでは多くの認証を提供する必要があり、ファイルの読み書きアクションが多かったためにサーバーがダウンしました。

これに対する解決策は、Apache リバース プロキシを使用し、CURL 要求をまったく省略することでした。

Apache でプロキシを使用する方法の詳細については、 https ://httpd.apache.org/docs/2.4/howto/reverse_proxy.html を参照してください。

于 2017-06-01T07:09:21.537 に答える