Solved

PHP Validate URL Field. Make sure i contains a certain website URL

Posted on 2014-01-25
10
433 Views
Last Modified: 2014-01-25
I have this validation setup:

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
  {
  $websiteErr = "Invalid URL";
  }

Open in new window


How do i make sure that url contains lets say "http://facebook.com/" in the url inputed?
0
Comment
Question by:jporter80
  • 4
  • 3
  • 3
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Use strips after you validate the URL format:

if (strpos (strtolower ($url), "facebook.com") !== false)
{
// URL contains facebook.com
}
if you want to make sure its in the domain part of the url, then use parse_url first.
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Sorry, auto correct on my phone changed strpos to strips.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
http://www.damnyouautocorrect.com/

You can do several things depending on what you want to accomplish.  The meaning of "validate" could be pretty broad.  Do you care if there is a valid URL that does not have a corresponding resource on HTTP or FTP?  Do you care if the URL has the wrong protocol? For example, if you have http://facebook.com, it does not point to https://facebook.com and will get rewritten to https://www.facebook.com

You might find this article helpful:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 

Author Comment

by:jporter80
Comment Utility
It should require http:// or https:// only.  I dont care if has a subdomain or not.. but must require a certain TLD is entered.

Help?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Do you understand the regular expression you posted?  If not, just tell us in plain language exactly what you want to find in the acceptable URLs and we can probably help from that.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:jporter80
Comment Utility
no not completely.. regular expression always throws me for a loop.  Basically:

Okay Inputs:
http://exactdomain.com/someother stuff
https://exactdomain.com/somother stuff
https://anysubdomain.exactdomain.com/somother stuff
http://anysubdomain.exactdomain.com/somother stuff

Not okay
exactdomain.com/someother stuff
anysubdomain.exactdomain.com/someotherstuff
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Regular Expressions always throw everybody for a loop!  I find them a little easier to read and understand if I deconstruct them into the components.  See lines 47-85

But even with that: http://xkcd.com/1171/

<?php // RAY_temp_jporter80.php
error_reporting(E_ALL);
echo "<pre>";


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28348079.html
// REF http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html


/* DESCRIPTION OF THE PROBLEM
Okay Inputs:
http://exactdomain.com/someother stuff
https://exactdomain.com/somother stuff
https://anysubdomain.exactdomain.com/somother stuff
http://anysubdomain.exactdomain.com/somother stuff

Not okay
exactdomain.com/someother stuff
anysubdomain.exactdomain.com/someotherstuff
*/


// TEST DATA IS AN ARRAY OF INDIVIDUAL TEST ARRAYS
// KEY = EXPECTED, VALUE = TEST-DATA
$targets
= array

// KNOWN BAD SHOULD NOT MATCH
(  array( "" => "exactdomain.com/someother stuff"
), array( "" => "anysubdomain.exactdomain.com/someotherstuff"

// KNOWN GOOD SHOULD MATCH
), array( "http://exactdomain.com/someother"              => "http://exactdomain.com/someother stuff"
), array( "https://exactdomain.com/somother"              => "https://exactdomain.com/somother stuff"
), array( "https://anysubdomain.exactdomain.com/somother" => "https://anysubdomain.exactdomain.com/somother stuff"
), array( "http://anysubdomain.exactdomain.com/somother"  => "http://anysubdomain.exactdomain.com/somother stuff"
)
)
;


// A SIGNAL STRING PREPARED FOR USE IN A REGULAR EXPRESSION
$signal = preg_quote('exactdomain.com');


// A REGEX THAT FINDS URLS AND DOMAIN SUBSTRINGS
$regex
= '#'         // REGEX DELIMITER

. '\b'        // ON WORD BOUNDARY

. '('         // START GROUP
. 'https?'    // HTTP OR HTTPS
. ')'         // END GROUP
. '{1}'       // EXACTLY ONE OF THIS GROUP

. '('         // START GROUP
. '://'       // COLON, SLASH, SLASH
. ')'         // END GROUP
. '{1}'       // EXACTLY ONE OF THIS GROUP

. '('         // START GROUP
. '[A-Z0-9]'  // A SUBDOMAIN
. '+?'        // INDETERMINATE LENGTH
. '\.'        // A DOT (ESCAPED)
. ')'         // END GROUP
. '??'        // ZERO OR ONE OF THIS GROUP, UNGREEDY

. '('         // START GROUP
. $signal     // THE DOMAIN WE WANT TO FIND
. ')'         // END GROUP

. '('         // GROUP
. '/{1}'      // ONE SLASH
. '.*?'       // URL PATHS (NOT WHITE SPACE)
. ')'         // END GROUP

. '('         // GROUP
. '[\S\B]'    // NOT WHITESPACE, NOT WORD BOUNDARY
. '{0,}'      // URL PATH OR NOTHING
. ')'         // END GROUP

. '#'         // REGEX DELIMITER
. 'i'         // CASE-INSENSITIVE
;

// TEST THE DATA STRINGS IN THE SUB-ARRAYS
foreach ($targets as $arr)
{
    foreach ($arr as $expected => $target)
    {
        preg_match_all($regex, $target, $match);

        // SHOW WHAT HAPPENED
        foreach ($match[0] as $matched)
        {
            // NO OUTPUT IF THE TEST WORKED AS EXPECTED
            if ($matched == $expected) continue;

            // EXPOSITION IF THE TEST DID NOT WORK AS EXPECTED
            echo PHP_EOL;
            echo "<b>EXPECT:</b> $expected";
            echo PHP_EOL;
            echo "<b>INPUTS:</b> $target";
            echo PHP_EOL;
            echo "<b>REGEXP:</b> $regex";
            echo PHP_EOL;
            echo "<b>OUTPUT:</b> ";
            print_r($match[0]);
            echo PHP_EOL;
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:jporter80
Comment Utility
thanks for the excellent education on regex.. definitely need to practice that.. it can be so helpful.  This worked like a charm
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
While I understand that regex might handle validation for you, you should really consider using parse_url if you want to examine different parts of a URL:

http://us3.php.net/parse_url

It -is- meant for that kind of thing.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now