note 75092 added to ref.curl
| From: | alan at zeroasterisk dot com | Date: | Fri, 11 May 2007 20:04:23 +0000 |
| Subject: | note 75092 added to ref.curl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126419@lists.php.net to get a copy of this message | ||
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;
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 65.60.216.79
----
Manual Page -- http://www.php.net/manual/en/ref.curl.php
Edit -- https://master.php.net/note/edit/75092
Del: integrated -- https://master.php.net/note/delete/75092/integrated
Del: useless -- https://master.php.net/note/delete/75092/useless
Del: bad code -- https://master.php.net/note/delete/75092/bad+code
Del: spam -- https://master.php.net/note/delete/75092/spam
Del: non-english -- https://master.php.net/note/delete/75092/non-english
Del: in docs -- https://master.php.net/note/delete/75092/in+docs
Del: other reasons-- https://master.php.net/note/delete/75092
Reject -- https://master.php.net/note/reject/75092
Search -- https://master.php.net/manage/user-notes.php