简体   繁体   中英

PHP CURL script runs but it does not set the cookie

Im trying to set a cookie through PHP CURL for more than twenty four hour for no avail.

Before i have been setting cookies in my browser by adding them as parameters in a url as shown below

http://localhost/setc.php?userid=123&panelid=1

but now i need to set the cookie when i run a script(setcookie.php)

below is the latest of various types of code that i tried.

setcookie.php

$c = curl_init('http://localhost/atst.php?userid=628929&panelid=1');

curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_COOKIE, 'userid=123; panelid=1');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);

it still does not create the cookie, can anybody help out

PS : if you guys too cant figure this out at least give me a hint/guide on how to set a simple cookie without any complications

The cookiejar is only saved when you close the curl handle using curl_close($ch).

From the manual:

CURLOPT_COOKIEFILE The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.

CURLOPT_COOKIEJAR The name of a file to save all internal cookies to when the handle is closed, eg after a call to curl_close.

http://www.php.net/manual/en/function.curl-setopt.php

$ckfile = tempnam ("/tmp/", "CURLCOOKIE");
        $BASEURL='http://localhost/openx/www/api/json/index.php/main/authenticate/';
        $POSTFIELDS='username='.$username.'&password='.$password.'';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
        curl_setopt($ch, CURLOPT_URL,'http://localhost/openx/www/api/json/index.php/main/authenticate/');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

        ob_start();      // prevent any output
        $result=curl_exec ($ch); // execute the curl command
        ob_end_clean();  // stop preventing output

        $result = curl_exec($ch);
            curl_close($ch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM