PHP based on Curl background remote login square teaching system method _php skills

Source: Internet
Author: User
Tags curl php programming php regular expression

This article describes the PHP based on the Curl background remote login to the Senate teaching system method. Share to everyone for your reference, specific as follows:

Since last year to think how to solve this matter, this year finally to figure him out, but the verification code must be filled.

If you can get the automatic identification code like 360 tickets, it's okay. There's no more nonsense. Back to the point.

Here we need to use curl.

Design idea: First login page to get cookies, and then take cookies to find the server to verify the code. Finally, provide all the information the server needs.

(This kind of thinking is completely analog browser access page, the fundamental difference is that people also machine)

The Public Function index () {
 $ch = Curl_init ();
 curl_setopt ($ch, Curlopt_url, $url); Fill in the URL to the
 curl_setopt ($ch, Curlopt_header, 0);
 curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.0) ");
 curl_setopt ($ch, Curlopt_cookiejar, $cookie _file); The path of the main cookie, this save page cookie
 curl_setopt ($ch, curlopt_returntransfer,1);//return result auto output
 $response = curl_exec ($ch );
 Curl_close ($ch);


Access the first page of the Senate educational system, get the HTML in the pages, and the second is to get cookies.

The rest of you want to ask? What's the use of getting HTML?

If you look closely at the HTML of the home page of the Dean's system, you will find that there are many hidden fields, and these fields are changed.
So you simply get the HTML and then parse it, and then take the required fields out of the peach.

HTML structure

See no, then the hidden fields are all submitted data must, and not much.

Page effects

Next I want to put one of the most important questions, how to obtain the verification code?

At first I wanted to direct



No, it's okay? This verification code although is out, but this verification code does not belong to you, so when you save remote login, will tell you that the verification code error

How do you get your own verification code? At this time, naturally thought of is cookies.

How the server distinguishes the browser request is not the same person, look at the cookies inside the SessionID

Such You take the cookies you get on the homepage, and then find the server to your verification code, it will give the wrong?

Public Function getimg ()
{
    $url = "http://xxxx/CheckCode.aspx";
    $filedir = Site_path. " /tmp/cookies ";
    $cookie _file = $filedir. " /cookie.txt ";
    $ch = Curl_init ();
    curl_setopt ($ch, Curlopt_url, $url);
    curl_setopt ($ch, Curlopt_header, 0);
    curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.0) ");
    curl_setopt ($ch, Curlopt_cookiefile, $cookie _file);
    curl_setopt ($ch, curlopt_returntransfer,1);
    curl_setopt ($ch, Curlopt_timeout, "ten");
    Header ("Content-type:image/gif"); This depends on the different picture format, please note that
    Echo curl_exec ($ch);
    Curl_close ($ch);


Then call this method on the other page and you can get the captcha.

Curl PHP requests that PHP that is issued with your browser belong to two different threads, so their cookies are different.

So you can log in, but do not think so log in, you can operate all functions, then you think wrong. There is also a small detail.

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Related Article

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.