リモートサイトへのログインに使用されているphpスクリプトがあり、うまく機能しています。ただし、指定されたCookieファイルに名前が付けられている場合にのみ機能するようですcookie.txt
私が望むのは、このスクリプトを使用する各ユーザーが個別の Cookie ファイルを持つことです。したがって、machineA の人が 19 日にそれを使用すると、cookie は次のような場所に配置され/tmp/19/someNameCookie.txt
、machineB は/tmp/19/someOtherNamedCookie.txt
になり、machineC が 20 日に使用すると、/tmp/20/someNamedCookie.txt
.
一意の名前のファイルを生成する方法はたくさんありますが、ここで重要なのはそれらを機能させることです。私はphpの知識が非常に若く、curlはまったく初めてです。
CURLOPT_FOLLOWLOCATION
余談ですが、ログインした結果の新しいページを取得するために使用しても成功しませんでした.
ありがとう。
これがコードの行使です。別のソースからこれに到達しました。
$username=urlencode($usr);
$password=urlencode($pass);
$url="http://domain.com/";
$cookie="cookie.txt";
$ch = curl_init();
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_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // have tried with just the jar, and just the file and both
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
これは、ログインが成功したかどうかを確認するために呼び出すコードです
$ch2 = curl_init();
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch2, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch2, CURLOPT_URL, "http://domain.com/");
$r = curl_exec($ch2);
echo $r;
curl_close($ch2);
私の理解では、curl は Cookie ファイルが存在しない場合は作成することになっています。また、他のファイルを手動で、または fopen を使用してスクリプトで作成しようとしましたが、cookie.txt を使用している場合にのみ機能し、現在はすべてテスト用に public_html に保存されています。