Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove previously set request header referer field from curl handle?

First I initialize curl handle:

$ch = curl_init();

Next I set the url and referer headers:

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_REFERER,$referer);

And finally execute statement:

curl_exec($ch);

Now I can use another url without closing and reopening the handle, so:

curl_setopt($ch,CURLOPT_URL,$another_url);

And here headache begins, because I do not know how to disable referer header that would be send do server, of course I've tried to put false and null into CURLOPT_REFERER but it causes the referer field to be empty, that is a Referer: is still send to the server but with no value (is this even correct with http specs?).

Is there any option to remove header altogether without closing and reinstantiating curl handle ?

I'd like to avoid it because curl keeps a connection open for some time, if I would constantly close the handle while downloading from the same host it could take more time.

like image 845
rsk82 Avatar asked Feb 03 '26 15:02

rsk82


2 Answers

You can remove completely the referer field, or any other field normally handled by curl, by passing it without anything after ":" to CURLOPT_HTTPHEADER:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer:"));

And it won't appear at all in the header.

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTHTTPHEADER

like image 189
alexisdm Avatar answered Feb 05 '26 04:02

alexisdm


The Referer header should be either the full URI or a URI relative to one requested:

http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z14

It seems like a blank Referer header meets the spec, so you could just:

curl_setopt($ch,CURLOPT_REFERER,'');

The header will still appear, but it will be blank.

like image 28
Derek Kurth Avatar answered Feb 05 '26 05:02

Derek Kurth