Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:18:26+00:00 2026-06-07T03:18:26+00:00

I have made a simple function to validate URL’s submitted through a textarea (1

  • 0

I have made a simple function to validate URL’s submitted through a textarea (1 link per line):

function validate_urls($value)
{
    //final array of links
    $links = array();

    $value = array_map(function($a) use (&$links){
        $a = trim($a);
        if(strlen($a) !== 0 and (strpos($a, 'http') !== 0 or strpos($a, 'https') !== 0)){
            $a = 'http://'.$a;
        }
        $url = parse_url($a,PHP_URL_HOST);
        if($url != null and !in_array($a, $links) and filter_var($a, FILTER_VALIDATE_URL) !== false and checkdnsrr($a)){
            $links[] = $a;
        }
        return false;
    }, explode("\n",$value));
    return $links;
}

var_dump(validate_urls($_POST['links']);

What this does is check if

  • the URL is valid
  • the URL is active
  • the URL is not a duplicate

The thing is, how come it doesn’t work (returns an empty array)? I have checked every checking and it should work but it doesn’t. Sorry if the code is messy i’m still trying to learn.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:18:28+00:00Added an answer on June 7, 2026 at 3:18 am
    if(strlen($a) !== 0 and (strpos($a, 'http') !== 0 or strpos($a, 'https') !== 0)){
    

    A and B or C does NOT translate to (A and B) or (A and C) as AND has a higher precedence than OR. So you’d want to change that to A and (B or C).


    The docs on FILTER_VALIDATE_URL state »Note that the function will only find ASCII URLs to be valid;«. So this is a pretty restrictive option. It adheres to the specification of URLs given in RFC 2396 which has been superseded by RFC 3986.

    Without having looked into this filter more thoroughly, these two pieces of information are sufficient (to me) to mark that filter as utterly useless.


    checkdnsrr($a)
    

    is testing the whole URL rather than just the host. Even if you’d be checking the host, you’d be looking for an MX record (i.e. if said host is accessible by mail). A would be checking if that host has an IP set, CNAME would be checking if the host is an alias of another DNS record, …. You’re probably looking for NS which would check if that host has got any DNS record at all.

    So if you changed your check to checkdnsrr($url, "NS") you would be validating if the host component of that URL is actually known to DNS. You are NOT checking if that host is actually listening on the specified port. And you’re NOT checking if the given resource (e.g. /foo/bar.html) exists.


    If you wanted to make sure an URL actually points to something useful, you’d have to make a HEAD request and check the response. You can do that easily with curl. If curl is not available, you could implement a simple HTTP client yourself using fsockopen() – with the disadvantage of not being able to speak HTTP (HTTP through SSL) and having to implement redirection following and similar stuff yourself. Short: you don’t want to go down that road.

    That said, there is also a performance problem up ahead. The HTTP requests are done synchronously. Should a host be failing to reply in an acceptable time frame, your script might time out – or at least take ages to finish, depending on the number of URLs you’re checking and the quality of service behind them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple Line Chart: Which is showing 4 grid-lines at points
So I have made this simple interface: package{ public interface GraphADT{ function addNode(newNode:Node):Boolean; }
I have a JQuery, Javascript, HTML website. I made a simple function to strike
I have made a simple javascript function that gets executed on button click -
I have made a simple chat application which uses long-polling approach using jquery, function
I have made simple encryption/decryption method in php that I'm trying to move to
OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
I have made a simple chat application in PHP/Ajax/MySQL. I am regularly calling these
I have made a simple code for ajax to call a page but it
I have made a simple test application for the issue, two winforms each containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.