Easy "PHP Proxy Checker" Writing Tutorial

October 31, 2007

I started to write this on Eli’s forum but decided to add it here so everyone could reference it.

If you have a list of proxies that you’ve got from somewhere and want to verify if they’re good or not this is simple script that will read a text file and check them.

First we’ll make a text file with our proxies on each line of the file in the format IP:PORT with one per line.

127.127.127.127:2487
123.123.123.123:3248

then save that text file as “proxylist.txt” in the same directory as the php file you’re going to make. Then we’ll start our php file with our proxy checking in it. Here’s the complete script that just echo’s the status of each proxy out to the screen. We’ll make a few changes and have it write a new file with only the good proxies next.

// This is the page that that we're going to request going through the proxy
$testpage = "http://www.google.com";
// This loads all the proxies from the file into an array
$proxies = file("proxylist.txt");
// Here we loop through each cell of the array with the proxies in them testing each one until we get to the end of the array
foreach($proxies as $proxy)
{
// This script utilizes cURL which is library you can read more about
//using curl in my intro tutorials
// starting curl and setting the page to get
$ch = curl_init($testpage);
// sets the proxy to go through
curl_setopt($ch,CURLOPT_PROXY,$proxy);
// sets to use a tunnel proxy which most http proxies are
curl_setopt($ch,CURLOPT_HTTPTUNNELPROXY,$proxy);
// makes the curl call do it's work based on what we've set previously and
//returns that fetched page to $page
$page = curl_exec($ch);
// cleans up the curl set
curl_close($ch);
// this will check that there was some html returned, now some sites might block some
//proxies so you'd want to set for that specific site in the $testpage var and then
//find something on that page to look for with the below function.
$check = stripos($page,'</html>');
// if there was a match in the stripos (string postion) function echo that the
//proxy got the data and works
if($check > 0)
{
echo $proxy." Works!
";
// or else echo it doesn't work
}else{
echo $proxy." Is Dead!
";
}
}
?>

There’s your code. The gist of this is that we load each line from a file into an array. Then we loop through each array element checking if we can get the google page going through the proxy. Then if it pulls data it prints on the screen that the proxy works or if it doesn’t get something returned it doesn’t.

Now we might not want to print things to the screen and would rather make a new file with the the good proxies. To do that we’d swap this code:

if($check > 0)

{

echo $proxy." Works!";

// or else echo it doesn't work

}else{

echo $proxy." Is Dead!";

}

with:

if($check > 0){file_put_content("newproxylist.txt",$proxy."n",FILE_APPEND);}

rename('proxylist.txt','proxylistold.txt)';

rename('newproxylist.txt','proxylist.txt');

}

And that will write a new file and add each of the good proxies to it. Then rename the the original file to proxylistold.txt in case there was an error it’s good to have a backup. Then we rename the newpoxylist.txt to the standard proxylist.txt and it’s all fresh and ready to go. There you have it a nice simple proxy checker.

P.S. make sure you make the dir that they’re in writable as there’ll be issues with creating a new file in an unwritable dir.

P.P.S Trying this new code plug-in and well let me tell yah. IT’S A HUGE PAIN IN THE ASS!

newsletter

Want More? The more people listening the more I’ll write.
Subscribe to get business insights in your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.