GDedieu92
GDedieu92

Reputation: 1

Manage cookies with cUrl

Hello i have a problem trying to store cookies in a txt file. my_cookies.txt is a txt in the same folder of this file.

I want to manage cookie before and after login, in this example facebook Then i want to insert that cookie in a database but first i need to put it in a .txt or atleast save

Obviously email and password dont be those. =)

<?php

/* STEP 2. define path of form_action */
$url = "https://facebook.com/login.php?login_attempt=1";
$email = "nn";
$password = "nn";

/* STEP 3. create a connection with curl, give post information and save cookies */
$handler = curl_init();
//insert in the handler the path
curl_setopt($handler, CURLOPT_URL, $url);

//insert in the handler parameters of post
curl_setopt($handler, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($password));

//insert in the handler option to allow sending post information
curl_setopt($handler, CURLOPT_POST,1);

curl_setopt($handler, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);

//load and save cookies generates in temp file
curl_setopt($handler, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($handler, CURLOPT_COOKIEFILE, "my_cookies.txt");

//catch information
curl_setopt ($handler, CURLOPT_RETURNTRANSFER, true);

//define agent
curl_setopt($handler, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");

$response = curl_exec ($handler);

curl_close($handler);

echo $response; 
?>

Thanks for your help =) and i hope that you can do anything to help me :(

Upvotes: 0

Views: 413

Answers (3)

Black0CodeR
Black0CodeR

Reputation: 787

You can assign cookies to a $cookies variable and use that in place,

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

and then after cookies' work is done in cURL process. you can do whatever you want to that variable($cookies).

TIP:

  1. For storing into .txt - you __FILE__ to store same directory or append __FILE__ with more path to store in another directory.
  2. You can use md5($_SERVER['REMOTE_ADDR']) for generating Separate use .txt
  3. For storing into Database, INSERT command will be okay.

Upvotes: -1

CodeAngry
CodeAngry

Reputation: 12985

PHP 5.3+

curl_setopt($handler, CURLOPT_COOKIEJAR, __DIR__."/my_cookies.txt");
curl_setopt($handler, CURLOPT_COOKIEFILE, __DIR__."/my_cookies.txt");

PHP 5.2-

curl_setopt($handler, CURLOPT_COOKIEJAR, dirname(__FILE__)."/my_cookies.txt");
curl_setopt($handler, CURLOPT_COOKIEFILE, dirname(__FILE__)."/my_cookies.txt");

Also use absolute paths for files. __DIR__ gives you that power!

Upvotes: 1

exussum
exussum

Reputation: 18550

facebook provide an API which is much less error prone than using curl to random facebook pages

To answer your actual question though this is the function you are looking for it reads the file in to a variable which you can then store

Upvotes: 1

Related Questions