不要只检查域名

发布于 2024-12-29 11:11:10 字数 812 浏览 1 评论 0原文

太棒了,又是我使用了这个功能。

我的功能可以正常工作。

function http_file_exists($url)
{
  $f = @fopen($url,"r");
  if($f)
  {
    fclose($f);
  return true;
  }
return false;
} 

这就是用法:

if ($submit || $preview || $refresh)
{
 $post_data['your_url'] = "http://www.google.com/this"; //remove the equals and url value if using in real post
  $your_url = $post_data['your_url'];
  $your_url_exists = (isset($your_url)) ? true : false;
  $your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

  if ($your_url_exists && http_file_exists($your_url) == true)
  {
    trigger_error('exists!');
  }

如何让它检查整个 url 而不仅仅是域名?例如http://www.google.com/this

Soooo it's me again with this function..

I have the function working.

function http_file_exists($url)
{
  $f = @fopen($url,"r");
  if($f)
  {
    fclose($f);
  return true;
  }
return false;
} 

And this is the usage :

if ($submit || $preview || $refresh)
{
 $post_data['your_url'] = "http://www.google.com/this"; //remove the equals and url value if using in real post
  $your_url = $post_data['your_url'];
  $your_url_exists = (isset($your_url)) ? true : false;
  $your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

  if ($your_url_exists && http_file_exists($your_url) == true)
  {
    trigger_error('exists!');
  }

How do I let it check the whole url and not the domain name only ? for example http://www.google.com/this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2025-01-05 11:11:10

测试的网址是 http://www.google.com/abadurltotest

下面的代码源 = 确定是否存在的最快方法是什么网址PHP 中存在吗?

function http_file_exists($url) 
{
  //$url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $url); 
$url_data = parse_url ($url);
    if (!$url_data) return FALSE;

   $errno="";
   $errstr="";
   $fp=0;

   $fp=fsockopen($url_data['host'],80,$errno,$errstr,30);

   if($fp===0) return FALSE;
   $path ='';
   if  (isset( $url_data['path'])) $path .=  $url_data['path'];
   if  (isset( $url_data['query'])) $path .=  '?' .$url_data['query'];

   $out="GET /$path HTTP/1.1\r\n";
   $out.="Host: {$url_data['host']}\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4)); //get http code
   fclose($fp);
   // if http code is 2xx or 3xx url should work
   return  ($code[0] == 2 || $code[0] == 3) ? TRUE : FALSE;
}

将顶部代码添加到functions_posting.php 中,替换之前的函数

if ($submit || $preview || $refresh)
{
 $post_data['your_url'] = " http://www.google.com/abadurltotest"; 
  $your_url = $post_data['your_url'];
  $your_url_exists = (request_var($your_url, '')) ? true : false;
  $your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

  if ($your_url_exists === true && http_file_exists($your_url) === false)
  {
    trigger_error('A bad url was entered, Please push the browser back button and try again.');
  }

url tested is http://www.google.com/abadurltotest

source of code below = What is the fastest way to determine if a URL exists in PHP?

function http_file_exists($url) 
{
  //$url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $url); 
$url_data = parse_url ($url);
    if (!$url_data) return FALSE;

   $errno="";
   $errstr="";
   $fp=0;

   $fp=fsockopen($url_data['host'],80,$errno,$errstr,30);

   if($fp===0) return FALSE;
   $path ='';
   if  (isset( $url_data['path'])) $path .=  $url_data['path'];
   if  (isset( $url_data['query'])) $path .=  '?' .$url_data['query'];

   $out="GET /$path HTTP/1.1\r\n";
   $out.="Host: {$url_data['host']}\r\n";
   $out.="Connection: Close\r\n\r\n";

   fwrite($fp,$out);
   $content=fgets($fp);
   $code=trim(substr($content,9,4)); //get http code
   fclose($fp);
   // if http code is 2xx or 3xx url should work
   return  ($code[0] == 2 || $code[0] == 3) ? TRUE : FALSE;
}

add the top code to functions_posting.php replacing previous function

if ($submit || $preview || $refresh)
{
 $post_data['your_url'] = " http://www.google.com/abadurltotest"; 
  $your_url = $post_data['your_url'];
  $your_url_exists = (request_var($your_url, '')) ? true : false;
  $your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);

  if ($your_url_exists === true && http_file_exists($your_url) === false)
  {
    trigger_error('A bad url was entered, Please push the browser back button and try again.');
  }
后eg是否自 2025-01-05 11:11:10

使用curl 并检查HTTP 状态代码。如果不是 200 - 很可能该 url 不存在或无法访问。

另请注意,这

$your_url_exists = (isset($your_url)) ? true : false;

没有任何意义。看来您想要

$your_url_exists = (bool)$your_url;

或只是检查 $your_url 而不是 $your_url_exists

Use curl and check the HTTP status code. if it's not 200 - most likely the url doesn't exist or inaccessible.

also note that

$your_url_exists = (isset($your_url)) ? true : false;

makes no sense. It seems you want

$your_url_exists = (bool)$your_url;

or just check $your_url instead of $your_url_exists

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文