All PHP Tutorials
Ad Management
Counter
Email System
Forum and Guestbook
File Upload
Image Manipulation
Login / Members / Password
Pagination
Encode Script
Refresh / Redirection
Miscellaneous
All MySQL Tutorials
Create, Manage Database using phpMyAdmin
Connect to Database
Insert Data
Select Data
Edit Database
Update Database
Delete Database
Order Results



Home > PHP Tutorials
PHP Counter script tutorial
PHP Counter script tutorial
Creates your own counter script. Using a basic knowledge of php + mysql script and keeps your visitors number to database for analysis. 
 
Overview

In this tutorial create 1 file
1. counter.php

Step
1. Create table "counter" in database "test".
2. Create file counter.php.


Create table "counter"

CREATE TABLE `counter` (
`counter` int(9) NOT NULL default '0'
) TYPE=MyISAM;



Create file counter.php

############### Code

<?php
$host="localhost";
// Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");
mysql_select_db("$db_name")or die("cannot select DB")

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
$counter=$rows['counter'];

// if have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}

echo "You 're visitors No. ";
echo $counter;

// count more value
$addcounter=$counter+1;
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);

mysql_close();
?>

Random
 
PHP Redirection script
Learn how to redirect in this tutorial, you can use header(); function in php or use meta to redirect to a new page you want or redirect to other website. 
 
 
   
Hosted by Hostgator
© PHPeasystep.com 2005-2007