TAG | Visitors
Today we’re going to make a simple script that logs all of your visitors and puts it into a database. I will comment every step so you will understand how it works. For newbies who don’t understand PHP yet check out the first tutorial.
<?php
$mysql_connect = mysql_connect('host', 'user', 'password') or die("We could not connect to the MySQL server.");
// This is simple a connection to the database.
// I'll provide some details on how to construct the table inside the DB later.
$mysql_database = mysql_select('database') or die("We could not select the database.");
// Just pick a database where u made your 'visitor' table
// Note: or die(mysql_error()); Is very handy in most cases. It returns a very specific error.
// So now we have the database connection
$user = array();
$user['ip'] = $_SERVER['REMOTE_ADDR'];
$user['browser'] = $_SERVER['HTTP_USER_AGENT'];
$user['page'] = $_SERVER['PHP_SELF'];
// We now create an array and we fill in keys that contain values.
// These values are generated by PHP when a user runs your script (opens a page)
// These values then get put in the $_SERVER array and you can simply fetch them from there.
$insert = mysql_query("INSERT INTO `visitors` (`ip`,`browser`,`page`)VALUES($user['ip'], $user['browser'], $user['page']) ") or die(mysql_error());
// This query inserts all the information we just gathered into our table.
?>
Now some info on how to create this table
The table name : visitors
Fields: 4
First field is the : visitor_id it is the Primary field and it auto increments and an Integer
This means that it simply starts from 0 and always goes up , its a unique id for each field
It helps a lot when you need to select one of these to set an ip to banned for example if you have such a column
Second field : visitor_ip VARCHAR 255 or something
Third field : browser VARCHAR 355
Fourth field : page VARCHAR 35
You can easily set up tables and databases using PHPMyAdmin
