Jump to content

A quick php script that checks list, and direct visitors to the fastest site?


JohnnyDoomo

Recommended Posts

Nothing fancy, I am just looking for a simple php script that would check a text file on my server that has a bunch of domains in it. I want the script to ping a random 5 or 10 sites in the list, and then redirects the visitor to the fastest one, or at least one that responds, and is not down. If all that are checked are down, then it would need to recheck the list.

 

I want it to pick random domains out of the list, as I don't want users directed to the same site(s) all the time, just because it pings the best.

 

When I say ping, I am talking about making sure the user isn't directed to a website that is down, or a slow loading website. I am not sure if "ping" is the correct word for that tho?

 

Can anybody offer me code like this for free, or is this type of request getting into something that would need to be paid code? (and if so, any idea how much? I didn't post this in the freelancing forum, because it sounds like it might be something simple to code.

 

I am not a coder, so I am looking for something complete that I can add to a php file and it works.

 

Thanks for any code, pointers/problems with this idea, or price quotes (if necessary) on this little script I am looking for.

Link to comment
Share on other sites

Sounds like a fun script, and got me interested enough to want to write it. You may want to use a database instead of a file, or does it have to be a file? One problem you have not been thinking about is if a lot of domains are down, and your script keeps picking out the ones that are down. It will take forever to load. Maybe it needs to pick out some new domains at random that haven't been checked?

Link to comment
Share on other sites

I would prefer it to just be a text file. I am not a programmer, though I do know how to create a mysql database with a user that has access to it, and I can insert a chunk of code for a query to create the needed tables.

 

I would say if I am paying for something I would want it in a text file, but if you would be willing to create this for free, I would be more than happy to accept whichever backend you decide, but I would need instructions on what to run on the empty database to create the required tables.

 

You're right on the script could constantly pick slow or dead domains.

 

If you want to get fancy, it could always have groups of known slow domains, and once they are pinged a certain amount of times and are dead or over a certain ping response, it removes them from the main list of domains to check.

 

I was trying to keep it simple, in the hopes that I could get it free, or at a cheap cost, but even at low price with some of these features I would be willing to pay, though if I were to pay, I may have a request or two to make, or at least questions on how much it would increase the cost.

 

If the script can modify the text file and move the domains from it to another text file if they've been removed, that would be cool. So the text files functioned like a database, but without all the trouble (speed?) of a mysql database.

 

Again, I'm not a programmer, so I don't really know if asking for this to function out of text files would have other side effects to the script, like slowness, CPU load, etc. I am interested in the script though, either way.

 

Thoughts?

Link to comment
Share on other sites

I am starting to think the way I wanted to solve the actual ping is unsafe if there is something in the file that shouldn't be there... how does it get there btw?

 

I could solve the problem in a more safe way, but that's not always going to give an accurate reading when it comes to the ping data. Still it's safer I think.

Link to comment
Share on other sites

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$domains = explode($newline, file_get_contents($file_name));
// print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0) || (microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000))){
			if(isset($fastest_time) && $fastest_time!=999){
				return $domains[$fastest];
			}else{
				return false;
			}
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if(!isset($fastest)){
			$fastest = $i;
			$fastest_time = $current_time;
		}else if($current_time<$fastest_time){
			$fastest = $i;
			$fastest_time = $current_time;
		}
	}
	if(isset($fastest_time) && $fastest_time!=999){
		return $domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
} 
return $result_time; 
}

$fastest_ping = fastest_ping();
if($fastest_ping){
echo $fastest_ping; // here is where you can start to use the fastest domain
}

?>

 

I hope this is what you want?

 

For all you OOP lovers, yes, I should probably have created a class... but these functions work as they should, at least I think and hope so.

Link to comment
Share on other sites

Is there more I need to add to this to get it to do the actual redirecting?

 

I try it, and all I get is a white page.

 

My domains.txt file is structured like this:

 

http://google.com

