JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.

File I/O

In this blog post I am referring to the File I/O (input/output) functionality for reading and writing to and from text files that is available with server side PHP script.

Because PHP is a Server Side Scripting language, then this PHP function only reads and writes to files that are on the web server, and it is unable to read or write to files that are on the clients computer. To read or write to files on the Clients computer then use a Client Side Scripting language such as Javascript.

There are times when you might need to read and write to text files on the web server, such as when creating trace log files, or for simple page hit counters. PHP offers an easy way for you to accomplish this.


In the following example, I opened up my favorite text editor and then entered the following lines of PHP code into a new text document, which are the PHP functions for reading, writing, and incrementing hit counts to and from a designated text file elsewhere on the web server. Afterwards, I then save the new text document as HitCounter.php instead of as a .txt file.

For this example, first I create a file to hold our PHP functions in, and then I include and call those functions from a PHP web page. I do it this way (instead of writing the functions directly into the PHP web page) so that I can easily re-use these same PHP functions in multiple different PHP web pages without having to re-write or copy/paste the functions into each page.

	// function for reading hit counter value from text file
	function getViews($targetFile)
		// init default return value
		$result = 0;
		// open file for reading
		$myfile = @fopen($targetFile, "r") or die("Unable to open file for reading!");
			// read current value from file
			$result = (int)fread($myfile, filesize($targetFile));
			// close the file
		// return the current value
		return $result;
	// function for writing hit counter value to text file
	function setViews($targetFile, $newValue)
		// open file for writing
		$myfile = @fopen($targetFile, "w") or die("Unable to open file for writing!");
			// write the new value into the file
			fwrite($myfile, $newValue);
			// close the file
	// function for incrementing current value by 1
	function incrementViews($targetFile)
		// read current value and add 1 to it
		$newValue = (int)getViews($targetFile) + 1;
		// set the new value
		setViews($targetFile, $newValue);
		// return the new value
		return $newValue;
	// function for incrementing current value by 1 if page is uniquely visited
	function incrementViewsIfUnique($targetFile, $targetPage)
		// Check session variable to see if user already visited the targetFile's web page during this session.
		// Assuming each web page will have its own targetFile for holding that web pages hit counts in. 
		if(!isset($_SESSION['pageViewed_' . $targetFile]))
			// Set and update session variable to remember that user visited this targetFile page
			$_SESSION['pageViewed_' . $targetPage] = "Visited";
			// increment view and return new value
			return incrementViews($targetFile);

Afterwards, I uploaded this HitCounter.php file to my web server and placed it into a common location where I keep my script files at : (root folder)/PHP_scripts/

Below is the source code for an example PHP web page that includes and uses the above HitCounter.php file and its functions:

<!DOCTYPE html>
	// include the HitCounter.php functions
	include_once($_SERVER['DOCUMENT_ROOT'] . '/PHP_scripts/HitCounter.php');
	// use this for unique page visit counts:
	// $thisPageViewCount = incrementViewsIfUnique("pageViewCount.txt", $_SERVER['SCRIPT_NAME']);
	// OR, use this for repeated page visit counts:
	$thisPageViewCount = incramentViews("pageViewCount.txt");
<html lang="en">
	<head lang="en-US">
		<title>My Web Page</title>
		<h1>Hello World</h1>
		<p>This page was viewed a total of <?php echo($thisPageViewCount); ?> times.</p>

Before you run the above example, make sure that the pageViewCount.txt text file exists at the target location (which is usually in the same folder that the web page is in), and has an initial value of 0 saved in it.

Once you created and saved the above files correctly, then if you navigate to that web page in your web browser and clicked refresh a couple of times, then you should see something similar to the following (if not then check your file paths):


Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.

(0) pollYesResult
(0) pollNoResult

     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved