Page 1 of 1

Apache (possible PHP) Problem

Posted: Fri Apr 07, 2006 11:47 am
by joek1010
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, thanks in advance for any help you can give me with this problem.

I have Apache 1.3.34 running on XP Proffessional with PHP 4.3.11. For about a year now its worked fine. I use it to test php code I write for a remote server. However, for the past week when I try to access a particular php script Apache gives this error message:
[i]
[error] Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting[/i]

and then Apache crashes.


I've done some searching and a lot of people mention that this a sign of a DoS attack. That couldn't be the problem on my server for 2 reasons, first that I'm the only one who accesses it, and 2 I've checked the log files.

Solutions that I've tried:

I upped the ThreadsPerChild to 1000, the server took longer to crash, and used more system resources.

Upgraded Apache: I uninstalled 1.3.33 (which I had previously) and ran the 1.2.34 installer. It used the same conf file. That did not fix the problem.


Here's the php file that crashes the server. It gets a list of files in a directory (text files) and puts them in an array. Then three functions are called by other pages to display these articles. Please excuse my messy code. If its too difficult to follow, I can try to comment it. Thank you again, in advance, for any help you can provide.

Code: Select all

<?PHP
//IMPORTANT! This Variable must be set to the file name of the php script!
$phpfiledir = "NewsInclude.php";

$dir = 'C:/Web/icta/news/';
$counter = "0";


//Reads files from the news directory.

if ($handle = opendir($dir)) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != ".." && $file != $phpfiledir && $file != "viewarticle.php" && $file != "header.php" && $file != "footer.php" && $file != "allarticles.php") {
				//echo "$file<br>";
			if($file != "addarticle") {
				//echo "$file<br>";
				$stories[$counter] = $file;
				//"<br>";
				}
			$counter++;
			}
		}
	closedir($handle);
	//echo "<br>"; - Debugging
	//print_r($stories); //- Debugging
	//echo "<br><br><br><br><br>";
	
	//Reads and Prints txt Stories
	//print_r($stories);
	$x = count($stories);
	$x++;
	$y = 1;
	
	//echo "<br>"; //- Debugging
	//echo "$x"; //- Debugging
	
	while ($y < $x) {
			//print_r($stories[$x]);
			//echo $y;
			//echo $storeis[$y];
			//echo "<br>";
			$news_file = file_get_contents('http://localhost/ICTA/News/' . $stories[$y]);
			$news_file_array = explode("/endtitle/",$news_file);
			$all_news[$y] = $news_file_array;
			//print_r($all_news[$y]);
			//echo "<br><br><br>";
			$y++;
			//echo $x;
		}
		
		//print_r($all_news);
		//echo "<br><br><br>";
		//Function Declaration for Template Pages
		//To display news somewhere, insert the following script (without the slash and star):
		/* 
		
		<?PHP
			include("news\NewsInclude.php"); //This must be the directory and filename of the main script.
			display_articles(1); // The number determines the number of articles displayed.
		?>
			
		*/
		
		$all_news_version2 = $all_news;
		//print_r($all_news_version2);
		
		function display_articles($num_articles,$split_num,$directory) {
			global $all_news_version2;
			$all_news = $all_news_version2;
			//print_r($all_news);
			$numarttot=count($all_news);
			$numarttot++;
			$num = 0;
			while ($num < $num_articles) {
				
				$num_second = 0;
				
				while ($num_second < count($all_news[$num])) {
					
					if(strlen($all_news[$numarttot-$num][$num_second]) > 40) {
						$all_news[$numarttot-$num][$num_second] = split(" ",$all_news[$numarttot-$num][$num_second],($split_num+1));
							$new_count = 0;
							while ($new_count < $split_num) {
								//echo "Output";
								//echo "<br>";
								echo $all_news[$numarttot-$num][$num_second][$new_count];
								echo " ";
								$new_count ++;
								}
							echo " . . . ";
					}
					else {
						echo "<strong>";
						echo '<a href="',$directory,'viewarticle.php?article=', urlencode($all_news[$numarttot-$num][$num_second]), '">';
						echo $all_news[$numarttot-$num][$num_second];
						echo "</a>";
						echo "</strong>";
						}
					echo "<br>";
					$num_second++;
					
					}
					
					//echo "This is the number!: ";
					$num ++;
				}
				//echo $directory;
				//print_r($allnews[1]);
			}
	
		//echo "<br>";
		//display_articles(3,20,'');
		
		function readnews ($article_name) {
		
			global $all_news;
			$counter = 1;
			$count_news = count($all_news);
			$count_news ++;
			
				while($counter < $count_news) {
					if(eregi($article_name,$all_news[$counter][0])) {
						echo $all_news[$counter][1];
						}
					$counter ++;
				}
			
			
			//print_r($all_news);
		}
		
		
		function display_articles_long($num_articles,$split_num,$directory) {
			global $all_news_version2;
			$all_news = $all_news_version2;
			//print_r($all_news);
			$num = 0;
			while ($num < $num_articles) {
				
				$num_second = (0);
				
				while ($num_second < count($all_news[$num])) {
					
					if(strlen($all_news[$num][$num_second]) > 40) {
						$all_news[$num][$num_second] = split(" ",$all_news[$num][$num_second],($split_num+1));
							$new_count = 0;
							while ($new_count < $split_num) {
								//echo "Output";
								//echo "<br>";
								echo $all_news[$num][$num_second][$new_count];
								echo " ";
								$new_count ++;
								}
							echo " . . . ";
					}
					else {
						echo "<strong>";
						echo '<a href="',$directory,'viewarticle.php?article=', urlencode($all_news[$num][$num_second]), '">';
						echo $all_news[$num][$num_second];
						echo "</a>";
						echo "</strong>";
						}
					echo "<br>";
					$num_second++;
					
					}
					
					//echo "This is the number!: ";
					$num ++;
				}
				//echo $directory;
				//print_r($allnews[1]);
			}
		
	
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Apr 07, 2006 11:54 am
by feyd
your file_get_contents() call is essentially making subrequests.. why are you referencing external (to PHP they are) pages when they are local?

Posted: Mon Apr 10, 2006 11:23 am
by joek1010
Sorry for not responding quickly, I was gone for the weekend, and while I did have my computer, I didn't have an internet connection. I also apologive for the post, I'll post the correct PHP tags next time.


As for the file_get_contents, the file basically finds a bunch of files in the News directory (text files) and reads the contents of those files into arrays. The three functions at the end serve to display the content of the arrays (the stories).

Do you think I could have a loop problem with file_get_contents? I've never had a PHP error (including infinite loops) that have cause Apache to crash, which is why I posted.

Thanks for the response!

Posted: Mon Apr 10, 2006 12:15 pm
by feyd
first thing to try is changing the call(s) to file_get_contents() from external requests to file system ones.

Posted: Mon Apr 10, 2006 9:22 pm
by joek1010
Ok, I startup apache today, and all of a sudden the page is working again. This is kind of bizarre; I haven't made any configuration changes (except those already described, which did not work at the time).

I'd still like to follow the advice you have though. It would not be good to have my script randomly crashing Apache. I'm a little confused about changing file_get_contents() though to file system requests. Is this the "resource context" in "string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )"