Building a Form to Count Back Links Using Yahoo Inbound Links API

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.

At the end of the third part, I’ll give you a link which you can use to download all the files associated with this web application project.

Passing form variables to PHP via AJAX

The script uses captcha code that will pass the answer for the challenge using a PHP session, so at the top of the web form (file name: countuniquelinks.php), this code is placed:

<?php

session_start();

?>

Discussion of captcha generation is out of the scope of this article. However, you can read another article on this site for a detailed discussion of how to build a captcha system.  

Shown below this HTML web form screen shot is the source code required to produce the form.


<h3>This tool will count the number of back links coming from unique domains.</h3>

Please fill in required data and click "Submit" button.<br />

<br /><br />

<form action="/countuniquelinks/yahooapiajax.php" method="post" onsubmit="return false;">

Enter root domain URL ( for example: <a href="http://www.php-developer.org"><b>http://www.php-developer.org</b></a>). URL <b>should</b> start <i>http://</i> and <b>DO NOT include trailing slash at the end.</b><br />

<br />

<input style="background-color: #FFFFC0" type="text" name="domainurl" id="domainurl" size="50">

<br /><br />

Select whether to get unique backlinks to the entire domain or only to the homepage URL<br />

<input type="radio" name="options" id="options1" value="1" > Entire Site


<br />


<input type="radio" name="options" id="options2" value="2" > Only to the homepage URL


<br /><br />


<img src="/countuniquelinks/captcha.php" />


<br />


Enter the Captcha as shown above:


<br /> <br />


<input style="background-color: #FFFFC0" type="text" name="captcha" id="captcha" size="10">


<br /> <br />


<input type="submit" value="Submit" onClick="sendRequest()">


</form>


<br />


<a href="/countuniquelinks/countuniquelinks.php"><b>RESET FORM</b></a>


<br /><br />


RESULTS WILL BE SHOWN BELOW after pressing the submit button:


<br />


<br />


</font>


{mospagebreak title=AJAX and variables for web form}

The form variables (name and ID) are the following:

domainurl = (Using text field) This is the user domain URL input to the form.

Options = (Using radio buttons) This covers the result options. The value is 1 if the user is interested in getting the results for the backlinks pointing to the entire site, not just to one specific URL. Otherwise, it is assigned 2.

Captcha = (Using text field) This is the user captcha input value.

Submitting values to PHP via AJAX means that the page does not need to be reloaded or refreshed. The JavaScript code that will communicate to PHP is the following, which is placed above the HTML web form discussed earlier (at the <head> tag, where most JavaScripts are located).

<html>


<head>


<title>Count Unique Back Links using Yahoo API</title>


<script type="text/javascript" src="prototype.js"></script>


<script type="text/javascript">


function sendRequest() {


new Ajax.Request("/countuniquelinks/yahooapiajax.php",


{


method: ‘post’,


parameters: ‘domainurl=’+$F(‘domainurl’)+’&options1=’+$F(‘options1′)+’&options2=’+$F(‘options2′)+’&captcha=’+$F(‘captcha’),


onComplete: showResponse


});


}


function showResponse(req){


$(‘show’).innerHTML= req.responseText;


}


</script>


</head>


<body>


<font face="Courier" size="2">


Using the AJAX Prototype method, the form variables are passed as parameters as shown above. PHP provides the results via the showResponse function, which will be shown on the same page as the web form (and below it):

Source code:


<p id="show"></p>


</body>


</html>


The id=”show” is an indicator that AJAX will look to send the results back as provided by PHP, according to this query:


function showResponse(req){


$(‘show’).innerHTML= req.responseText;


}



Other things to note about the AJAX query provided above is the path to the PHP script:


new Ajax.Request("/countuniquelinks/yahooapiajax.php",


When the submit button is pressed, without reloading the page, AJAX will send variables to the PHP script “yahooapiajax.php” in the background, which will process the variables, validate them and then query Yahoo’s inlinks API.

For more details on the principles of building a PHP AJAX web form using prototype, check the article at the link.

{mospagebreak title=PHP Script Preliminary Form Processing}

This is the start of the PHP script (yahooapiajax.php) which first receives inputs from AJAX via $_POST. Again at the top of the PHP script, place the following:


<?php


session_start();


This is to let PHP capture the correct captcha answer generated by the captcha script: captcha.php

The first thing the PHP script should do is receive the variables passed as parameters on the AJAX query discussed earlier:


$domainurl =trim($_POST['domainurl']);


$options1 =trim($_POST['options1']);


$options2 =trim($_POST['options2']);


$usercaptchaanswer =trim($_POST['captcha']);


$correctcaptcha = $_SESSION['answer'];


The trim function is used to remove any unnecessary spaces in the user input. $_POST values from the form input are assigned to their respective PHP variables.

The next step is to validate the captcha, and to display an error (as well as terminate the script executing) if the captcha entered does not match the correct answer or f it is entered empty:


if (($correctcaptcha != $usercaptchaanswer) || (empty($usercaptchaanswer))) {


echo ‘ERROR: You have not entered captcha code or it is entered incorrectly.';


die ();


}


{mospagebreak title=Validating the Root Domain Name URL using PHP}

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.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye