Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

PHP curl / how to ensure cookie file is read?

+4
−0

I've recently discovered that php curl does not load cookies from a cookie file until after at least one request is made. For instance, assume I already have a cookie file from a previous session which contains cookies. This will always return an empty array:

$cookie_file = '/dir/cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$cookies = curl_getinfo($ch, CURLINFO_COOKIELIST);

My workaround is to do something like:

curl_setopt($ch, CURLOPT_URL, "");
curl_exec($ch);
$cookies = curl_getinfo($ch, CURLINFO_COOKIELIST);

But this feels like a hack. Is there a less hacky way to ensure that cookies are loaded into memory for an existing curl session? Alternately, is there any way to determine whether or not they've been loaded?

I want have a method called get_cookies() but currently it's difficult to know whether or not the cookie file has even been read.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

0 answers

Sign up to answer this question »