fopen - many times per page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

fopen - many times per page

Post by IGGt »

I've put together a debug function, that is set to run at various point in my site. But as I add more instances of this function I am starting to get more and more errors in the form of:

[text]Warning: fopen(.\DebugData\PageListing_20120801_17.txt): failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Test\debug.function.php on line 96 Can't Open File[/text]

In some cases it is accessed up to 25 times per page, to record every page that is loaded, every Database that is accessed, and every query that is run etc.

Does it matter that I open and close the debug file each time I write to it, or is there a better option?

My function is:

Code: Select all

function debugf($P,$S,$D,$Ds)
{
	$debug = "On";
	
	if ($debug == "On") {
	
	$timezone = "Europe/London";
	date_default_timezone_set ($timezone);
	
	$DebugDate = date('Ymd_H');
	$ResetTime = date('Y-m-d H:i:s');			
	$tab = (chr(9));		

	$page = $P;
	$Section = explode(",",$S);
	$DB = $D;
	$DBPoint = $Ds;
	
	//print_r($Section);
	
	//$a=0;
	foreach($Section AS $Sc){
		switch($Sc){
			case 1:
				//echo "COUNTER";
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 1;};
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, "--PAGE COUNT: ".$Counter."--\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				print $Counter;
				$Counter++;
				$_SESSION['debugCounter'] = $Counter;
				break;
			case 2:
				//echo "DBCONNECTIONS";
				switch($DBPoint){
					case 1:
						$DBp = "Start";
						break;
					case 2:
						$DBp = "Connected";
						break;
					case 3:
						$DBp = "End";
						break;
					default:
						$DBp = $Ds;
						break;
					};
				$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, $page.$tab.date('H:i:s').$tab.$DB." ".$DBp."\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				break;
			case 3:
				//echo "EMAIL ON RESET";
				if(isset($_SESSION['debugCounter'])){$Counter = $_SESSION['debugCounter'];}else{$Counter = 2;};
				if($Counter == 2) {
					$email = "email@company.com";
					$subj = "Test Page Counter Has Been Reset";
					$mes = "Test Page Counter Has Been Reset\n 
					Time: $ResetTime";
					mail($email, $subj, $mes);
				};
				unset($Counter);
				break;
			case 4:
				//echo "HEADER";
				//DBConnections
				$myFile = ".\\DebugData\\DBConnections_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, "## START ##".$tab."Time".$tab."Database \r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				//PageListing//
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				fwrite($fh, "## ".$page." ##".$tab."Time".$tab."Current".$tab."Peak \r\n");
				fclose($fh);
				break;
			case 5:
				//echo "PAGE LISTING";
				$mem = memory_get_usage(true); //in bytes
				$memPeak = memory_get_peak_usage (true); //in bytes
				$myFile = ".\\DebugData\\PageListing_".$DebugDate.".txt";
				$fh = fopen($myFile, 'a') or die("Can't Open File");
				flock($fh, LOCK_EX);
				fwrite($fh, $page.$tab.date('H:i:s').$tab.$mem.$tab.$memPeak."\r\n");
				flock($fh, LOCK_UN);
				fclose($fh);
				break;
			};
		//$a++;	
	};
	};
};
and is called as:

Code: Select all

//DEBUG (Page Listing)
debugf("pageName.php(START)","5",0,0);	
//--

//DEBUG (DB Connection)
debugf("pagename.php","2",$Cserver,3);	
//--
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fopen - many times per page

Post by requinix »

A database is a better option. With files you have to deal with concurrency and that is not fun.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: fopen - many times per page

Post by IGGt »

My only concern with that is that if I am opening and closing a DB connection 25+ times per page (apx 400 times for a complete load, including all includes etc.), isn't that going to add a lot of overhead, and time?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fopen - many times per page

Post by requinix »

Yes. Which is why you don't keep opening and closing it. Open once when you need it the first time, close it if you want to at the end (or let PHP do it for you).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: fopen - many times per page

Post by pickle »

You should translate this to a class. In that class, you can open the file once (in the __construct() function) then write to it multiple times. The file will be closed automatically by PHP when your script completes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply