preg_match 401访问被拒绝

时间:2012-12-08 15:42:30

标签: php preg-match http-status-code-401

我正在尝试使用preg_match检查“401 Access Denied”,如果找到,则echo“failed”,否则为“ok”。我似乎无法抓住正确的信息,所以我可以输出它。

HTTP/1.1 401 Access Denied Set-Cookie: cprelogin=no; 
HttpOnly; path=/; port=2082 Set-Cookie: 
cpsession=%3aXYBHer1OXxaqzCNVLHNR3ujtmdkZow6PYsnyE8daVUF1lq8fQBiFd7EeHJ99CK93; 
HttpOnly; path=/; port=2082 Server: cpsrvd/11.34.1.4 Content-type: text/html;

1 个答案:

答案 0 :(得分:2)

尝试类似

的内容
preg_match('#^HTTP\/1\.(0|1)\s401#i', $yourheaders, $match);

if(!empty($match) && !is_null($match[1])){
   echo "failed";
}else{
   echo "ok";
}

GOT:

  HTTP/1.1 500 Internal Server Error Set-Cookie: cprelogin=no;.
  HttpOnly; path=/; port=2082 Set-Cookie:.
  cpsession=%3aXYBHer1OXxaqzCNVLHNR3ujtmdkZow6PYsnyE8daVUF1lq8fQBiFd7EeHJ99CK93;.
  HttpOnly; path=/; port=2082 Server: cpsrvd/11.34.1.4 Content-type: text/html;

ok
Array
(
)

与401:

HTTP/1.1 401 Access Denied Set-Cookie: cprelogin=no; 
HttpOnly; path=/; port=2082 Set-Cookie: 
cpsession=%3aXYBHer1OXxaqzCNVLHNR3ujtmdkZow6PYsnyE8daVUF1lq8fQBiFd7EeHJ99CK93; 
HttpOnly; path=/; port=2082 Server: cpsrvd/11.34.1.4 Content-type: text/html;

failed
Array
(
    [0] => HTTP/1.1 401
    [1] => 1
)