Archive for the ‘PHP’ Category

Display page load time

Monday, August 4th, 2008

You may want to display the time it took to load a page.
Following is a set of simple PHP code which would help you achieve this goal.

Place the following snippet at the very top of your page.
(Ultimately, it’s advisable to have a header file and a footer file that are included on every page of your website).

<?php
$timer_start = str_replace(" ","",microtime());
?>

Then place the following code at the very end of your footer.

<?php
$timer_end = str_replace(" ","",microtime());
$timer_diff = number_format($timer_end-$timer_start,6,'.','');
if($timer_diff<0) $timer_diff = "0.00";
echo "<div id='load-time' style='font-family: verdana; font-size: 8pt; color: #444444; text-align: center'>Page load time: <b>$timer_diff</b> second</div>";
?>

Obviously, we’re assuming here that this code should be within .php files, with the current server configuration allowing for parsing of such files.

Add authentication to any php page the easy way

Tuesday, April 3rd, 2007

This is useful for anyone who’d like to add a login form to any php page.

Simply, save the following code as access.php, and insert the following snippet on the first line of any php file you want protected.

< ?php include “access.php”; ?> (remove the space before the question mark)

Voila! as easy as 1,2,3. The crentials can be edited in the access.php file, by changing the values of $ADMIN_USER and $ADMIN_PASSWORD.

This useful script requires no database, and no big tweaking to the code on your pages.

PHP script to import csv data into mysql

Monday, February 19th, 2007

This is a simple script that will allow you to import csv data into your database. This comes handy because you can simply edit the appropriate fields, upload it along with the csv file and call it from the web and it will do the rest.

It allows you to specify the delimiter in this csv file, whether it is a coma, a tab etc. It also allows you to chose the line separator, allows you to save the output to a file (known as a data sql dump).

It also permits you to include an empty field at the beginning of each row, which is usually an auto increment integer primary key.

This script is useful mainly if you don’t have phpmyadmin, or you don’t want the hassle of logging in and prefer a few clicks solution, or you simply are a command prompt guy.
Just make sure the table is already created before trying to dump the data.
Kindly post your comments if you got any bug report.

Download file here


How to import a csv file into mysql from the prompt

Thursday, January 11th, 2007

For the follows out there who do not know how to import coma separated data into mysql from the prompt, here it is:

Assume:

PATH is the path to the csv file (can be relative to the directory you were in before logging into mysql)
TABLE is the table name that you have already created and whos fields match the csv file fields.
USERNAME is the mysql user that have access to insert data into TABLE
DATABASE is the database containing that table

- login to mysql by typing: mysql -u USERNAME -p DATABASE
The prompt will ask you for the user password, type it.

- Execute the following query:

load data local infile ‘PATH’ into table TABLE fields terminated by ‘,’ lines terminated by ‘\n’;

And voila.