All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Check URL Existence Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Dikshita

In this tutorial we will show you how to check url existence using PHP.There are many times when user submit url for information and other user won't be able to go to that webpage just because the url does not exist and this will also decrease website SEO.

So in this tutorial we will solve that problem by checking submitted URL if it is exist or not.You may also like Detect URL Using jQuery.

Check URL Existence Using PHP

To Check URL Existence It Takes Only Two Steps:-

  1. Make a HTML file to send URL
  2. Make a PHP file to check URL

Step 1. Make a HTML file to send URL

We make a HTML file and save it with a name send_url.html

<html>
<body>
<div id="wrapper">
<div id="url_form">
 <form method="post" action="check_url.php">
 Enter URL : <input type="text" name="url">
 <input type="submit" name="submit_url" value="CHECK URL">
</form>
</div>

</div>
</body>
</html>

I n this step we create a form to enter URL for checking by user.

Step 2. Make a PHP file to check URL

We make a PHP file and save it with a name check_url.php

<?php
if(isset($_POST['submit_url']))
{
 $url = $_POST['url'];
 $url_headers = @get_headers($url);
 if(strpos($url_headers[0],'200'))
 {
  $exist = "URL Exist";
 }
 else 
 {
  $exist = "URL Does Not Exist";
 }
}
?>

In this step we get the URL value submitted by user and then by using PHP get_headers() function we get all the headers of that URL and it return headers array and then we check '200' is present in array's 1st value if yes then the URL is exist and running and if not URL is not running or exist.'200' is URL status '200' means URL is running.

You may also like Convert URL Text Into Link Using JavaScript.

Thats all, this is how to check url existence using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on url exists php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