Php CURL - several independent sessions - need help!

Here's my dilemma ... I basically have a script that, through CURL messages on a third-party website, logs in and then creates another record to update user data based on this login session. Now that my site is getting busy, I have several users who are doing the same thing, and it sometimes seems like curl gets confused and updates user information with other user information. This causes real problems. It seems that the cookie that the user uses after one login is used by other users, and they end up logging in with the same cookie - confusing the third-party system. My code is posted below and I need to use a cookie and cookiejar to support a php session so that I can do what I need. But it seemsthat the same cookie is reused by all users. Does this make sense? Is there anything I can do to change this? Please recommend .... Thanks so much!

Below is the code that I use for both logging in and publishing a user update

function hitForm($postURL, $postFields, $referer="", $showerr = FALSE, $ispost = TRUE) {
    global $islocal, $path_escape;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");

    curl_setopt($ch, CURLOPT_URL, $postURL);
    if ($ispost)
        curl_setopt($ch, CURLOPT_POST, 1);
    else
        curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $ret = curl_exec($ch);
    if ($error = curl_error($ch)) {
        if ($showerr)
            echo 'ERROR: ' . $error;
        return -1;
        exit;
    }
    $CU_header = curl_getinfo($ch);
    $CU_header["err"] = curl_errno($ch);
    $CU_header["errmsg"] = curl_error($ch);
    curl_close($ch);

    $returnout = $ret;

    //for debugging purposes for now we are logging all form posts
    SaveLog("hitform", "F[".$this->curruserid." - ".$this->currfunc." - ".date("d-m-y h:i:s")."]".$postFields);

    return $ret;
}
+3
source share
4 answers

You use the same cookie.txt for each session so that there is a problem with the common cookie. You will need to specify a separate file for each concurrent session that you want to run.

+2
source

You use a shared cookie for all users. Each user needs a separate cookie jar.

+2
source

cookie .

, postFields (, ), - :

$cookie_file = 'cookies_' . $postFields['user_id'] . '.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
+2

, script . ?

, - , ;)

So, first of all, I would associate the session identifier with information about the user (or, say, save the user information in the session, which is unique to everyone) and download it from there. And I think this should do the trick;)

0
source

Source: https://habr.com/ru/post/1782471/


All Articles