http://yahoo.com

http://apple.com

 

When I uncomment the $domains line, I get a read out of the text file that looks like this:

 

Array ( [0] => http://google.com [1] => http://yahoo.com [2] => http://apple.com )

 

Did I do something wrong, or do I need to do more to get the actual redirection to work?

 

Thanks again for your help!

Link to comment
Share on other sites

Is there more I need to add to this to get it to do the actual redirecting?

 

I try it, and all I get is a white page.

 

My domains.txt file is structured like this:

 

http://google.com

http://yahoo.com

http://apple.com

 

When I uncomment the $domains line, I get a read out of the text file that looks like this:

 

Array ( [0] => http://google.com [1] => http://yahoo.com [2] => http://apple.com )

 

Did I do something wrong, or do I need to do more to get the actual redirection to work?

 

Thanks again for your help!

 

Try not to put http at the start, just the domain, for example google.com.

If you give me a day, I will make it so you don't even need to worry about http or anything else, but can't do it now...

 

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$domains = explode($newline, file_get_contents($file_name));
// print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0) || (microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000))){
			if(isset($fastest_time) && $fastest_time!=999){
				return $domains[$fastest];
			}else{
				return false;
			}
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if(!isset($fastest)){
			$fastest = $i;
			$fastest_time = $current_time;
		}else if($current_time<$fastest_time){
			$fastest = $i;
			$fastest_time = $current_time;
		}
	}
	if(isset($fastest_time) && $fastest_time!=999){
		return $domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
} 
return $result_time; 
}

$fastest_domain = fastest_ping();
if($fastest_domain){
header('Location: http://'.$fastest_domain.'.com'); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \'http://'.$fastest_domain.'.com/\'
//-->
</script>';*/
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
}else{
echo 'ERROR: Couldn't find a fast domain!<br /><a href="">try again</a>?';
}

?>

 

See if this helps you any more...

Link to comment
Share on other sites

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$original_domains = explode($newline, file_get_contents($file_name));
$domains = preg_replace('/^.*www.|^.*\/\/|\/.*|\?.*/', '', $original_domains);
print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0) || (microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000))){
			if(isset($fastest_time) && $fastest_time!=999){
				return $original_domains[$fastest];
			}else{
				return false;
			}
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if(!isset($fastest)){
			$fastest = $i;
			$fastest_time = $current_time;
		}else if($current_time<$fastest_time){
			$fastest = $i;
			$fastest_time = $current_time;
		}
	}
	if(isset($fastest_time) && $fastest_time!=999){
		return $original_domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
} 
return $result_time; 
}

$fastest_domain = fastest_ping();
if($fastest_domain){
header('Location: '.$fastest_domain); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \'.$fastest_domain.'/\'
//-->
</script>';*/
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
}else{
echo 'ERROR: Couldn\'t find a fast domain!<br />
<a href="">try again</a>?';
}

?>

This should handle that the domains are more like links:

http://www.phpfreaks.com/forums/index.php
www.fakelink.com?search=test

and actually redirect you to them, rather than phpfreaks.com

If there is any types of link it doesn't like, just tell me and I will re-write the regex to match this.

Link to comment
Share on other sites

It looks like it's working now, but I did notice that it doesn't like domains in the format of google.com or www.google.com. Only http://google.com and http://www.google.com format seem to work.

 

If I wanted it to sit on the page before redirecting the user for 2 seconds with a message that simply says "Redirecting...", could you include that? I would normally have an idea about how to do this, but with all the other stuff going on and in php I have no idea where to even start.

 

Thanks for creating this little script, it will really help me out quite a bit. I don't know coding, but I am still amazed that you wrote it so quickly. I do still need to test it with some known bum domains to test it, but maybe you already did that?

 

Thanks again!

