So we all know Alexa puts everyone’s website into a ranking system against each other, but is rather used as a guidance where your site stands in the world as for some people say it’s inaccurate and they aren’t half wrong there. But I’m not here to debate on if Alexa is accurate or not I’m here to show you a simple way of getting your website’s Alexa rank with PHP, if you look to the right under ‘Statistics’ you will see this script in action!
So let’s begin shall we?
If you are using a WordPress blog then you will need to place this code in your functions.php file:
function alexaRank($domain){
$remote_url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url='.trim($domain);
$search_for = '<POPULARITY URL';
if ($handle = @fopen($remote_url, "r")) {
while (!feof($handle)) {
$part .= fread($handle, 100);
$pos = strpos($part, $search_for);
if ($pos === false)
continue;
else
break;
}
$part .= fread($handle, 100);
fclose($handle);
}
$str = explode($search_for, $part);
$str = array_shift(explode('"/>', $str[1]));
$str = explode('TEXT="', $str);
return $str[1];
}
function alexaRank($domain){
$remote_url = 'http://data.alexa.com/data?cli=10&dat=snbamz&url='.trim($domain);
$search_for = '<POPULARITY URL';
if ($handle = @fopen($remote_url, "r")) {
while (!feof($handle)) {
$part .= fread($handle, 100);
$pos = strpos($part, $search_for);
if ($pos === false)
continue;
else
break;
}
$part .= fread($handle, 100);
fclose($handle);
}
$str = explode($search_for, $part);
$str = array_shift(explode('"/>', $str[1]));
$str = explode('TEXT="', $str);
return $str[1];
}
If you are using any other blog system then you will need to place it in the appropriate file, read instructions for that system how to add custom PHP scripts for everything else such as your own website with no CMS system involved just place it in a single file and name it ‘alexarank.php’ or whatever you like. Remember if you put this in a separate file be sure to add <?php before any code and ?> at the end of the script (before the closing bracket).
Now to use this function just use this:
<?php echo alexaRank('http://google.com'); ?>
If you would like it to be the same as mine on this site then use the following code:
<?php echo number_format(alexaRank('http://google.com' )); ?>
Those examples above can be used for a WordPress blog, please use the below example for anything else:
<? include_once '../alexarank.php'; echo alexaRank('http://google.com'); ?>
OR
<?php include_once '../alexarank.php'; echo number_format(alexaRank('http://google.com')); ?>
So I hope you find this script helpful or useful, if you do please do share it below and leave a comment if you need help or just to say thanks!





I using a joomla CMS , i do not know place this code in which file??index.php or heder or i do not khow which file :(
I’m not that familiar with Joomla but I believe it’ll be index.php but my best advice would be to read the documentation regarding adding third-party code.