Home arrow PHP arrow Page 4 - Building a Form to Count Back Links Using Yahoo Inbound Links API

Validating the Root Domain Name URL using PHP - PHP

In the first part of this three-part series, you learned the guiding concepts and design for building a PHP web application to count back links from unique domains using the Yahoo Site Explorer API. In this part, we will illustrate, in detail, how to do the programming for the PHP Ajax web form and the validation script.

TABLE OF CONTENTS:
  1. Building a Form to Count Back Links Using Yahoo Inbound Links API
  2. AJAX and variables for web form
  3. PHP Script Preliminary Form Processing
  4. Validating the Root Domain Name URL using PHP
By: Codex-M
Rating: starstarstarstarstar / 7
January 25, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

It is highly important to validate user input on the root domain URL after they have entered it on the web form. If this is not validated, or not in the proper URL syntax as required by the Yahoo Site Explorer API, the API won't be able to process the request, or it will return inaccurate data.

The API currently accepts URLs that start with a protocol (http:// or https://) and do not end with a trailing slash. Of course the URL should also be in the correct format (domain name + '.' + TLD). Examples of TLDs (top level domain) are .com, .gov, .net, .org.

The first thing to be checked is that the domain URL field is not empty:


if (empty($domainurl)) {


//name field is blank


echo 'ERROR: The homepage URL field is empty.';


die ();


}


If the URL is not empty, we will need to validate that the user correctly entered the protocol (http:// or https://) as well as whether or not there are trailing slashes on the end:


$httpprotocol= substr($domainurl, 0, 7);


$httpprotocolwrong=substr($domainurl, 0, 8);


$httpsprotocol =substr($domainurl, 0, 8);


$httpsprotocolwrong= substr($domainurl, 0, 9);


$trailingslash = substr($domainurl, -1);


$firstdot= strpos($domainurl, '.');


$offset=$firstdot + 1;


$seconddot= strpos($domainurl,'.',$offset);


The substr function will extract the first seven characters if the protocol of the root domain is http:// or extract the first eight characters if the protocol is https://.

Also, you need to check other errors commonly made by the user when inputting domain URLs, such as http:/// or https:///.

There should only be two slashes in the protocol. These are defined via:


$httpprotocolwrong=substr($domainurl, 0, 8);


$httpsprotocolwrong= substr($domainurl, 0, 9);

The presence of a trailing slash at the end of the URL is defined by:


$trailingslash = substr($domainurl, -1);


If there are no dots in the URL, then it is considered a malformed URL, which is defined by these PHP variables:


$firstdot= strpos($domainurl, '.');


$offset=$firstdot + 1;


$seconddot= strpos($domainurl,'.',$offset);


The actual validation script for detecting allowable and correct protocols:


if (!(($httpprotocol=='http://') || ($httpsprotocol=='https://'))) {


echo 'ERROR: Root domain URL protocol should either only be http:// or https://';


die ();


}


This means that, if the user input domain URL does not contain either http:// or https://, then an error will be displayed to them so they can correct the entry.

Detecting malformed protocol such as https:/// or http:/// follows a similar concept of validation:


if (($httpprotocolwrong=='http:///') || ($httpsprotocolwrong=='https:///')) {


echo 'ERROR: Root domain URL protocol should either only be http:// or https://';


die ();


}


If the user inputs a trailing slash at the end of the URL, then an error will also display:


if ($trailingslash=='/') {


echo 'ERROR: Do not include trailing slash "/" at the end of the URL';


die ();


}


Please note that all of the validating variables used in the above validation script are first defined earlier.

To determine whether or not the domain URL is malformed (for example: http://wwwphpdeveloperorg or http://www.phpdeveloperorg), the validation line will be:


if (($firstdot=='') && ($seconddot=='')) {


echo 'ERROR: Malformed domain root URL';


die ();


}


Finally, one of the additional user input items is the options (whether to get backlinks data pointing to the entire site or just to a specific URL):


if ($options1==1) {


$options=1;


}


elseif ($options2==2) {


$options='';


}


else {


echo 'ERROR: The options field is empty.';


die ();


}


According to the Yahoo Site Explorer API guide, if the entire_site parameter is set to "1," then it will return backlinks pointing to the entire site; otherwise, if left empty "", then it will retrieve back links pointing to that specific URL only. These definitions have been included above. So if the $options1 variable value from the form is 1, assign 1 to the final $options variable (to be passed to the Yahoo inlinks API); otherwise, if it is 2, then assign it to be empty ""."

In the third part, you will learn the PHP script pertaining to the details of API communication and data analysis.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap

Dev Shed Tutorial Topics: