PHP display pageload time
Its very useful to know how long your pages took to process. This not only gives you some idea of the efficiency of your websites and of the server running time. This code can help you diagnose problems, benchmark code corrections/additions, etc. The best way to do this is program PHP to read the system time at the beginning of the page, scan it again near the end, and then work out the difference between the values. To get us started, just add the following basic code near the top of your page, before all the main content:
<?php $endtime = microtime(); $endarray = explode(" ", $endtime); $endtime = $endarray[1] + $endarray[0]; $totaltime = $endtime - $starttime; $totaltime = round($totaltime,5); echo "This page loaded in $totaltime seconds."; ?>Read More
PHP visitor counter
An essential way to see how popular is your website, is an php counter. This is an small code, php counter that can give you precise information about how many times a page was visited. The concept is very simple, the data is stored in a .dat file, so there is no need for a mySQL database. First:
1. create a directory in your computer called “counter”;
2.chmod 777 directory called “counter”;
3.create counter.dat with any text reader ( wordpad, notepad ), this will be the “database” were the information will be stored.
Now, the explication for the code:
- Use the file_exists() function in PHP and see if the counter.dat file exists. If it does, then open the file using the PHP fopen() function.
- Use the PHP fgets() function to find the number of hits there and save it in a variable.
- Add one to the variable.
- Close the file.
- Display the number of hits.
- Open the counter.dat file using the fopen() function and save it as a variable.
- Use the PHP fputs() function to insert the new number of hits.
- Close the file.
- If the counter.dat file doesn’t exist, then use the fopen() function and declare it as a variable.
- Use the Fputs() function and put a “1″ in there.
- Print out that 1 person has come to the page and then close the file.
<? if(file_exists("counter.dat")) { $exist_file = fopen("counter.dat", "r"); $new_count = fgets($exist_file, 255); $new_count++; fclose($exist_file); print("$new_count people have visited this page"); $exist_count = fopen("counter.dat", "w"); fputs($exist_count, $new_count); fclose($exist_count); } else { $new_file = fopen("counter.dat", "w"); fputs($new_file, "1"); print("1 person have visited this page"); fclose($new_file); } ?>Read More
PHP Browser Redirect
It is an simple example which will take the visitors to one page if they are using Internet Explorer, and to another page if the visitor is using another type of browser.
Now lets decide what is going to happen if:
- If the browser is Microsoft Internet Explorer (MSIE), it will automatically redirect to: www.tutorialize.org/redirect1 ( example )
- If the browser is not Microsoft Internet Explorer (MSIE), it will automatically redirect to: www.tutorialize.org/redirect2( example )
The most interesting part of this is that this code is, that it has to be sent out before any output to the HTML page. You will have to make sure that the code is filled under the first line of code on your PHP page.
This is the example php code, it can be modified in many ways to fill your requests.
//if its MSIE then if ($name = strstr ($HTTP_USER_AGENT, "MSIE")) { //it will send to www.tutorialize.org/redirect1 Header ("Location: http://www.tutorialize.org/redirect1"); } else { //else will send to www.tutorialize.org/redirect2 Header ("Location: http://www.yahoo.com/"); }