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'].'
';