1

我正在尝试使用 preg_match 来检查“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;
4

1 回答 1

2

尝试类似的东西

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

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

得到:

  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
)
于 2012-12-08T15:44:59.290 回答