Web Development Information
';

echo '

';

echo '

';

echo '

';

echo '

';

echo '

';

echo '

';

}

echo '';

You can also use custom functions to filter out search engines and crawlers. Or create graphs using PHP/SWF Charts (http://www.maani.us/charts/index.php). The possibilities are endless, and you can do all kinds of things!

In Conclusion...

In this article I have shown you have to create a logging module for your own PHP website, using nothing more than PHP and its built-in functions. To view the log file you need to parse it using PHP, and then display it in whatever way you like. It is up to you to create a kick-ass traffic analyzer.

If you still prefer to use a pre-built traffic analyzer, have a look at http://www.hotscripts.com.

About The Author

Dennis Pallett is a young tech writer, with much experience in ASP, PHP and other web technologies. He enjoys writing, and has written several articles and tutorials. To find more of his work, look at his websites at http://www.phpit.net, http://www.aspit.net and http://www.ezfaqs.com.


MORE RESOURCES:
Best web design software of 2024 - TechRadar
Best web design software of 2024  TechRadar

10 Best Website Builders Of 2024 – Forbes Advisor - Forbes
10 Best Website Builders Of 2024 – Forbes Advisor  Forbes

My Sublime Text Setup in 2024 for Web Development - Laravel News
My Sublime Text Setup in 2024 for Web Development  Laravel News

Best 5 Shopify development & web design companies in India - Forbes India
Best 5 Shopify development & web design companies in India  Forbes India

9 Chicago Web Design and Development Agencies To Know - Built In Chicago
9 Chicago Web Design and Development Agencies To Know  Built In Chicago

Design Week magazine acquired and set for relaunch this year - Dezeen
Design Week magazine acquired and set for relaunch this year  Dezeen

Top 20 Best Programming Languages To Learn in 2024 - Simplilearn
Top 20 Best Programming Languages To Learn in 2024  Simplilearn

Web Developer's Guide to the 2024 Esri Developer Summit - Esri
Web Developer's Guide to the 2024 Esri Developer Summit  Esri

AWS Marketplace: Netlify Web Development Cloud Comments - AWS Blog
AWS Marketplace: Netlify Web Development Cloud Comments  AWS Blog

Why (and How) I Learned Web Development as a Data Scientist - Towards Data Science
Why (and How) I Learned Web Development as a Data Scientist  Towards Data Science

Front-end Web Development with .NET for Beginners - Microsoft
Front-end Web Development with .NET for Beginners  Microsoft

Including files results in 403 - General Web Dev - SitePoint
Including files results in 403 - General Web Dev  SitePoint

WP Engine's DE{CODE} 2024: What's Next in Modern Web Development - Business Wire
WP Engine's DE{CODE} 2024: What's Next in Modern Web Development  Business Wire

Wix Launches AI-Powered Image Enhancement and Creation Tools - Martechcube
Wix Launches AI-Powered Image Enhancement and Creation Tools  Martechcube

Website Development: A Beginner's Guide – Forbes Advisor - Forbes
Website Development: A Beginner's Guide – Forbes Advisor  Forbes

Best Web Design Companies Of 2024 – Forbes Advisor - Forbes
Best Web Design Companies Of 2024 – Forbes Advisor  Forbes

The future of web development - AWS Blog
The future of web development  AWS Blog

How To Make A Small Business Website In 2024 - Forbes
How To Make A Small Business Website In 2024  Forbes

How Much Does A Website Cost? (2024 Guide) - Forbes
How Much Does A Website Cost? (2024 Guide)  Forbes

Netlify introduces AI-enabled tool to streamline web development deployments - SiliconANGLE News
Netlify introduces AI-enabled tool to streamline web development deployments  SiliconANGLE News

Best web development tool of 2024 - TechRadar
Best web development tool of 2024  TechRadar

Full-stack web development with HTMX and Bun, Part 2: Pug templating - InfoWorld
Full-stack web development with HTMX and Bun, Part 2: Pug templating  InfoWorld

This Web Development Grad is Developing for Esports - Full Sail News
This Web Development Grad is Developing for Esports  Full Sail News

Best Online Web Development Bootcamps Of 2024 – Forbes Advisor - Forbes
Best Online Web Development Bootcamps Of 2024 – Forbes Advisor  Forbes

Technology Mentor- Web Development - Athena Education - Built In
Technology Mentor- Web Development - Athena Education  Built In

Luis Gonzales Morales | Chief of the Web Development and Data Visualization, UNSD - World Bank
Luis Gonzales Morales | Chief of the Web Development and Data Visualization, UNSD  World Bank

From Data Scientist to AI Developer: Lessons Building an Generative AI Web App in 2023 - Towards Data Science
From Data Scientist to AI Developer: Lessons Building an Generative AI Web App in 2023  Towards Data Science

YouthWorks Works: Program Put Young City Student on Career Path to UMB's Web Development Team - The Elm - The Elm
YouthWorks Works: Program Put Young City Student on Career Path to UMB's Web Development Team - The Elm  The Elm

Why Invest in USD 183 Billion Web Hosting Services Market Reach by 2027, Key Factors behind Market's Exponential ... - openPR
Why Invest in USD 183 Billion Web Hosting Services Market Reach by 2027, Key Factors behind Market's Exponential ...  openPR

5 web development bootcamps teaching the skills to build the next great website - Fortune
5 web development bootcamps teaching the skills to build the next great website  Fortune

Program aims to help Wakulla Correctional Institute inmates learn web development, life skills - WCTV
Program aims to help Wakulla Correctional Institute inmates learn web development, life skills  WCTV

Full-stack web development with HTMX and Bun, Part 1: Elysia and MongoDB - InfoWorld
Full-stack web development with HTMX and Bun, Part 1: Elysia and MongoDB  InfoWorld

How to design this construction? - HTML & CSS - SitePoint
How to design this construction? - HTML & CSS  SitePoint

Web Development in 2023: JavaScript Still Rules, AI Emerges - The New Stack
Web Development in 2023: JavaScript Still Rules, AI Emerges  The New Stack

Is this login process valid? - General Web Dev - SitePoint
Is this login process valid? - General Web Dev  SitePoint

How Much does Website Development Cost in 2024? - Appinventiv
How Much does Website Development Cost in 2024?  Appinventiv

PHP[TEK] 2024: Pioneering the Future of Web Development in Chicago - Security Boulevard
PHP[TEK] 2024: Pioneering the Future of Web Development in Chicago  Security Boulevard

The Role of Web Development in the Growth of Fintech Startups - BOSS Magazine
The Role of Web Development in the Growth of Fintech Startups  BOSS Magazine

Wakulla Correctional Institute inmates learn web development, life skills through coding program - WCTV
Wakulla Correctional Institute inmates learn web development, life skills through coding program  WCTV

Angular 18 will arrive next week - InfoWorld
Angular 18 will arrive next week  InfoWorld

Charlotte ad agency AC&M Group acquires Argentina web development company Axlot - Charlotte Business Journal
Charlotte ad agency AC&M Group acquires Argentina web development company Axlot  Charlotte Business Journal

Back-end Web Development with .NET for Beginners - Microsoft
Back-end Web Development with .NET for Beginners  Microsoft

11 of the Best Web Development Agencies in New York + Tips on How to Choose the Best Agency - Influencer Marketing Hub
11 of the Best Web Development Agencies in New York + Tips on How to Choose the Best Agency  Influencer Marketing Hub

Website Development, a Game-Changer for Business Growth (2024) - Shopify
Website Development, a Game-Changer for Business Growth (2024)  Shopify

Ecommerce Website Cost: Full Guide for 2024 - Shopify
Ecommerce Website Cost: Full Guide for 2024  Shopify

Daily Deal: The Python & Django Web Development Bundle - Techdirt
Daily Deal: The Python & Django Web Development Bundle  Techdirt

Aligning in html - HTML & CSS - SitePoint
Aligning in html - HTML & CSS  SitePoint

Best Coding Bootcamps Online Of 2024 – Forbes Advisor - Forbes
Best Coding Bootcamps Online Of 2024 – Forbes Advisor  Forbes

Trying to locate text - PHP - SitePoint
Trying to locate text - PHP  SitePoint

Launch Code recruiting applicants for its free web development course - KTVI Fox 2 St. Louis
Launch Code recruiting applicants for its free web development course  KTVI Fox 2 St. Louis

Frontend Developer Interview Questions [Ultimate List] - Simplilearn
Frontend Developer Interview Questions [Ultimate List]  Simplilearn

Top Web Frameworks to Learn and Tips to Build a Career in Web Development - Simplilearn
Top Web Frameworks to Learn and Tips to Build a Career in Web Development  Simplilearn

17 Most Popular Python IDEs in 2024: Code Like a Pro - Simplilearn
17 Most Popular Python IDEs in 2024: Code Like a Pro  Simplilearn

Top 32 Web Development Agencies For All Things Technical - Influencer Marketing Hub
Top 32 Web Development Agencies For All Things Technical  Influencer Marketing Hub

Earning A Web Developer Associate Degree – Forbes Advisor - Forbes
Earning A Web Developer Associate Degree – Forbes Advisor  Forbes

85 Must-Know C# Interview Questions and Answers [2024] - Simplilearn
85 Must-Know C# Interview Questions and Answers [2024]  Simplilearn

Top Website Statistics For 2024 – Forbes Advisor - Business - Forbes
Top Website Statistics For 2024 – Forbes Advisor - Business  Forbes

Web Developer: Best Low-Stress, High-Pay Job in America? - Dice Insights
Web Developer: Best Low-Stress, High-Pay Job in America?  Dice Insights

150 Most Popular Python Interview Questions for 2024 - Simplilearn
150 Most Popular Python Interview Questions for 2024  Simplilearn

Inclusivity Unveiled: Decoding Why Accessibility Is Critical in Web Development - ITPro Today
Inclusivity Unveiled: Decoding Why Accessibility Is Critical in Web Development  ITPro Today

15 Highest Paying Countries for Web Developers - Yahoo Finance
15 Highest Paying Countries for Web Developers  Yahoo Finance

Earning An Online Bachelor's Degree In Web Development - Forbes
Earning An Online Bachelor's Degree In Web Development  Forbes

More Ways to Slim Down 'JavaScript Heavy' Web Development - The New Stack
More Ways to Slim Down 'JavaScript Heavy' Web Development  The New Stack

Node.js vs Django: Which is Better for Web Development in 2024? - SitePoint
Node.js vs Django: Which is Better for Web Development in 2024?  SitePoint

Best PHP Frameworks for Web Development in 2024 - Simplilearn
Best PHP Frameworks for Web Development in 2024  Simplilearn

Can AI-Powered Tools End the Web Developer Shortage? - Spiceworks News and Insights
Can AI-Powered Tools End the Web Developer Shortage?  Spiceworks News and Insights

How to Use Node Js for Backend Web Development - Simplilearn
How to Use Node Js for Backend Web Development  Simplilearn

38 Web Development Stats You Should Know in 2024 - Influencer Marketing Hub
38 Web Development Stats You Should Know in 2024  Influencer Marketing Hub

Best Backend Languages For 2024: Everything You Need to Know - Simplilearn
Best Backend Languages For 2024: Everything You Need to Know  Simplilearn

Rendering problem in my table - HTML & CSS - SitePoint
Rendering problem in my table - HTML & CSS  SitePoint

Earning A Master's In Web Development Online - Forbes
Earning A Master's In Web Development Online  Forbes

Leaked: New Web Development Tools from Google - AI Business
Leaked: New Web Development Tools from Google  AI Business

9 Simple Projects to Learn Front-End Web Development - Yonkers Times
9 Simple Projects to Learn Front-End Web Development  Yonkers Times

Top 16 eCommerce Web Development Companies To Grow Your Sales - Influencer Marketing Hub
Top 16 eCommerce Web Development Companies To Grow Your Sales  Influencer Marketing Hub

The 5 Easiest Website Builders for Beginners in 2024 - Tech.co
The 5 Easiest Website Builders for Beginners in 2024  Tech.co

Small Business Website Design Best Practices & Examples - Forbes
Small Business Website Design Best Practices & Examples  Forbes

Watch this web developer dismantle vintage computers - Technical.ly
Watch this web developer dismantle vintage computers  Technical.ly

Are Coding Bootcamps Worth It? Here's What You Should Know - Forbes
Are Coding Bootcamps Worth It? Here's What You Should Know  Forbes

Netlify Announces Software Developer Kit (SDK) to Accelerate Modern Web Development for Enterprises - PR Newswire
Netlify Announces Software Developer Kit (SDK) to Accelerate Modern Web Development for Enterprises  PR Newswire

Best Linux distro for developers of 2024 - TechRadar
Best Linux distro for developers of 2024  TechRadar

Top 8 AI Web Design Software 2024 - Artificial Intelligence - eWeek
Top 8 AI Web Design Software 2024 - Artificial Intelligence  eWeek

Which Programming Language Should I Learn First in 2024? — SitePoint - SitePoint
Which Programming Language Should I Learn First in 2024? — SitePoint  SitePoint

9 Cheapest Website Builders In 2024 – Forbes Advisor - Forbes
9 Cheapest Website Builders In 2024 – Forbes Advisor  Forbes

Web Design Trends to Watch in 2024 — SitePoint - SitePoint
Web Design Trends to Watch in 2024 — SitePoint  SitePoint

The Web Coding Boot Camp - Harper College News
The Web Coding Boot Camp  Harper College News

Java Projects 2024: Elevate Your Development Skills Now! - Simplilearn
Java Projects 2024: Elevate Your Development Skills Now!  Simplilearn

The Life of a Computer Programmer - Southern New Hampshire University
The Life of a Computer Programmer  Southern New Hampshire University

Web Design Companies & Agencies In Austin To Know - Built In Austin
Web Design Companies & Agencies In Austin To Know  Built In Austin

Government Website Design and Content Management - CivicPlus
Government Website Design and Content Management  CivicPlus

freeCodeCamp: Everything You Need To Know Before Enrolling - Forbes
freeCodeCamp: Everything You Need To Know Before Enrolling  Forbes

Why have the discovered URLs disappeared? - Marketing - SitePoint
Why have the discovered URLs disappeared? - Marketing  SitePoint

Top 40+ Web Developer Interview Questions and Answers for 2024 - Simplilearn
Top 40+ Web Developer Interview Questions and Answers for 2024  Simplilearn

Locofy.ai Launches AI-Powered Web Design-to-Code Tool, Locofy Lightning • - Contxto
Locofy.ai Launches AI-Powered Web Design-to-Code Tool, Locofy Lightning •  Contxto

eCommerce Website Design Best Practices & Examples - Forbes
eCommerce Website Design Best Practices & Examples  Forbes

I have a problem with header() - PHP - SitePoint
I have a problem with header() - PHP  SitePoint

DesignRush Unveils the Top Web Development Companies in January 2024 - PR Web
DesignRush Unveils the Top Web Development Companies in January 2024  PR Web

5 Best Online Full-Stack Bootcamps – Forbes Advisor - Forbes
5 Best Online Full-Stack Bootcamps – Forbes Advisor  Forbes

Essential Django Interview Questions Answered [2024] - Simplilearn
Essential Django Interview Questions Answered [2024]  Simplilearn

Web Design and Development Alumni Highlight: Jenna Smith | Digital Media - Utah Valley University
Web Design and Development Alumni Highlight: Jenna Smith | Digital Media  Utah Valley University

Website Development Best Practices & Trends for 2024 - Techopedia
Website Development Best Practices & Trends for 2024  Techopedia


Track Your Visitors, Using PHP


There are many different traffic analysis tools, ranging from simple counters to complete traffic analyzers. Although there are some free ones, most of them come with a price tag. Why not do it yourself? With PHP, you can easily create a log file within minutes. In this article I will show you how!

Getting the information

The most important part is getting the information from your visitor. Thankfully, this is extremely easy to do in PHP (or any other scripting language for that matter). PHP has a special global variable called $_SERVER which contains several environment variables, including information about your visitor. To get all the information you want, simply use the following code:

// Getting the information

$ipaddress = $_SERVER['REMOTE_ADDR'];

$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";

$page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");

$referrer = $_SERVER['HTTP_REFERER'];

$datetime = mktime();

$useragent = $_SERVER['HTTP_USER_AGENT'];

$remotehost = @getHostByAddr($ipaddress);

As you can see the majority of information comes from the $_SERVER variable. The mktime() (http://nl2.php.net/mktime) and getHostByAddr() (http://nl2.php.net/manual/en/function.gethostbyaddr.php) functions are used to get additional information about the visitor.

Note: I used a function in the above example called iif(). You can get this function at http://www.phpit.net/code/iif-function.

Logging the information

Now that you have all the information you need, it must be written to a log file so you can later look at it, and create useful graphs and charts. To do this you need a few simple PHP function, like fopen (http://www.php.net/fopen) and fwrite (http://www.php.net/fwrite).

The below code will first create a complete line out of all the information. Then it will open the log file in "Append" mode, and if it doesn't exist yet, create it.

If no errors have occurred, it will write the new logline to the log file, at the bottom, and finally close the log file again.

// Create log line

$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "";

// Write to log file:

$logfile = '/some/path/to/your/logfile.txt';

// Open the log file in "Append" mode

if (!$handle = fopen($logfile, 'a+')) {

die("Failed to open log file");

}

// Write $logline to our logfile.

if (fwrite($handle, $logline) === FALSE) {

die("Failed to write to log file");

}

fclose($handle);

Now you've got a fully function logging module. To start tracking visitors on your website simply include the logging module into your pages with the include() function (http://www.php.net/include):

include ('log.php');

Okay, now I want to view my log file

After a while you'll probably want to view your log file. You can easily do so by simply using a standard text editor (like Notepad on Windows) to open the log file, but this is far from desired, because it's in a hard-to-read format.

Let's use PHP to generate useful overviews for is. The first thing that needs to be done is get the contents from the log file in a variable, like so:

// Open log file

$logfile = "/some/path/to/your/logfile.txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r");

$log = fread($handle, filesize($logfile));

fclose($handle);

} else {

die ("The log file doesn't exist!");

}

Now that the log file is in a variable, it's best if each logline is in a separate variable. We can do this using the explode() function (http://www.php.net/explode), like so:

// Seperate each logline

$log = explode("", trim($log));

After that it may be useful to get each part of each logline in a separate variable. This can be done by looping through each logline, and using explode again:

// Seperate each part in each logline

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

$log[$i] = trim($log[$i]);

$log[$i] = explode('|', $log[$i]);

}

Now the complete log file has been parsed, and we're ready to start generating some interesting stuff.

The first thing that is very easy to do is getting the number of pageviews. Simply use count() (http://www.phpit.net/count) on the $log array, and there you have it;

echo count($log) . " people have visited this website.";

You can also generate a complete overview of your log file, using a simple foreach loop and tables. For example:

// Show a table of the logfile

echo '';

echo 'IP Address';

echo 'Referrer';

echo 'Date';

echo 'Useragent';

echo 'Remote Host';

foreach ($log as $logline) {

echo '

' . $logline['0'] . '' . urldecode($logline['1']) . '' . date('d/m/Y', $logline['2']) . '' . $logline['3'] . '' . $logline['4'] . '
home | site map
© 2006