0

I am trying to validate if the input of a url, actually exists or not. I have been trying the following code, however I got no success. This is the following code:

Using cURL:

<?php


$url = 'https://github.com';

$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    echo "Url not working";
}
echo "true";


?>

Using get_headers

<?php
  
// Initialize an URL to the variable
$url = "https://www.geeksforgeeks.org";
  
// Use get_headers() function
$headers = @get_headers($url);
  
// Use condition to check the existence of URL
if($headers && strpos( $headers[0], '200')) {
    $status = "URL Exist";
}
else {
    $status = "URL Doesn't Exist";
}
  
// Display result
echo($status);
  
?>

I have searche both these answers on stackoverflow and other websites, and used these to check if the url I give actually exists. When I write down a non existing url, I would like it to output that the website does not exists, however, I always end up having the same output as an existing url, meaning that somethig might be wrong, although I cannot fully see it. By the first code, the output is always true, even if the url does not exist. By the second code, the output is always 'URL doesn't exist', even if the url actually exists. Am I doing something wrong? I am using PHP Version 7.4, is this tool still working? I apologize if the question is not clear.

  • 2
    So have you looked at the actual content of `$headers` to see if it meets your expectations and the `strpos` function would therefore be likely to work? From your description of the situation I don't see any evidence of any worthwhile debugging taking place. Also why are you using `@` to suppress errors? That's just going to hide problems from you. – ADyson Oct 25 '22 at 08:57
  • I tested your second code example with the URL `https://www.geeksforgeeks.org/kashkdjadh` and it returns `URL Doesn't Exist`. The code works as expected. – KIKO Software Oct 25 '22 at 08:57
  • @ADyson I used var_dump on $headers, output was bool(false), meaning that strpos is practically doing nothing. Again I am not a 100% sure. – Dylan Grech Oct 25 '22 at 09:10
  • @KIKOSoftware I used the exact same code with a url that exists and I am still getting 'URL doesn't exists'. I am not really sure where the underlying problem is. – Dylan Grech Oct 25 '22 at 09:11
  • As per the [manual](https://www.php.net/manual/en/function.get-headers.php), get_headers will return `false` if it fails for any reason. One reason that could potentially happen is if the domain you're trying to make a request to simply doesn't exist at all, or its server is offline - and therefore there's nothing there to provide any response headers to you. In that scenario, you can treat it as the URL not existing, I would think – ADyson Oct 25 '22 at 09:19
  • Also if you remove the @ then you might get an error or warning which might give you more info about the failure. Hiding useful error info is never a good idea. – ADyson Oct 25 '22 at 09:21
  • It could also be because of your server configuration. Remove the `@` before `get_headers()` See: [https://3v4l.org/c1bN5](https://3v4l.org/c1bN5). – KIKO Software Oct 25 '22 at 09:21
  • @KIKOSoftware im not sure if the link u sent me means that it is supposed to be working - All I am getting is the following error - Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: System error in /in/c1bN5 on line 7 - I did remove the @ at the beginning when I received the comment, however I received no error output – Dylan Grech Oct 25 '22 at 09:29
  • No it's not supposed to be working, Nico's point is similar to mine - it's an example of one reason why get_headers might return false, and why you shouldn't use @ because it might be suppressing a warning telling you more about why it failed. – ADyson Oct 25 '22 at 09:33
  • 1
    The link shows an example where it is not working because the server is not configured correctly. You will only receive error/warning messages in the browser if you [configured PHP to do this](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). Otherwise you need to look in the error log. – KIKO Software Oct 25 '22 at 09:34

0 Answers0