It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




9年前关闭。





这是方案:
我已经用登录名aa1,aa2 .. zz99配置了多个用户,都使用相同的密码,现在我想使用这些登录ID登录到基于php的服务器。我有一个工作脚本,该脚本使用用户名和密码登录一个用户,并使用curl导航到目标页面:


//假设使用php,因为不知何故php封装引号给我带来了麻烦

$ 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];
//可选的参数:
if($ argc> 11)

{
$ sCommonSID = $ argv [11];
}
// $ sXhprofLogFile =“”;
$ sSysStatsLogFile =“”;
$ sBaseUrl ='https://'.$sHost.'/';
$ nExecTime = 0;
$ sCookieFileName ='cookiejar /'。genRandomString()。'。txt';
touch($ sCookieFileName);


//设置执行延迟:
$ sStart + = $ sExecDelay;


//获取PHP会话ID:
if(isset($ sCommonSID))
{
$ sSID = $ sCommonSID;
}其他{
$ sSID = getSID($ sHost,$ sBaseUrl,$ sUserName,$ sPassword);
}


//以100us的间隔睡眠,直到达到规定的执行时间:

{
usleep(100);
} while(getFullMicrotime()$ sPage,
“ pageUrl” => $ sBaseUrl,
“ execStart” => $ nExecStart,
“ execEnd” => $ nExecEnd,
“ respTime” => $ nExecTime,
“ xhprofToken” => $ sXhpToken,
“ xhprofLink” => $ sXhpLink,
“ fiveMinLoad” => $ nFiveMinLoad);
}其他{
$ nExecStart = 0;
$ sUrl =“ ***错误***”;
$ aReturn = null;
}
writeReqLog($ sReqId,$ nExecStart,$ sSID,$ sUrl,$ sReqLogFile);
返回$ aReturn;
}


函数getFullMicrotime()
{
$ fMtime = microtime(true);
if(strpos($ fMtime,'')!==否)
{
list($ nUsec,$ nSec)= explode('',$ fMtime);
返回$ nSec + $ nUsec;
}
返回$ fMtime;
}


函数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);
}


函数writeReqLog($ nReqId,$ nExecStart,$ sSID,$ sUrl,$ sReqLogFile)
{
$ sMsg = $ nReqId;
$ sMsg。=“ \ t”。$ sUrl;
$ sMsg。=“ \ t”。$ sSID;
$ sMsg。=“ \ t”。$ nExecStart;
error_log($ sMsg。“ \ n”,3,$ sReqLogFile);
}


函数parseSIDValue($ sText)
{
$ sSID =“”;
preg_match('/ SID:(。*)/',$ sText,$ aSID);
如果(count($ aSID))
{
$ sSID = $ aSID [1];
}
返回$ sSID;
}


函数parseFiveMinLoad($ sText)
{
$ nLoad = 0;
$ aMatch = array();
preg_match('/-5-MIN-LOAD:(。*)-/',$ sText,$ aMatch);
如果(count($ aMatch))
{
$ nLoad = $ aMatch [1];
}
返回$ nLoad;
}


函数curlRequest($ sUrl,$ sSID =“”)
{
全局$ 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(兼容; MSIE 6.0; Windows NT 5.0)”);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
if($ sSID ==“”)
{
curl_setopt($ ch,CURLOPT_COOKIEJAR,$ sCookieFileName);
}
其他
{
curl_setopt($ ch,CURLOPT_COOKIEFILE,$ sCookieFileName);
}
$ result = curl_exec($ ch);
curl_close($ ch);
返回$ result;
}


函数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);
返回$ aResp;
}


函数genRandomString(){
$ length = 10;
$ characters ='0123456789abcdefghijklmnopqrstuvwxyz';
$ string ='';
对于($ p = 0; $ p

最佳答案

您发现的代码“片段”似乎是一个命令行脚本。要使用多个帐户登录,您必须将执行代码(从开始到while循环全部重新组合)为以下功能:

function login($sHost, $sStart,$sReqId, $sPage, $sReqLogFile,
               $sRespLogFile, $sUserName, $sPassword, $sExecDelay) {


然后遍历您的用户列表,并调用该函数:

foreach ($users as $sUserName=>$sPassword) {
    login($sStupidArgNames...);

关于php - 我该如何使用curl从一个php脚本登录多个用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4540797/

10-11 20:37