|
|
Mar 17, PST
|
|
Creating the Database The first step to making this online visit counter is to create the database. Open up phpMyAdmin and click on SQL. The code below is what you'll need to copy and paste into the form in SQL and click Go. CREATE TABLE `views` (`ip` VARCHAR(30) NOT NULL, `time` VARCHAR(10) NOT NULL, PRIMARY KEY (`ip`)); The PHP code Now you'll need to make a new file. It doesn't matter what you name it. Mine is called hits.php. Copy and paste the code below into the file. <?php mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); //Put database username where it says USERNAME and your database password where it says PASSWORD mysql_select_db("DATABASE") or die(mysql_error()); //Put your database name where it says DATABASE //Do not edit anything below! $time = time(); $ip = $_SERVER["REMOTE_ADDR"]; $query = mysql_query("SELECT * FROM views WHERE ip='$ip'"); $query2 = mysql_num_rows($query); if ($query2 == 0) { mysql_query("INSERT INTO views (ip, time) VALUES('$ip', '$time')") or die(mysql_error()); } else { mysql_query("UPDATE views SET time = $time WHERE ip='$ip'") or die(mysql_error()); } $total = $time - 120; $delete = $time - 1800; mysql_query("DELETE FROM views WHERE time < '$delete'") or die(mysql_error()); //Deletes records that are no longer needed $database = mysql_query("SELECT COUNT(time) FROM views WHERE time > '$total'") or die(mysql_error()); while($views = mysql_fetch_array($database)) { //Selects and counts the visitors within the last 2 minutes $total_views = $views['COUNT(time)']; } echo "Total People Viewing Site: $total_views"; //You can edit this if you want. ?> You'll need to change the database username and password to the one you made. Adding the visit counter to your site pages I'll be using the include() function to make the visit counter show on your site pages. First you'll need to get the full directory path to the file where you saved the main code. It should look something like this: /home/USERNAME/public_html/hits.php. Once you get that you'll need to replace DIRECTORY in the code below with your path. <?php include('DIRECTORY'); ?> Put that code on every page. If you have any questions, comment below.
| Written by: Thomas | Added: Dec 11 2009 | Last Modified: Dec 21 2009 | Views: 959 | Member Comments
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Windymill | Neoeditor | Rabidish | KeliJo.net | NeopetsGuide | Hidden Star | The Neopian Bay | Skylish | Tugboat | Dash of Color | Neo Nutters | Smiley Helper | Spicy-Sugar | Vintaged.org | MSPT | Darkgirl's Life | Gamexe.net |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||