Link to comment
Share on other sites

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$original_domains = explode($newline, file_get_contents($file_name));
$original_domains = preg_replace('/^.*www\.|^.*\/\//', '', $original_domains);
$domains = preg_replace('/\/.*|\?.*/', '', $original_domains);
//print_r($original_domains);
//print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0) || (microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000))){
			if(isset($fastest_time) && $fastest_time!=999){
				return $original_domains[$fastest];
			}else{
				return false;
			}
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if(!isset($fastest)){
			$fastest = $i;
			$fastest_time = $current_time;
		}else if($current_time<$fastest_time){
			$fastest = $i;
			$fastest_time = $current_time;
		}
	}
	if(isset($fastest_time) && $fastest_time!=999){
		return $original_domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
} 
return $result_time; 
}

$fastest_domain = fastest_ping();
if($fastest_domain){
//header('Location: http://'.$fastest_domain); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \'http://'.$fastest_domain.'\';
//-->
</script>';*/
//echo '<meta http-equiv="refresh" content="5;url=http://'.$fastest_domain.'">';
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
}else{
echo 'ERROR: Couldn\'t find a fast domain!<br />
<a href="">try again</a>?';
}

?>

I've fixed the problems now I think.

 

If you want something to appear on the screen before it redirects, then you can either use the meta tag I put in the script and use it in the header, or you can google and make your own delayed JavaScript redirect. :)

 

All you really need to understand is the last few lines, from here and out to be exact:

$fastest_domain = fastest_ping();

 

Oh and if it still doesn't read the file correctly, tell me again. I've not tested how the script handles sub domains for example and if it stops the script from working correctly.

Link to comment
Share on other sites

hmm, when I test with a complete list of fake domains, I get sent to one of the fake domains that's a dead page.

 

Also, after the line that reads

 

header('Location: http://'.$fastest_domain); // can't be anything sent to the web browser before this

 

I add echo 'Redirecting...'; just like how you have the line echo if a domain isn't found, and I don't see anything saying "Redirecting...".

 

Did I do something wrong?

 

 

Link to comment
Share on other sites

hmm, when I test with a complete list of fake domains, I get sent to one of the fake domains that's a dead page.

 

Also, after the line that reads

 

header('Location: http://'.$fastest_domain); // can't be anything sent to the web browser before this

 

I add echo 'Redirecting...'; just like how you have the line echo if a domain isn't found, and I don't see anything saying "Redirecting...".

 

Did I do something wrong?

A header should be sent to the web bro wser before any html. That header tells the web browser the new location of the site, so the web browser will stop loading the page and go there. You should use JavaScript or html for delayed redirect. I think I added both of those options. The timed JavaScript needs to be timed, and for this you should use google, just google delayed redirect and read the guides. The html should be part of the html code, more specifically the head tag, and it's currently set to 5 seconds (you see the number).

 

Yes, the script doesn't do that a great job at checking the webpage. If you want to do that, it's a rather complicated and nasty way. Could run a check with curl to see if the site is at all up though when it sets a new fastest ping.

Link to comment
Share on other sites

I need something that won't be directing visitors to dead pages though. I just assumed that the pinging of websites through php and the ping I do at a command prompt would be similar, as my ping at a command prompt knows when I am trying to ping a dead domain.

 

It will probably need something that runs a whois on the picked domains, or something to ensure that they are still active websites, and have not been abandoned.

 

I'm assuming anything at this depth is farther than you are willing to code for free?

 

In my mind, I need it so that I'm not redirecting users to dead pages. If slow pages can't get passed the random redirection, I don't want dead pages to make it through.

 

Do you think this would only require a little bit more coding, or would what I'm looking for be a lot more coding to check if a website actually is serving a web page at its domain? (If you have a better idea of how to check and if you'd like to price quote it, give me a figure.)

 

Thanks again for your help. I'm sure I can use what you've written, and I may have to have it tweaked by somebody more knowledgeable than me, but it's an excellent start for what I have planned.

Link to comment
Share on other sites

