当使用curl远程登录网站有ssl时,PHP会话已过期

时间:2014-02-22 05:56:18

标签: php codeigniter session curl ssl

我使用curl远程登录ssl网站,我使用框架codeigniter。有时我登录成功,但有时当我登录时,我收到响应是会话过期。我不知道如何解决这个问题。

这是我的代码。

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;
}

}

1 个答案:

答案 0 :(得分:0)

第一次请求后关闭卷曲。否则它不会将cookie保存到文件中。这意味着CURLOPT_COOKIEFILE正在获取空文件。

curl_close($ch);

之后打开另一个curl实例进行登录调用。确保这次您使用以下登录请求。

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);