1
我使用curl遠程登錄到ssl網站,並使用框架codeigniter。有時我登錄成功,但有時登錄時,我收到響應會話過期。我不知道如何解決這個問題。PHP會話過期使用捲曲遠程登錄網站時有ssl
這是我的代碼。
public function index()
{
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$username = $this->input->post('username');
$password = $this->input->post('password');
//username and password of account
//set the directory for the cookie using defined document root var
//login form action url
$url="https://www.voipchief.com/login/";
$cookie_file_path = "/tmp/cookies.txt";
// begin script
$ch = curl_init();
// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// set first URL
curl_setopt($ch, CURLOPT_URL, $url);
// execute session to get cookies and required form inputs
$content = curl_exec($ch);
$fields='<form action="https://www.voipchief.com/login" method="post" class="form-detail">';
$fields.= $this->Get_form('<form action="https://www.voipchief.com/login" method="post" class="form-detail">', '<form', $content);
$fields.='</form>';
$test = $this->getInputs($fields);
$test['login[username]'] = $username;
$test['login[password]'] = $password;
$test['page_referrer'] = 'login';
$test['login[remember_me]'] =1;
// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($test);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $url);
// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
// perform login
$result = curl_exec($ch);
print $result;
}
$this->load->view('welcome_message');
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
function Get_form($filter1, $filter2, $html)
{
$content = '';
$filter = '#(?<=' . $filter1 . ').*(?=' . $filter2 . ')#imsU';
$a = preg_match($filter, $html, $co_non);
if($a) $content = $co_non[0];
return $content;
}
}
T嘗試過,但它不工作。 –