I need something that won't be directing visitors to dead pages though. I just assumed that the pinging of websites through php and the ping I do at a command prompt would be similar, as my ping at a command prompt knows when I am trying to ping a dead domain.

 

It will probably need something that runs a whois on the picked domains, or something to ensure that they are still active websites, and have not been abandoned.

 

I'm assuming anything at this depth is farther than you are willing to code for free?

 

In my mind, I need it so that I'm not redirecting users to dead pages. If slow pages can't get passed the random redirection, I don't want dead pages to make it through.

 

Do you think this would only require a little bit more coding, or would what I'm looking for be a lot more coding to check if a website actually is serving a web page at its domain? (If you have a better idea of how to check and if you'd like to price quote it, give me a figure.)

 

Thanks again for your help. I'm sure I can use what you've written, and I may have to have it tweaked by somebody more knowledgeable than me, but it's an excellent start for what I have planned.

You could rewrite the ping function... Using exec, and then you can fetch the result of that and edit it to give you the actual ping.

As I said, you can also use curl to try visit the page, and see if you can load it, before you set it to be the fastest domain in the code.

 

So which do you want to do?

 

Oh, and I'm curious, how will these be registered in the file? Because the exec can be very dangerous if someone edits the file with some malicious code.

Link to comment
Share on other sites

I am self editing the text file, but at some point I may allow users to submit domains, but it would of course go threw a queue that I would moderate.

 

A lot of what you talk about is still over my head, but as long as the end result is that it doesn't direct users to a dead page, I would be willing to go whichever way would put less strain and ran the code the fastest.

Link to comment
Share on other sites

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$original_domains = explode($newline, file_get_contents($file_name));
$original_domains = preg_replace('/^.*www\.|^.*\/\//', 'http://', $original_domains);
$domains = preg_replace('/^http:\/\/|\/.*|\?.*/', '', $original_domains);
//print_r($original_domains);
//print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0) || (microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000))){
			if(isset($fastest_time)){
				return $original_domains[$fastest];
			}
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if($current_time!=999){
			if(!isset($fastest)){
				if(possible_to_load($original_domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}else if($current_time<$fastest_time){
				if(possible_to_load($original_domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}
		}
	}
	if(isset($fastest_time)){
		return $original_domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
}
return $result_time; 
}

function possible_to_load($url, $timeout){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code>=200 && $http_code<300){
	// might want to change 300 to 400 if you allow redirects
	return true;
}
return false;
}


$fastest_domain = fastest_ping();
if($fastest_domain){
//header('Location: '.$fastest_domain); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \''.$fastest_domain.'\';
//-->
</script>';*/
//echo '<meta http-equiv="refresh" content="5;url='.$fastest_domain.'">';
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
//echo $fastest_domain;
}else{
echo 'ERROR: Couldn\'t find a fast domain!<br />
<a href="">try again</a>?';
}

?>

Probably not the fastest, but at least you are more sure! :)

And I changed quite a lot in the above code.

Link to comment
Share on other sites

It is back to not working with urls that don't have http:// in them.

 

I took the domains to check down to 5 and it takes 16 seconds for it to return. It isn't suppose to take this long is it? Maybe it's taking so long because of the above mention of the http:// not working?

