首頁 >後端開發 >php教程 >如何使用curl模拟登录ci框架项目?

如何使用curl模拟登录ci框架项目?

WBOY
WBOY原創
2016-06-06 20:42:271044瀏覽

curl模拟登录代码:

<code>header(“content-type:text/html;charset=’utf-8’”);
$login_url = ‘http://127.0.0.1/app/index.php/auth/do_login‘;//登录页地址

$post_fields = array();
$post_fields[‘loginfield’] = ‘user’;
$post_fields[‘loginsubmit’] = ‘true’;

$post_fields[‘user’] = ‘cj’;
$post_fields[‘pass’] = ‘a’;

$cookie_file = tempnam(‘./temp’,’cookie’);

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5’);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$res=curl_exec($ch);
echo $res;
curl_close($ch);

?></code>

处理登录代码:

<code>function do_login()
{
$username = $this->input->post(“user”);
$password = $this->input->post(“pass”);

   try {
       $this->load->model("User_Model");
       $user = $this->User_Model->find_user_byname(trim($username));
       if($user->pw != md5(trim($password))) throw new Exception("用户名或密码错误!");

       $this->load->library('session');
       $this->session->set_userdata(array(
                    'user_id'   => $user->tid,
                    'user_name' => $user->xm
               ));

       $msg['code'] = 1;
       $msg['msg']  =  "登录成功";
       $msg['user_id']  =  $user->tid;
       $msg['user_name']  =  $user->xm;
       $msg['session_id']   = $this->session->userdata('session_id');
   } catch (Exception $e) {
       $msg['code'] = 0;
       $msg['msg']  =  $e->getMessage();
   }
   echo json_encode($msg);
}</code>

采用curl模拟登录成功了,返回的$msg中的值都是正确的。但是随后调用
echo json_encode($this->session->userdata(“user_id”));这段代码却获取不到数据。

而正常的登录就可以正常获取到数据。

回复内容:

curl模拟登录代码:

<code>header(“content-type:text/html;charset=’utf-8’”);
$login_url = ‘http://127.0.0.1/app/index.php/auth/do_login‘;//登录页地址

$post_fields = array();
$post_fields[‘loginfield’] = ‘user’;
$post_fields[‘loginsubmit’] = ‘true’;

$post_fields[‘user’] = ‘cj’;
$post_fields[‘pass’] = ‘a’;

$cookie_file = tempnam(‘./temp’,’cookie’);

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5’);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$res=curl_exec($ch);
echo $res;
curl_close($ch);

?></code>

处理登录代码:

<code>function do_login()
{
$username = $this->input->post(“user”);
$password = $this->input->post(“pass”);

   try {
       $this->load->model("User_Model");
       $user = $this->User_Model->find_user_byname(trim($username));
       if($user->pw != md5(trim($password))) throw new Exception("用户名或密码错误!");

       $this->load->library('session');
       $this->session->set_userdata(array(
                    'user_id'   => $user->tid,
                    'user_name' => $user->xm
               ));

       $msg['code'] = 1;
       $msg['msg']  =  "登录成功";
       $msg['user_id']  =  $user->tid;
       $msg['user_name']  =  $user->xm;
       $msg['session_id']   = $this->session->userdata('session_id');
   } catch (Exception $e) {
       $msg['code'] = 0;
       $msg['msg']  =  $e->getMessage();
   }
   echo json_encode($msg);
}</code>

采用curl模拟登录成功了,返回的$msg中的值都是正确的。但是随后调用
echo json_encode($this->session->userdata(“user_id”));这段代码却获取不到数据。

而正常的登录就可以正常获取到数据。

再次调用页面的时候要把ci_session 这个cookie和 useragent头带上,IP也不能变,但你应该是同一台机器,可以不用考虑。另外,cookie默认会每隔5分钟更新一次,如果你是长时间请求,建议每次把服务器返回的新cookie 带到下一次请求中。

发现问题的所在了,原来是采用curl模拟登录ci框架项目后,无法在服务端保存session数据。
正常情况下,session文件是保存在服务端的C:\Documents and Settings\Administrator\Local Settings\Temp目录下的,比如文件名是sess_e7ced24a34353ad44d2ea41843ba786g,如果不是采用curl模拟登录,那么在执行保存session的代码后:
session_start();
$_SESSION["user_id"] = $user->tid;
在sess_e7ced24a34353ad44d2ea41843ba786g文件里面会生成session数据,如:
user_id|s:2:"20";

但是采用curl模拟登录之后,就算保存session的代码,在sess_e7ced24a34353ad44d2ea41843ba786g文件里面也不能保存任何session数据,这个session文件里面的内容都是空的。

这个是什么原因引起的?该如何解决呢?

问题的根本是CI2的Session是用cookie实现的~!根本没法保存Session数据~!
请看拙文一篇,优雅地使用CodeIgniter之Session类库

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn