以www.verycd.com為例
在Firefox瀏覽器登入wuming88888888帳號為發送方
在chrome瀏覽器登入wuming1990帳號為接收方
分析發送方的表單
分析提交頁原始碼POST的資料 [php] view plain copy <?php require('./http.class.php'); $http=new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0'); $msg=array( 'formhash'=>'10fe754a', 'message'=>'你好', 'pmsubmit'=>true, 'pmsubmit_btn'=>'發送', 'refer'=>'http://home.verycd.com/space.php?do=pm&filter=privatepm', 'username'=>'wuming1990' ); file_put_contents('./res.html',$http->post($msg)); ?>
開啟res.html,分析原始碼 [php] view plain copy HTTP/1.1 301 Moved Permanently Server: nginx Date: Fri, 05 Dec 2014 06:57:05 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: close Set-Cookie: sid=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: member_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: member_name=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: pass_hash=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: rememberme=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: mgroupId=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: coppa=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: uchome_auth=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: uchome_loginuser=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Location: http://www.verycd.com/account/profile/ Set-Cookie: uchome__refer=cp.php%253Fac%253Dprofile; path=/; domain=.verycd.com 33fc
調試查看自己發送的是什麼內容。 [php] view plain copy 這時候我們可以分析出錯誤出現在第一行 列印該對象 Http Object ( [errno:protected] => 0 [errstr:protected] => [response:protected] => [url:protected] => Array ( [scheme] => http [host] => home.verycd.com [path] => /cp.php [query] => ac=pm&op=send&touid=0&pmid=0 [port] => 80 ) [version:protected] => HTTP/1.1 [fh:protected] => Resource id #3 [line:protected] => Array ( [0] => POST /cp.php HTTP/1.1 ) [header:protected] => Array ( [0] => Host:home.verycd.com [1] => Content-type:application/x-www-form-urlencoded [2] => Content-length:185 ) [body:protected] => Array ( [0] => formhash=10fe754a&message=%E4%BD%A0%E5%A5%BD&pmsubmit=1&pmsubmit_btn=%E5%8F%91%E9%80%81&refer=http%3A%2F%2Fhome.verycd.com%2Fspace.php%3Fdo%3Dpm%26filter%3Dprivatepm&username=wuming1990 ) )
[php] view plain copy 修改我們的http類 <pre name="code" class="php"> //http請求類的介面 interface Proto{ //串連url function conn($url); //發送get查詢 function get(); //發送post查詢 function post(); //關閉串連 function close(); } class Http implements Proto{ const CRLF="\r\n"; protected $errno=-1; protected $errstr=''; protected $response=''; protected $url=null; protected $version='HTTP/1.1'; protected $fh=null; protected $line=array(); protected $header=array(); protected $body=array(); public function __construct($url){ $this->conn($url); $this->setHeader('Host:'.$this->url['host']); } //此方法負責寫請求行 protected function setLine($method){ $this->line[0]=$method.' '.$this->url['path'].'?'.$this->url['query'].' '.$this->version; } //此方法負責寫頭資訊 public function setHeader($headerline){ $this->header[]=$headerline; } //此方法負責寫主體資訊 protected function setBody($body){ $this->body[]=http_build_query($body);; } //串連url function conn($url){ $this->url=parse_url($url); //判斷連接埠 if(!isset($this->url['port'])){ $this->url['port']=80; } $this->fh=fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); } //構造get請求的資料 function get(){ $this->setLine('GET'); $this->request(); return $this->response; } //構造post請求的資料 function post($body=array()){ //構造主體資訊 $this->setLine('POST'); //設定content-type $this->setHeader('Content-type:application/x-www-form-urlencoded'); //設定主體資訊,比GET不一樣的地方 $this->setBody($body); //計算content-length $this->setHeader('Content-length:'.strlen($this->body[0])); $this->request(); return $this->response; } //真正請求 function request(){ //把請求行,頭資訊,實體資訊 放在一個數組裡,便於拼接 $req=array_merge($this->line,$this->header,array(''),$this->body,array('')); $req=implode(self::CRLF,$req); // print_r($this); // echo $req; // exit; fwrite($this->fh,$req); while(!feof($this->fh)){ $this->response.=fread($this->fh,1024); } $this->close();//關閉串連 return $this->response; } //關閉串連 function close(){ fclose($this->fh); } }
產生如下POST /cp.php?ac=pm&op=send&touid=0&pmid=0 HTTP/1.1Host:home.verycd.comContent-type:application/x-www-form-urlencodedContent-length:185formhash=10fe754a&message=%E4%BD%A0%E5%A5%BD&pmsubmit=1&pmsubmit_btn=%E5%8F%91%E9%80%81&refer=http%3A%2F%2Fhome.verycd.com%2Fspace.php%3Fdo%3Dpm%26filter%3Dprivatepm&username=wuming1990 [php] view plain copy HTTP/1.1 200 OK Server: nginx Date: Fri, 05 Dec 2014 07:11:39 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: close Set-Cookie: sid=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: member_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: member_name=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: pass_hash=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: rememberme=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: mgroupId=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: coppa=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: uchome_auth=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: uchome_loginuser=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com Set-Cookie: uchome__refer=cp.php%253Fac%253Dpm; path=/; domain=.verycd.com 表示已成功,但是不完全,我們接著看res.html中的內容 在網頁中有如下內容:表明需要先登入以後才能操作
伺服器怎麼知道咱們沒登陸的。
http一個很重要的特點:無狀態,兩次請求之間沒有關係。
伺服器如何記住一個客戶。
建立cookie.