How can I use curl to simulate logon to a ci framework project?

Source: Internet
Author: User
Curl simulated logon code: {code...} processes the logon code: {code...} simulates Successful Logon using curl. The returned $ msg value is correct. But then call echojson_encode ($ this-& amp; gt; session-& amp; gt; userdata ("user_id"); this Code cannot get... curl simulated logon code:

Header ("content-type: text/html; charset = 'utf-8'"); $ login_url = 'HTTP: // 127.0.0.1/app/index. php/auth/do_login '; // logon page address $ post_fields = array (); $ post_fields ['loginfield'] = 'user '; $ post_fields ['loginsubmit '] = 'true'; $ post_fields ['user'] = 'cj'; $ post_fields ['pass'] = 'a '; $ cookie_file = tempnam ('. /temp ', 'cookier'); $ ch = curl_init ($ login_url); curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows; U; Wi Ndows 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, batch, 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); c Url_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); $ res = curl_exec ($ ch); echo $ res; curl_close ($ ch);?>

Process the logon 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 ("the user name or password is incorrect! "); $ This-> load-> library ('session '); $ this-> session-> set_userdata (array ('user _ id' => $ user-> tid, 'user _ name' => $ user-> xm )); $ msg ['code'] = 1; $ msg ['msg '] = "Logon 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 );}

When a successful login is simulated using curl, the returned $ msg value is correct. However
echo json_encode($this->session->userdata(“user_id”));This Code cannot obtain data.

Normally, data can be obtained through normal logon.

Reply content:

Curl simulated logon code:

Header ("content-type: text/html; charset = 'utf-8'"); $ login_url = 'HTTP: // 127.0.0.1/app/index. php/auth/do_login '; // logon page address $ post_fields = array (); $ post_fields ['loginfield'] = 'user '; $ post_fields ['loginsubmit '] = 'true'; $ post_fields ['user'] = 'cj'; $ post_fields ['pass'] = 'a '; $ cookie_file = tempnam ('. /temp ', 'cookier'); $ ch = curl_init ($ login_url); curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (Windows; U; Wi Ndows 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, batch, 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); c Url_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); $ res = curl_exec ($ ch); echo $ res; curl_close ($ ch);?>

Process the logon 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 ("the user name or password is incorrect! "); $ This-> load-> library ('session '); $ this-> session-> set_userdata (array ('user _ id' => $ user-> tid, 'user _ name' => $ user-> xm )); $ msg ['code'] = 1; $ msg ['msg '] = "Logon 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 );}

When a successful login is simulated using curl, the returned $ msg value is correct. However
echo json_encode($this->session->userdata(“user_id”));This Code cannot obtain data.

Normally, data can be obtained through normal logon.

When you call the page againci_sessionThis cookie anduseragentTop of the headband,IPIt cannot be changed, but you should be the same machine. In addition,cookieIt is updated every 5 minutes by default. If you have a long request, we recommend that youcookieTo the next request.

The problem was found. After using curl to simulate login to the ci framework project, session data cannot be saved on the server.
Normally, the session file is stored in the C: \ Documents ents and Settings \ Administrator \ Local Settings \ Temp directory on the server. For example, the file name is sess_e7ced24a341_ad44d2ea41843ba786g. If you do not use curl to simulate logon, after executing the code for saving the session:
Session_start ();
$ _ SESSION ["user_id"] = $ user-> tid;
Session data is generated in the sess_e7ced24a341_ad44d2ea41843ba786g file, for example:
User_id | s: 2: "20 ";

However, after Simulated logon using curl, even if the session code is saved, no session data can be saved in the sess_e7ced24a341_ad44d2ea41843ba786g file, and the content in the session file is empty.

What is the cause? How can this problem be solved?

The problem is that CI2's Session is implemented using cookies ~! No way to save Session data ~!
For more information, see the Session class library of CodeIgniter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.