Search code examples
phpphpbb

Don't check the domain name only


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


Solution

  • 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.');
      }