I run this code in the background using require 'adlinkcheck.php'; to load this script when someone is viewing an ad on the site.
Here is the issue:
This script will use 100% of the cpu, even at slow hours of the night/day.
When I comment this line out, my cpu load (on average) is 10%.
Code: Select all
<?
//filename: adlinkcheck.php
$check_link=false;
if(!($ad['website']=="" || $ad['link_try']>=3)) {
$days=Ad::GetDaysLastCheckWebsite2($ad['link_checked_on']);
if($ad['link_checked_on']=="0000-00-00 00:00:00" || $days>=5){
$website=str_replace("http://", "", str_replace("www.", "", $ad['website']));
$web_file=$website;
if(strpos($website, "/")) $website=substr($website, 0, strpos($website, "/"));
$host=gethostbyname($website);
if($host!=$website){
$html='';
$f=@fopen("http://".(strpos($ad['website'], "www")!==false ? "www." : "").$web_file, "r");
while(!@feof($f)){
$html.=@fgets($f);
}
@fclose($f);
$html=strtolower($html);
$html_arr=explode("<a ", $html);
for($i=0; $i<count($html_arr); $i++){
$v=$html_arr[$i];
$v=substr($v, 0, strpos($v, ">"));
$arr_v=explode(" ", $v);
foreach ($arr_v as $vv){
$vv=str_replace("\"", "", str_replace("'", "", $vv));
if(strpos($vv, "href=")===false) continue;
foreach ($config['self_url'] as $url){
if(strpos($vv, $url)!==false) $check_link=true;
}
}
}
$ad['link_checked_on']=date('Y-m-d H:i:s');
if ($check_link){
$ad['link_try']=0;
$ad['link_active']=1;
} else {
$ad['link_try'] = (int)$ad['link_try']+1;
$ad['link_active']=0;
}
Ad::SaveLinkData($ad['link_checked_on'], $ad['link_try'], $ad['link_active'], $ad['id']);
}
}
}
?>Linux, Dual xeon 2.8, 3gb ram
PHP 4.4.4, MySQL 4.1.22
I ran out of ideas, anyone wanna help out?