Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8687 > unrolled thread

php curl only saves a cookie with tempnam()

Started bydoug@dougcassidy.com
First post2013-03-16 11:23 -0700
Last post2013-03-19 09:06 -0400
Articles 11 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  php curl only saves a cookie with tempnam() doug@dougcassidy.com - 2013-03-16 11:23 -0700
    Re: php curl only saves a cookie with tempnam() Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-16 18:48 -0400
      Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-16 23:46 -0700
        Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-16 23:52 -0700
        Re: php curl only saves a cookie with tempnam() Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-17 10:03 -0400
          Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-17 09:42 -0700
            Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-17 10:16 -0700
              Re: php curl only saves a cookie with tempnam() Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-17 21:13 -0400
                Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-18 11:02 -0700
                  Re: php curl only saves a cookie with tempnam() Doug Cassidy <doug@dougcassidy.com> - 2013-03-18 21:46 -0700
                    Re: php curl only saves a cookie with tempnam() Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 09:06 -0400

#8687 — php curl only saves a cookie with tempnam()

Fromdoug@dougcassidy.com
Date2013-03-16 11:23 -0700
Subjectphp curl only saves a cookie with tempnam()
Message-ID<dcd1e0bf-0bc9-432b-b089-e05e1f8bfd93@googlegroups.com>

$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?

[toc] | [next] | [standalone]


#8696

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-03-16 18:48 -0400
Message-ID<ki2sol$hs7$1@dont-email.me>
In reply to#8687
On 3/16/2013 2:23 PM, doug@dougcassidy.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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8702

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-16 23:46 -0700
Message-ID<caf1db94-c7aa-46ca-a908-81ff3590459f@googlegroups.com>
In reply to#8696
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;
}

[toc] | [prev] | [next] | [standalone]


#8703

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-16 23:52 -0700
Message-ID<5427b02d-ff4c-4a4f-935d-bc97e58991fb@googlegroups.com>
In reply to#8702
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'

[toc] | [prev] | [next] | [standalone]


#8706

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-03-17 10:03 -0400
Message-ID<ki4icg$g1u$1@dont-email.me>
In reply to#8702
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8708

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-17 09:42 -0700
Message-ID<5e24e972-cbfa-4c70-8f83-cc87cd3d4216@googlegroups.com>
In reply to#8706
> 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).'&format='.$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.

[toc] | [prev] | [next] | [standalone]


#8709

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-17 10:16 -0700
Message-ID<0dfdd4d7-27e0-42eb-a753-d1ce0f54abc6@googlegroups.com>
In reply to#8708
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

[toc] | [prev] | [next] | [standalone]


#8710

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-03-17 21:13 -0400
Message-ID<ki5pk3$dq6$1@dont-email.me>
In reply to#8709
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8714

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-18 11:02 -0700
Message-ID<b7c5cb83-5ab4-43b8-ab45-f7201f4cac8b@googlegroups.com>
In reply to#8710
> 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.

[toc] | [prev] | [next] | [standalone]


#8728

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-03-18 21:46 -0700
Message-ID<1a25d75a-a391-404e-9cb4-5c9dedda5486@googlegroups.com>
In reply to#8714
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.

[toc] | [prev] | [next] | [standalone]


#8735

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-03-19 09:06 -0400
Message-ID<ki9nob$2ll$2@dont-email.me>
In reply to#8728
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@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web