Curl Impersonation Login 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);?>
To process the login 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 ("Username or password Error! "); $this->load->library (' Session '); $this->session->set_userdata (' user_id ' = ' $user->tid, ' User_nam E ' = $user->xm)); $msg [' code '] = 1; $msg [' msg '] = "Login Successful"; $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);}
The login succeeded with curl and the values in the returned $msg are correct. But then call
echo json_encode($this->session->userdata(“user_id”));
The code does not get the data.
and normal login can get the data normally.
Reply content:
Curl Impersonation Login 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);?>
To process the login 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 ("Username or password Error! "); $this->load->library (' Session '); $this->session->set_userdata (' user_id ' = ' $user->tid, ' User_nam E ' = $user->xm)); $msg [' code '] = 1; $msg [' msg '] = "Login Successful"; $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);}
The login succeeded with curl and the values in the returned $msg are correct. But then call
echo json_encode($this->session->userdata(“user_id”));
The code does not get the data.
and normal login can get the data normally.
Call the page again when you want to put ci_session
this cookie and useragent
headband, IP
also can not change, but you should be the same machine, you can not consider. In addition, the cookie
default is updated every 5 minutes, and if you are a long-time request, it is recommended to bring the new server back cookie
to the next request every time.
Found the problem, the original is to use the Curl simulation login CI framework project, the server can not save session data.
Normally, the session file is stored in the server's C:\Documents and settings\administrator\local Settings\Temp directory, such as the file name is Sess_ E7CED24A34353AD44D2EA41843BA786G, if you are not using curl to impersonate the login, then after executing the code to save the session:
Session_Start ();
$_session["user_id"] = $user->tid;
The session data is generated in the sess_e7ced24a34353ad44d2ea41843ba786g file, such as:
User_id|s:2: "20";
However, after using the Curl simulation login, even if you save the session code, in the sess_e7ced24a34353ad44d2ea41843ba786g file can not save any session data, the contents of the session file is empty.
What is the cause of this? How to solve it?
The root of the problem is that CI2 's session is made with cookies! There is no way to save session data!
Please take a look at the article, elegant use of the CodeIgniter session class library