note 75092 deleted from ref.curl by nlopess
| From: | nlopess@php.net | Date: | Sat, 12 May 2007 11:24:07 +0000 |
| Subject: | note 75092 deleted from ref.curl by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126434@lists.php.net to get a copy of this message | ||
Note Submitter: alan at zeroasterisk dot com
----
You ever want a simple auto-login... so one part of your application can log you into another?
Here's a simple CURL function which is meant to do that... specifically submit a login post and
get those cookies... then set the cookies on this page. should be simple enough to customize as you
need.
<?php
// code by Alan -- http://zeroasterisk.com/
function curl_login() {
// config
$data_to_login_with = array(
'log' => 'admin',
'pwd' => 'whateveryourpasswordis',
'rememberme' => 'forever',
'redirect_to' => '/'
);
$url='/wp-login.php';
$cookie_filename = '/tmp/tmp_cookie.txt';
$user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20060601
Firefox/2.0.0.1 (Ubuntu-edgy)';
// prep
foreach ( $data_to_login_with as $key => $val ) {
$data[] = urlencode($key).'='.urlencode($val);
}
// do stuff
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$re = curl_exec($ch);
curl_close($ch);
$dump[] = $re;
$cookieJar = file($cookie_filename);
if (is_array($cookieJar)) {
foreach ( $cookieJar as $line ) {
// can filter for more things here
if (strpos($line,'FALSE')!==false) {
$parts = explode(' ',$line);
if (isset($parts[5]) && !empty($parts[5])) {
$dump[] = trim($parts[5]). ' - '.trim($parts[6]);
setcookie(trim($parts[5]),trim($parts[6]), time()+60*60*24,'/');
}
}
}
}
if (count($dump)>1) {
return true;
} else {
return false;
}
}
?>