MY mENU


Tuesday, 20 March 2012

Log Visitors IP Address in PHP

OK start up MySQL and then enter the following to create the database .

CREATE DATABASE ipvisits;

USE ipvisits;

mysql> CREATE TABLE visitorsips(
-> ip VARCHAR(50),
-> date VARCHAR(50));

Now we will insert our visitors IP addresses into the database with the following code

                                    //get the visitors ip address
$ip_address = $REMOTE_ADDR;
                                           //get the date
$the_date = date("Y-m-d");
                                     //connect to MySQL using your details
$conn = @mysql_connect("localhost","username","password") or die("cannot connect to MySQL");
                                          //select the database
@mysql_select_db("ipvisits") or die("cannot connect to the database");
                                         //execute the query
@mysql_query("INSERT INTO visitorsips(ip,date) VALUES (,|$ip_address,|,,|$the_date,|)");
                                               //close the connection
mysql_close($conn);
?>

Now you have got a system for logging peoples IP addresses that visit your site .

No comments:

Post a Comment