troubleshooting Question

PHP script overloads server

Avatar of jhmplondon
jhmplondon asked on
PHP
2 Comments1 Solution328 ViewsLast Modified:
Hi guys,

I have customised version of OpenX ad server, I asked someone to add a code that will read content of meta keywords and depending on that will show accurate ads, it works great however after few days my servers needs restarting because the file takes 99% of server power (to be precise it's pretty good dedicated server).

So... below is a part responsible for extracting meta keywords and serving appropriate ads. Do you guys see something in code that could make it overload the server? Just to clarify again, its purpose is to extract only keywords, not the whole page content. I'm not php geek and i don't understand anything in this code.
$url = $GLOBALS['loc'];

	$fp = fopen( $url, 'r' );
	$content = "";

	while( !feof( $fp ) )
	{
		$buffer = trim(fgets($fp, 4096));
		$content .= $buffer;
	}

	$start = '<title>';
	$end = '<\/title>';

	preg_match( "/$start(.*)$end/s", $content, $match );
	$title = $match[ 1 ];
	

	$metatagarray = get_meta_tags( $url );
		
	$keywords = $metatagarray[ "keywords" ];
	
if(!empty($keywords)) {


		$temp = explode(",",$keywords);

	
		for($i=0;$i<count($temp);$i++)
		{

		$add=explode(' ',trim($temp[$i]));

			if(sizeof($add)> 1)
			{

				$temp=array_merge($temp,$add);
	
			}
	
		}
	
		if(sizeof($temp)< 2)
		{
			$temp = explode(" ",$keywords);
		} 


		 $count = count($temp);

} else {

	$count = 0;

}







$con1 = mysql_connect($GLOBALS['_MAX']['CONF']['database']['host'],$GLOBALS['_MAX']['CONF']['database']['username'],$GLOBALS['_MAX']['CONF']['database']['password']);
mysql_select_db($GLOBALS['_MAX']['CONF']['database']['name'], $con1)or die("culnot select:".mysql_error());
$table_prefix = $GLOBALS['_MAX']['CONF']['table']['prefix'];

$keywordAds = array(); $nokeywordAds = array(); $k = 0; $nk = 0; 
$l = 0; $lastToSendAd = array();



	  $query = mysql_query("SELECT * FROM ".$table_prefix."ad_zone_assoc WHERE zone_id =".$_GET['zoneid']." ") or die("Error1".mysql_error());



	 if(mysql_num_rows($query)>0)
		{   
			while ($rowValues = mysql_fetch_assoc($query)) {	

				 $bannerQuery = mysql_query("SELECT * FROM oxm_keyword WHERE banner_id =".$rowValues['ad_id']." ") or die("Error2".mysql_error());

				if(mysql_num_rows($bannerQuery) > 0) {   

					$rowKeywords = mysql_fetch_assoc($bannerQuery);

						if(($count >= 1)) {

					    		for ($j = 0; $j < $count; $j++)
							{
									if ( strcmp (strtolower(trim($rowKeywords['keyword'])),strtolower(trim($temp[$j]))) == 0 )
									{
										 $lastToSendAd[$l] = $rowKeywords['banner_id'];
										 $l++;

									} 
							}

						} 

					
				 } else {

						$nokeywordAds[$nk] = $rowValues['ad_id'];
						$nk++;

				} 


			}

		}

	$keywordAdsCount =  sizeof($lastToSendAd);


	$nokeywordAdsCount =  sizeof($nokeywordAds);


	
	if($count >= 1) {

			if(!empty($lastToSendAd)) {


				if($keywordAdsCount > 1) {

					$banid=$lastToSendAd[rand(0,($keywordAdsCount-1))];

				} else {

					$banid=$lastToSendAd[0];
				}
					
					$ad = MAX_cacheGetAd($banid);

			} else {

				if($nokeywordAdsCount > 1) {

					$banid=$nokeywordAds[rand(0,($nokeywordAdsCount-1))];

				} else {

					$banid=$nokeywordAds[0];
				}
	
				$ad = MAX_cacheGetAd($banid);


			}


	} else {


				if($nokeywordAdsCount > 1) {

					$banid=$nokeywordAds[rand(0,($nokeywordAdsCount-1))];

				} else {

					$banid=$nokeywordAds[0];
				}
	
				$ad = MAX_cacheGetAd($banid);


	}

Open in new window



Thanks for help
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros