burger menu icon
WillMaster

WillMasterBlog > PHP

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Testing a URL

Every so often, a person just wants to know if a URL is valid.

This is especially true for website programmers. URLs are used frequently. It may be for a redirect. Or it may be a thank-you page URL. Perhaps the script is designed to post data to a URL.

In those cases, depending on the script you are writing, it may be prudent to test the URL's validity before you engage with it.

The TestWebPageURL() PHP function is used to verify a URL is valid and reachable.

To use the function, include the TestWebPageURL() function within your script. Then call the function with a URL to test. The function returns an array with information it collected, including the status code.

Here is the source code for the TestWebPageURL() function.

function TestWebPageURL($url)
{
   $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER         => false,
      CURLOPT_CONNECTTIMEOUT => 120,
      CURLOPT_TIMEOUT        => 120,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS      => 10,
   );
   $ch = curl_init($url);
   curl_setopt_array($ch,$options);
   curl_exec($ch);
   $info = curl_getinfo($ch);
   $info['errno'] = curl_errno($ch);
   $info['errmsg'] = curl_error($ch) ;
   curl_close($ch);
   return $info;
} # function TestWebPageURL()

A demonstration PHP script comes with this article. It uses TestWebPageURL(). The script is copy and paste.

The demonstration script displays the HTTP status code of the URL you tested. It prints any error messages the test encountered. The script also notes the destination URL if any redirects were encountered during the test. At the end, the demonstration script publishes an array of information that was gathered during the test.

To use the demonstration script, copy the source code and save it with any *.php file name, testURL.php for example. Then upload it to your server.

<?php
/*
Test URL Demonstration
Version 1
May 5, 2026
Will Bontrager Software LLC
https://www.willmaster.com/
*/

if(empty($_GET['url']))
{
   $ta = explode('/',$_SERVER['PHP_SELF']);
   $thisScript = array_pop($ta);
   echo "Use parameter name 'url' and value of the URL to test. Example:<br><span style='font-size:130%;font-family:monospace;'>$thisScript?url=https://example.com/testing.php";
   exit;
}

// test the URL.
$info = TestWebPageURL($_GET['url']);

// echo this if there was a redirect.
if( $info['url']!=$_GET['url'] )
{
   echo <<< CHUNK
<div style="white-space:pre-wrap;">
URL <b>{$_GET['url']}</b>
redirects to <b>{$info['url']}</b>
</div>
CHUNK;
}

// always echo this.
echo <<< CHUNK
<div style="white-space:pre-wrap;">
URL <b>{$info['url']}</b> returns HTTP status code: <b>{$info['http_code']}</b>
</div>
CHUNK;

// echo this if there is an error message.
if( $info['errno']>0 )
{
   echo <<< CHUNK
<div style="white-space:pre-wrap;">
Error: <b>{$info['errmsg']}</b>
</div>
CHUNK;
}

// echo the entire information array.
echo '<pre>'.print_r($info,true).'</pre>';

function TestWebPageURL($url)
{
   $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER         => false,
      CURLOPT_CONNECTTIMEOUT => 120,
      CURLOPT_TIMEOUT        => 120,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS      => 10,
   );
   $ch = curl_init($url);
   curl_setopt_array($ch,$options);
   curl_exec($ch);
   $info = curl_getinfo($ch);
   $info['errno'] = curl_errno($ch);
   $info['errmsg'] = curl_error($ch) ;
   curl_close($ch);
   return $info;
} # function TestWebPageURL()
?>

After it is uploaded, type the URL of the demonstration script into your browser. Append ?url= and the URL to test.

Example: testURL.php?url=https://example.com/testing.php

When you run the script, it displays data as described further above.

Any values in the array of information that the TestWebPageURL() function gathers may be used in your PHP script. Here is an example array of information.

Array
(
    [url] => https://www.willmaster.com/
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 406
    [request_size] => 106
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 0.721025
    [namelookup_time] => 0.006239
    [connect_time] => 0.152778
    [pretransfer_time] => 0.479417
    [size_upload] => 0
    [size_download] => 29208
    [speed_download] => 40510
    [speed_upload] => 0
    [download_content_length] => 29208
    [upload_content_length] => -1
    [starttransfer_time] => 0.72038
    [redirect_time] => 0.316062
    [redirect_url] => 
    [primary_ip] => 66.33.193.44
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 10.1.10.152
    [local_port] => 49485
    [http_version] => 3
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 478744
    [connect_time_us] => 152778
    [namelookup_time_us] => 6239
    [pretransfer_time_us] => 479417
    [redirect_time_us] => 316062
    [starttransfer_time_us] => 720380
    [total_time_us] => 721025
    [errno] => 0
    [errmsg] => 
)

To reiterate, TestWebPageURL() PHP function is used to verify a URL is valid and reachable. It returns an array of information that may be used in your PHP scripts.

To use the function, include TestWebPageURL() within your script. Then call the function with a URL to test.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

The HTML address Tag

The address tag is used to present an address to the site user. It also makes the address available to website search engines.

Rotate

The CSS 'rotate' property is a versatile way to rotate elements.

Building a URL to Self With PHP

PHP has all the parts to build the URL it was accessed with.

Easier Field Information Change

How to "select the field when the field is tapped into" for your own forms.

File Uploader

This file uploader is complete as one PHP script. No external modules or classes are required. Optional login for private use.

Fun Jumper

This fun jumper is easy to implement. Paste the code into one of your web pages. When you are ready to define your own content, replace the image tag.

The HTML optgroup Tag

The HTML <optgroup> tag allows you to group items in a dropdown list.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC