FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » php curl only saves a cookie with tempnam()
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
php curl only saves a cookie with tempnam() [message #180751] Sat, 16 March 2013 18:23 Go to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
$tmpfile = tempnam('/tmp', 'CURL_tempnam_');
d($tmpfile); //string: /tmp/CURL_tempnam_lpeeqs --random string on end

$tmpfile = '/tmp/CURL_var';
d($tmpfile); //string: /tmp/CURL_var

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
[...]

A cookie will be saved with the randomized name, even though I am setting the $tmpfile again. If I comment the

$tmpfile = tempnam('/tmp', 'CURL_tmpfile');

no cookie will be saved. What gives?
Re: php curl only saves a cookie with tempnam() [message #180760 is a reply to message #180751] Sat, 16 March 2013 22:48 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/16/2013 2:23 PM, doug(at)dougcassidy(dot)com wrote:
>
>
> $tmpfile = tempnam('/tmp', 'CURL_tempnam_');
> d($tmpfile); //string: /tmp/CURL_tempnam_lpeeqs --random string on end
>
> $tmpfile = '/tmp/CURL_var';
> d($tmpfile); //string: /tmp/CURL_var
>
> $ch = curl_init($url);
> curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
> curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
> [...]
>
> A cookie will be saved with the randomized name, even though I am setting the $tmpfile again. If I comment the
>
> $tmpfile = tempnam('/tmp', 'CURL_tmpfile');
>
> no cookie will be saved. What gives?
>

Did you copy/paste your code? Or did you type it in again?

The code you show should not do this. I highly suspect you left
something (a lot?) out.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php curl only saves a cookie with tempnam() [message #180766 is a reply to message #180760] Sun, 17 March 2013 06:46 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
Here's the whole function.

Two things I dont get, why does the curl_setopt use the $tmpfile that I set first and not the overwritten one. And, even if I comment the tempnam() $tmpfile, it wont write a cookie to /tmp/CURL_var_.txt. No cookie gets written at all, or at least not in the tmp dir. Both local Win and live Linux server.

function curl($url, $referer = null, $post = null, $removeWhiteSpaces = true) {
global $cost, $cost, $last_url;
if(is_array($referer)){//must be an args array
extract( wp_parse_args( $referer, array(
'referer' => null
,'post' => null
,'removeWhiteSpaces' => true
,'caller' => 'nocaller'
) ) );
}
if($removeWhiteSpaces)$url = removeWhiteSpaces($url);

static $tmpfile;

if(!isset($tmpfile) || ($tmpfile == ''))
$tmpfile = tempnam('/tmp', 'CURL_tmpfile');

$tmpfile = '/tmp/CURL_var_.txt';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);


@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");

if($referer) curl_setopt($ch, CURLOPT_REFERER, $referer);

if(!is_null($post)){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}

$html = curl_exec($ch);

$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);
return $html;
}
Re: php curl only saves a cookie with tempnam() [message #180767 is a reply to message #180766] Sun, 17 March 2013 06:52 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
also, the static doesnt have any effect on this behavior. It has the expected effect of writing only one cookie, even though this func is run 5 times per page load (on this particular page). Without static, I get 5 cookies. But this is all the tempnam() cookies, not my '/tmp/CURL_var_.txt'
Re: php curl only saves a cookie with tempnam() [message #180770 is a reply to message #180766] Sun, 17 March 2013 14:03 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/17/2013 2:46 AM, Doug Cassidy wrote:
> Here's the whole function.
>
> Two things I dont get, why does the curl_setopt use the $tmpfile that I set first and not the overwritten one. And, even if I comment the tempnam() $tmpfile, it wont write a cookie to /tmp/CURL_var_.txt. No cookie gets written at all, or at least not in the tmp dir. Both local Win and live Linux server.
>
> function curl($url, $referer = null, $post = null, $removeWhiteSpaces = true) {
> global $cost, $cost, $last_url;
> if(is_array($referer)){//must be an args array
> extract( wp_parse_args( $referer, array(
> 'referer' => null
> ,'post' => null
> ,'removeWhiteSpaces' => true
> ,'caller' => 'nocaller'
> ) ) );
> }
> if($removeWhiteSpaces)$url = removeWhiteSpaces($url);
>
> static $tmpfile;
>
> if(!isset($tmpfile) || ($tmpfile == ''))
> $tmpfile = tempnam('/tmp', 'CURL_tmpfile');
>
> $tmpfile = '/tmp/CURL_var_.txt';
>
> $ch = curl_init($url);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
>
> curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
> curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
>
>
> @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
> curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0");
>
> if($referer) curl_setopt($ch, CURLOPT_REFERER, $referer);
>
> if(!is_null($post)){
> curl_setopt($ch, CURLOPT_POST, true);
> curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
> }
>
> $html = curl_exec($ch);
>
> $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
>
> curl_close($ch);
> return $html;
> }
>

Did you copy/paste this code? Or did you type it in? Are you sure this
is the code being executed?

Because there is no way the code would generate the results you claim.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php curl only saves a cookie with tempnam() [message #180772 is a reply to message #180770] Sun, 17 March 2013 16:42 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
> Did you copy/paste this code? Or did you type it in? Are you sure this
> is the code being executed?
> Because there is no way the code would generate the results you claim.

Yes, I pasted in this code. I am sure it is the func being executed because changes like I mentioned, commenting, etc, are having effect.

I am calling it with this
function get_bitly_short_url($url,$format='txt') {
$connectURL = 'http://api.bit.ly/v3/shorten?login= '.bitlyLogin.'&apiKey='.bitlyApiKey.'&uri='.urlencode($url).'&f ormat='.$format;
return trim(curl($connectURL, array('caller'=>__FUNCTION__)));
}
Which does indeed create the shorturls. Also, I can echo $caller successfully. Also, I can rename the func and get the same results. I just verified all of this.
Re: php curl only saves a cookie with tempnam() [message #180773 is a reply to message #180772] Sun, 17 March 2013 17:16 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
actually, the temp dir is different on Win. I changed to
$tempdir = sys_get_temp_dir();
if(!isset($tmpfile) || ($tmpfile == '')) $tmpfile = tempnam($tempdir, 'CURL_tmpfile');
$tmpfile = $tempdir.'/CURL_var_.txt';

same results, Win local and Linux live
Re: php curl only saves a cookie with tempnam() [message #180774 is a reply to message #180773] Mon, 18 March 2013 01:13 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/17/2013 1:16 PM, Doug Cassidy wrote:
> actually, the temp dir is different on Win. I changed to
> $tempdir = sys_get_temp_dir();
> if(!isset($tmpfile) || ($tmpfile == '')) $tmpfile = tempnam($tempdir, 'CURL_tmpfile');
> $tmpfile = $tempdir.'/CURL_var_.txt';
>
> same results, Win local and Linux live
>

Then what you're saying is the interpreter is broken. It is not setting
$tmpfile to the new value; rather it is keeping the old value.

In your code, if $tmpfile were actually being to the new value, there is
no way curl_opt() could get the old value.

What happens if you echo $tmpfile before and after the statement:

$tmpfile = $tempdir.'/CURL_var_.txt';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php curl only saves a cookie with tempnam() [message #180778 is a reply to message #180774] Mon, 18 March 2013 18:02 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
> Then what you're saying is the interpreter is broken. It is not setting
> $tmpfile to the new value; rather it is keeping the old value.

> In your code, if $tmpfile were actually being to the new value, there is
> no way curl_opt() could get the old value.

> What happens if you echo $tmpfile before and after the statement:
> $tmpfile = $tempdir.'/CURL_var_.txt';

string: C:\Users\sv32\AppData\Local\Temp\CURC9BD.tmp
string: C:\Users\sv32\AppData\Local\Temp/CURL_var_.txt

then, because of the static
string: C:\Users\sv32\AppData\Local\Temp/CURL_var_.txt
string: C:\Users\sv32\AppData\Local\Temp/CURL_var_.txt

One cookie in temp: CURC9BD.tmp

Without static, I get 5 cookies, each matching the tempnam() filename. I copy/pasted $tmpfile in each location of the code, to make sure i was using the correct var. Does the tempnam() lock in the var somehow? again, If I comment the $tmpfile = tempnam() part, I get no cookie, with a before and after of
NULL
string: C:\Users\sv32\AppData\Local\Temp/CURL_var_.txt

You notice the back and forward slashes, I have never had an issue with these, using EasyPHP on Win, back and forward slashes seem to work identically, except in str compares, of course.

Same results local and live.
$cookfile = curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfile);
$cookjar = curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfile);
both come back true.
Re: php curl only saves a cookie with tempnam() [message #180792 is a reply to message #180778] Tue, 19 March 2013 04:46 Go to previous messageGo to next message
doug[1] is currently offline  doug[1]
Messages: 10
Registered: March 2013
Karma: 0
Junior Member
Figured it out. tempnam() creates the file whether you use it or not. My curl to bitly wasnt giving me back a cookie, at least not all the time. So, when i was setting
$tmpfile = tempnam($tempdir, 'CURL_tmpfile');
it created the random named file.
then when i set
$tmpfile = $tempdir.'/CURL_var_.txt';
no cookie was created because bitly didnt send me one. I was not realizing that the tempnam() file was just an empty file created and then never used.

Gahhhh!
Thanks for the help.
Re: php curl only saves a cookie with tempnam() [message #180799 is a reply to message #180792] Tue, 19 March 2013 13:06 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/19/2013 12:46 AM, Doug Cassidy wrote:
> Figured it out. tempnam() creates the file whether you use it or not. My curl to bitly wasnt giving me back a cookie, at least not all the time. So, when i was setting
> $tmpfile = tempnam($tempdir, 'CURL_tmpfile');
> it created the random named file.
> then when i set
> $tmpfile = $tempdir.'/CURL_var_.txt';
> no cookie was created because bitly didnt send me one. I was not realizing that the tempnam() file was just an empty file created and then never used.
>
> Gahhhh!
> Thanks for the help.
>

Yes, it does. I didn't think about it being an empty file, either.

Glad you figured it out.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: all done
Next Topic: validation of a text box input...
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 14 17:22:10 GMT 2024

Total time taken to generate the page: 0.04094 seconds