How can I use curl to login multiple users from one php script
- by kamal
Here is the scenario:
I have configured multiple users with login names aa1, aa2 .. zz99 , all with the same password, now i want to login to a php based server with these login ID's. I have a working script that logs in one user with a username and password, and using curl, browses to a target page:
// Assume php , since somehow the php encapsulation quotes were giving me trouble
$sHost = $argv[2];
$sStart = $argv[3];
$sReqId = $argv[4];
$sPage = $argv[5];
$sReqLogFile = $argv[6];
$sRespLogFile = $argv[7];
$sUserName = $argv[8];
$sPassword = $argv[9];
$sExecDelay = $argv[10];
//optional args:
if($argc 11)
{
$sCommonSID = $argv[11];
}
//$sXhprofLogFile = "";
$sSysStatsLogFile= "";
$sBaseUrl = 'https://'.$sHost.'/';
$nExecTime = 0;
$sCookieFileName = 'cookiejar/'.genRandomString().'.txt';
touch($sCookieFileName);
// Set the execution delay:
$sStart += $sExecDelay;
// Get the PHP Session Id:
if(isset($sCommonSID))
{
$sSID = $sCommonSID;
}else{
$sSID = getSID($sHost,$sBaseUrl, $sUserName, $sPassword);
}
// Sleep for 100us intervals until we reach the stated execution time:
do
{
usleep(100);
}while(getFullMicrotime()$sPage,
"pageUrl"=$sBaseUrl,
"execStart" =$nExecStart,
"execEnd"=$nExecEnd,
"respTime"=$nExecTime,
"xhprofToken"=$sXhpToken,
"xhprofLink"=$sXhpLink,
"fiveMinLoad"=$nFiveMinLoad);
}else{
$nExecStart = 0;
$sUrl = "***ERROR***";
$aReturn = null;
}
writeReqLog($sReqId, $nExecStart, $sSID, $sUrl, $sReqLogFile);
return $aReturn;
}
function getFullMicrotime()
{
$fMtime = microtime(true);
if(strpos($fMtime, ' ') !== false)
{
list($nUsec, $nSec) = explode(' ', $fMtime);
return $nSec + $nUsec;
}
return $fMtime;
}
function writeRespLog($nReqId, $sHost, $sPage, $sSID = "***ERROR***", $nExecStart = 0, $nExecEnd = 0, $nRespTime = 0, $sXhpToken = "", $sXhpLink = "", $nFiveMinLoad = 0, $sRespLogFile)
{
$sMsg = $nReqId;
$sMsg .= "\t".$sHost;
$sMsg .= "/".$sPage;
$sMsg .= "\t".$sSID;
$sMsg .= "\t".$nExecStart;
$sMsg .= "\t".$nExecEnd;
$sMsg .= "\t".$nRespTime;
$sMsg .= "\t".$sXhpToken;
$sMsg .= "\t".$nFiveMinLoad;
error_log($sMsg."\n",3,$sRespLogFile);
}
function writeReqLog($nReqId, $nExecStart, $sSID, $sUrl, $sReqLogFile)
{
$sMsg = $nReqId;
$sMsg .= "\t".$sUrl;
$sMsg .= "\t".$sSID;
$sMsg .= "\t".$nExecStart;
error_log($sMsg."\n",3,$sReqLogFile);
}
function parseSIDValue($sText)
{
$sSID = "";
preg_match('/SID:(.*)/',$sText, $aSID);
if (count($aSID))
{
$sSID = $aSID[1];
}
return $sSID;
}
function parseFiveMinLoad($sText)
{
$nLoad = 0;
$aMatch = array();
preg_match('/--5-MIN-LOAD:(.*)--/',$sText, $aMatch);
if (count($aMatch))
{
$nLoad = $aMatch[1];
}
return $nLoad;
}
function curlRequest($sUrl, $sSID="")
{
global $sCookieFileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
if($sSID == "")
{
curl_setopt($ch, CURLOPT_COOKIEJAR, $sCookieFileName);
}
else
{
curl_setopt($ch, CURLOPT_COOKIEFILE, $sCookieFileName);
}
$result =curl_exec ($ch);
curl_close ($ch);
return $result;
}
function parseXHProfToken($sPageContent)
{
//https://ktest.server.net/xhprof/xhprof_html/index.php?run=4d004b280a990&source=mybox
$sToken = "";
$sRelLink = "";
$aMatch = array();
$aResp = array();
preg_match('/$sToken, "relLink"=$sRelLink);
return $aResp;
}
function genRandomString() {
$length = 10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p