When opening the other party web page with a FOPEN mode, because Fopen cannot send sessionID, some of the needs of the website cannot be obtained. You can use socket to perform this operation, simulate the header information of the HTTP
Reference Code
/ * * Get web content * Parameters: $ host [in] string * host name (for example: www.imsorry.com.cn) * Parameters: $ method [in] string * Submission method: post, get, head ... And add corresponding parameters (see RFC1945, RFC2068) * Parameters: $ STR [in] string * Submitted content * Parameters: $ sessid [in] string * php sessionId * * @ 网 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)) {echo "phpsessid = $ sessid"; FPUTS ($ FP) , "Cookie: phpsessid = $ sessid; path = /; / r / n");} if (Substr (TRIM ($ Method), 0, 4) == "POST") {FPUTS ($ FP, "Content- Length: "Strlen ($ STR)." / R / n "); // Don't forget the specified 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 = s TRPOS ($ response, "/ r / n / r / n"); // linux is "/ N / N" $ 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 /_/ =/&//#/.] ) {$ a [' location ") {$ a ['location" ] = $ matches [1];} $ a ['content'] = $ Entity; fclose ($ fp); returnid $ a;}
$ localHost = $ response = getWebcontent ($ localhost, "get $ page http / 1.0", ''); Echo $ response ['location']. $ response ['content']. "; echo $ response ['sessid ']. ""; Print_R ($ response); if (preg_match (' / error / .php / i ', $ response [' location ']) {echo "land failed";} else {echo "Landing success" ; / * // You can not access User.php, because without sessid parameters $ response = getWebcontent ("LocalHost", "Get /user.php http / 1.0", '', '); Echo $ Response [' Location ']. ""; // Results: error.php? Errcode = 2 * / / / can access User.php $ response = getWebcontent ($ localhost, "Get $ Page Http / 1.0",', $ response [' Sessid ']); Echo $ Response [' Location ']. ""; // Result: User.php Print_R ($ Response);
$ response = getWebContent ($ localhost, "get $ page http / 1.0", '', $ response ['sessid']); Echo $ response ['location']. ""; // Result: User.php Print_R ( $ cyponse);}
?>