PHP simulation Landing Campus Educational Administration network Management system

Source: Internet
Author: User
Tags curl sessions

Written in front: After using python for reptiles, found that it has not used my old line of PHP crawl, plus a senior dog has just started to meet the so-called pit * training, and can not go out to find an internship, so can only entertain themselves, just see the recent school operation of the public number, there are binding school number, Then query the functions of various things, and then want to imitate this function to do. Functional interface with the use of TP development, but also relatively fast, followed by the post code logic. This is only about the two days of Curl I studied. The realization process of simulating landing

Objective: To simulate the landing of school Administration network

Principle: Submit account password, verify through, save cookies, and then carry cookies to access other pages

Process:

1. Study the data stream of the website first

The school's academic network management system is the fruit, however I looked for the website of the fruit on the Internet, found that the school has made a unified certification landing, I would like to refer to the Internet on the simulation of the landing attempt failed

The school's certified login page is like this:


Then, using Firefox's firebug to analyze the entire login, remember to click to keep, the whole process will be more clear, because the page will redirect

As shown in the figure:



Click Post to view, we need to submit the data to have these


Then on the landing page I found these fields, and __requestverificationtoke is a security encryption means, each refresh is different, how to obtain, here can use Simple_html_dom for the first access to the landing page for a regular match to get

Next, I found that there are two times get data after post, after viewing the two times page redirection, and the cookie will change, the last access to the Academic Management System home page is the last generated cookie, so need to get the last redirect of cookies

The complete process is:

First get to landing page, obtain __requestverificationtoke value, save cookie

Construct the data to be submitted

Bring the saved cookie post to the login URL and save the cookie

Carry the saved cookie two times get access to the next URL, save the cookie

Log on to the home page with cookies

The above based on the firebug of the entire process analysis, here just the results are simply written out, in fact, the process I have been watching for a long time, pay attention to the request header information to carry the cookie value, as well as the response head of the location redirect, the whole process can be analyzed out

Use of 2.curl

In fact, there has been no contact with this function, is not very familiar with the English document for the first time also difficult to understand, so on the Internet to refer to other code, after all, imitation, and so cooked before going over the manual will be more familiar with the point

So, what you need to know about this is, simply explain:

Curlopt_useragent Forged Browser User_agent
Curlopt_header Displays response header information for 1 o'clock
Returns the content of the response when Curlopt_returntransfer is False
Support for Web redirection when Curlopt_followlocation is true
Curlopt_autoreferer automatically sets the Referre information in the header when redirected according to location
  when Curlopt_ssl_verifypeer is disabled curl will terminate authentication from the server. Setting up certificates using the CURLOPT_CAINFO option set the certificate directory with the Curlopt_capath option if Curlopt_ssl_verifypeer (the default is 2) enabled, Curlopt_ssl_ Verifyhost need to be set to true otherwise set to False.
Curlopt_cookiejar Save Cokkie, general use File
Curlopt_cookiefile Carry Cookie Files

3. Implementation process (here is the interception of the code snippet I wrote)

1 first get to landing page, obtain __requestverificationtoke value, save cookie

Self:: $sessions = dirname (__file__). $opid. TXT ';//Use file to save cookies,
        $html = new Simple_html_dom ();//Use this class, specific Baidu
        $ch = Curl_init ();
        Header ("Content-type:text/html;charset=utf-8");
        $options = Array (curlopt_url => ' http://***.***.edu.cn/jwweb/cas_dglg.aspx ',
            curlopt_useragent => ' Mozilla /4.0 (compatible; MSIE 7.0; Windows NT 5.2) ',
            curlopt_header => 0,//does not look at the head curlopt_returntransfer => true,//
            return response content
            Curlopt_ Followlocation => true,//Support redirection
            curlopt_ssl_verifypeer => false,//settings do not authenticate with server-side certificates
            curlopt_cookiejar=> Self:: $sessions,//Save Cookie
        );
        Curl_setopt_array ($ch, $options);
        $result = curl_exec ($ch);
        $html->load ($result);
        $array = $html->find (' input[name=__requestverificationtoken] ')//Find this token get the value
        @$__ Requestverificationtoken = UrlEncode ($array [0]->value);
2) construct the data to be submitted

$post = Array (
            token ' ReturnUrl ' obtained above the ' __requestverificationtoken ' => $__requestverificationtoken,//
            > ' HTTPS://**.**.EDU.CN/?APPID=JWXT ',//need to submit the data
            ' UserName ' => $username,//Your username
            ' Password ' => $ password//password
        );
3) <span style= "font-family:arial, Helvetica, Sans-serif;" > Bring saved cookies post to login URL, save cookie</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "php" > $post = Http_build_query ($post);//http_build_ Query is the form of data that constructs the data as a URL $options = array (curlopt_url => ' Https://***/user/login?) Returnurl=%2f%3fappid%3djwxt&appid=jwxt ', Curlopt_header => 0, Curlopt_autoreferer => tr UE,//Curlopt_returntransfer => 1, curlopt_encoding => ', Curlopt_httpheader =& Gt Array (' Referer:https://***/user/login? Returnurl=%2f%3fappid%3djwxt&appid=jwxt '), Curlopt_followlocation => false, Curlopt_post =& Gt
            True, Curlopt_ssl_verifypeer => false, Curlopt_cookiefile=>self:: $sessions,//Send last Cookie Curlopt_useragent => ' mozilla/4.0 (compatible; MSIE 7.0;
        Windows NT 5.2) ', @CURLOPT_POSTFIELDS => $post//submitted data);
        Curl_setopt_array ($ch, $options); $result = CURL_EXEC ($CH); 


4 carry a saved cookie two times to access the next URL, save the cookie (get URL has two times, the last time without impersonation, is redirected, as long as the support redirection, save cookies)

$options = Array (curlopt_url => ' https://***/?appid=jwxt ',
            curlopt_useragent => ' mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2) ',
            curlopt_header => 1,
            Curlopt_returntransfer => true,
            curlopt_followlocation => True,
            Curlopt_ssl_verifypeer => false,
            curlopt_httpheader => array (' Referer:https://***/user/login? Returnurl=%2f%3fappid%3djwxt&appid=jwxt '),
            curlopt_cookiefile=>self:: $sessions,
        );
        Curl_setopt_array ($ch, $options);
        $re = curl_exec ($ch);
        $html->load ($re);
        Preg_match_all ('/^location:\s* [^;] *)/mi ', $re, $mmm);//Pre-view the difference between success and unsuccessful pages, look for different points to determine the login status
        $res = $mmm [1][0];
        $state = substr ($res, 0,11) = = '/user/login '? 0:1;//to determine if the login was successful

5 Carry stored cookies to visit the homepage of the website

Here subsidy code, with get access


Program run results, as shown in figure:


The principle is probably like this, encapsulation, you can use the

Above






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.