Link to comment
Share on other sites

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$original_domains = explode($newline, file_get_contents($file_name));
$domains = preg_replace('/^.*www\.|^http:\/\/|\/.*|\?.*/', '', $original_domains);
//print_r($original_domains);
//print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0)){
			if(isset($fastest_time)){
				return $original_domains[$fastest];
			}
		}
		if(microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000)){
			if(isset($fastest_time)){
				return $original_domains[$fastest];
			}
			return false;
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if($current_time!=999){
			if(!isset($fastest)){
				if(possible_to_load($domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}else if($current_time<$fastest_time){
				if(possible_to_load($domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}
		}
	}
	if(isset($fastest_time)){
		return $original_domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
}
return $result_time; 
}

function possible_to_load($domain, $timeout){
$url = 'http://'.$domain;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, ceil($timeout/1000));
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code>=200 && $http_code<400){
	// 400 = allow redirects
	return true;
}
return false;
}


$fastest_domain = fastest_ping();
if($fastest_domain){
//header('Location: '.$fastest_domain); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \''.$fastest_domain.'\';
//-->
</script>';*/
//echo '<meta http-equiv="refresh" content="5;url='.$fastest_domain.'">';
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
//echo $fastest_domain;
}else{
echo 'ERROR: Couldn\'t find a fast domain!<br />
<a href="">try again</a>?';
}

?>

Link to comment
Share on other sites

Yet another fix...

 

If this doesn't work, let's take it in PM, because I don't want to spam the forum.

<?php

function fastest_ping(){
// setting some variables you can change
$file_name = 'domains.txt'; // the text file with the domains
$picks = 10; // number of domains picked from file and tested per interval
$time_limit = 60; // how long the script maximum will run for in seconds
$time_limit_slack = 5; // how much time the rest of the script can use in seconds
$pings = 1; // number of times you want to send a ping request
$timeout = 1000; // how many ms is considered a timeout
$newline = PHP_EOL; // just setting what the newline in the text file is
// if it doesn't read the file correctly
// you may want to replace PHP_EOL with either \r\n or \n

set_time_limit($time_limit);
$original_domains = explode($newline, file_get_contents($file_name));
$domains = preg_replace('/^.*www\.|^http:\/\/|\/.*|\?.*/', '', $original_domains);
//print_r($original_domains);
//print_r($domains);
// uncomment the above line to see if the script reads the file correctly

if($domains>0){
	shuffle($domains);
	$domains_count = count($domains);
	$timing = microtime(true);
	for($i=0; $i<$domains_count; $i++){
		if(($i%$picks==0 && $i>0)){
			if(isset($fastest_time)){
				return $original_domains[$fastest];
			}
		}
		if(microtime(true)+($timeout*$pings)-$timing>(($time_limit-$time_limit_slack)*1000)){
			if(isset($fastest_time)){
				return $original_domains[$fastest];
			}
			return false;
		}
		$current_time = ping($domains[$i], $timeout);
		for($ping=1; $ping<$pings; $ping++){
			$extra_time = ping($domains[$i], $timeout);
			if($extra_time<$current_time){
				$current_time = $extra_time;
			}
		}
		if($current_time!=999){
			if(!isset($fastest)){
				if(possible_to_load($original_domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}else if($current_time<$fastest_time){
				if(possible_to_load($original_domains[$i], $timeout)){
					$fastest = $i;
					$fastest_time = $current_time;
				}
			}
		}
	}
	if(isset($fastest_time)){
		return $original_domains[$fastest];
	}
}
return false;
}

function ping($domain, $timeout){
$start_time = microtime(true);
$fp = @fsockopen($domain, 80, $errno, $errstr, $timeout);
$result_time = microtime(true)-$start_time;
if(!$fp){
	return 999;
}
return $result_time; 
}

function possible_to_load($url, $timeout){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, ceil($timeout/1000));
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($http_code>=200 && $http_code<400){
	// 400 = allow redirects | 300 = not allow redirects
	return true;
}
return false;
}


$fastest_domain = fastest_ping();
if($fastest_domain){
//header('Location: '.$fastest_domain); // can't be anything sent to the web browser before this
/*echo '<script type="text/javascript">
<!--
window.location = \''.$fastest_domain.'\';
//-->
</script>';*/
//echo '<meta http-equiv="refresh" content="5;url='.$fastest_domain.'">';
// uncomment away the above to use javascript to redirection instead, this is if there is anything sent before the first redirect
echo $fastest_domain;
}else{
echo 'ERROR: Couldn\'t find a fast domain!<br />
<a href="">try again</a>?';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.