parse/pass cookies from a server?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

parse/pass cookies from a server?

Post by moussa854 »

I am triying to get data from website and I am getting please enable cookies, any idae how to do this.

Code: Select all

 
<?php 
function LoadCURLPage($url, $agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) 
Gecko/20030624 Netscape/7.1 (ax)', $cookie = '', $referer = '', $post_fields = '', $return_transfer = 1, $follow_location = 1, $ssl = '', $curlopt_header = 0) 
{ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
if($ssl) 
{ 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
} 
curl_setopt ($ch, CURLOPT_HEADER, $curlopt_header); 
if($agent) 
{ 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
} 
if($post_fields) 
{ 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
} 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
if($referer) 
{ 
curl_setopt($ch, CURLOPT_REFERER, $referer); 
} 
if($cookie) 
{ 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
} 
$result = curl_exec ($ch); 
curl_close ($ch); 
return $result; 
} 
function extract_unit($string, $start, $end) 
{ 
$pos = stripos($string, $start); 
$str = substr($string, $pos); 
$str_two = substr($str, strlen($start)); 
$second_pos = stripos($str_two, $end); 
$str_three = substr($str_two, 0, $second_pos); 
$uit = trim($str_three); 
return $unit; 
} 
$url = 'http:www.mysite.com'; 
$data = LoadCURLPage($url); 
$string_one = '<title>'; 
$string_two = '</title>'; 
$info = extract_unit($data, $string_one, $string_two); 
echo 'title '.$info; 
?>
 
Last edited by Benjamin on Thu Apr 30, 2009 6:06 pm, edited 1 time in total.
Reason: Added code tags.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: parse/pass cookies from a server?

Post by it2051229 »

is the "please enable cookies" an error? I think you should configure your browser to enable or accept cookies.

if you're using firefox.. type in google "how to enable cookie in firefox"
if you're using IE .. type in google "how to enable cookie in internet explorer"
if you're using opera. type in google "how to enable cookie in opera"..

ok i think you got my point
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: parse/pass cookies from a server?

Post by Benjamin »

Actually, have a look at this:

Code: Select all

 
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 
curl_setopt($ch, CURLOPT_URL, 'http://site.com/page1.php');
$result1 = curl_exec($ch);
 
curl_setopt($ch, CURLOPT_URL, 'http://site.com/page2.php');
$result2 = curl_exec($ch);
 
curl_close($ch);
?>
 
From: http://us2.php.net/manual/en/function.c ... .php#85956

You'll need to do some research on the cURL CURLOPT_COOKIEJAR option.
Post Reply