Simulate login with PHP

Source: Internet
Author: User
Some people often ask questions about the title of the imitation login. The principle is very simple. you only need to keep the SessionID. today, I spent an hour writing a function for your reference, the header information returned by the website. SOURCE Generation

Some people often ask questions about the title of the imitation login. The principle is very simple. you only need to keep the SessionID. today, I spent an hour writing a function for your reference, the header information returned by the website.

Source code:

/*
* Get the webpage content
* Parameter: $ host [in] string
* Host name (for example, www.imsorry.com.cn)
* Parameter: $ method [in] string
* Submission method: POST, GET, HEAD... and add relevant parameters (for detailed syntax, see RFC1945, RFC2068)
* Parameter: $ str [in] string
* Submitted content
* Parameter: $ sessid [in] string
* PHP SESSIONID
*
* @ Return webpage content string
*/
Function GetWebContent ($ host, $ method, $ str, $ sessid = '')
{
$ Ip = gethostbyname ($ host );
$ Fp = fsockopen ($ ip, 80 );
If (! $ Fp) return;
Fputs ($ fp, '$ method \ r \ n ');
Fputs ($ fp, 'host: $ Host \ r \ n ');
If (! Empty ($ sessid ))
{
Fputs ($ fp, 'Cookie: PHPSESSID = $ sessid; path =/; \ r \ n ');
}
If (substr (trim ($ method), 0, 4) = 'post ')
{
Fputs ($ fp, 'Content-Length: '. strlen ($ str).' \ r \ n'); // do not forget to specify the Length.
}
Fputs ($ fp, 'Content-Type: application/x-www-form-urlencoded \ r \ n \ r \ n ');
If (substr (trim ($ method), 0, 4) = 'post ')
{
Fputs ($ fp, $ str. '\ r \ n ');
}
While (! Feof ($ fp ))
{
$ Response. = fgets ($ fp, 1024 );
}
$ Hlen = strpos ($ response, '\ r \ n \ r \ n'); // it is' \ n \ n' in LINUX'
$ Header = substr ($ response, 0, $ hlen );
$ Entity = substr ($ response, $ hlen 4 );
If (preg_match ('/PHPSESSID = ([0-9a-z]);/I', $ header, $ matches ))
{
$ A ['sessid '] = $ matches [1];
}
If (preg_match ('/Location: ([0-9a-z \_\? \=\\# \.])/I ', $ header, $ matches ))
{
$ A ['location'] = $ matches [1];
}
$ A ['content'] = $ entity;
Fclose ($ fp );
Return $;
}

/* Structure username, password string */
$ Str = ('username = test & password = test ');
$ Response = GetWebContent ('localhost', 'post/login. php HTTP/1.0 ', $ str );
Echo $ response ['location']. $ response ['content'].'
';

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.