ChrisMartino Posted March 22, 2010 Share Posted March 22, 2010 Ok, So i want to make it so when someone visits a page it add's +1 to the number stored in the mysql database, how could i make it echo this value too?, Thanks. Link to comment https://forums.phpfreaks.com/topic/196176-mysql-stored-page-views/ Share on other sites More sharing options...
Bio Posted March 22, 2010 Share Posted March 22, 2010 I wrote a script to do this not long ago. Its 2 scripts, a table in a MySql database, and image display for the hit count. its pretty simple.. Ill see what i can do about posting it. Link to comment https://forums.phpfreaks.com/topic/196176-mysql-stored-page-views/#findComment-1030216 Share on other sites More sharing options...
Bio Posted March 22, 2010 Share Posted March 22, 2010 This is the main file: /counter.php <html> <head> </head> <body> <?php define ('SECURITY', True); include('./includes/connect.php'); $TheData = $_SERVER['HTTP_REFERER']; // make sure the script is called from a page not called directly. if ($TheData == "") { echo "This script must be embedded on a web page."; die; } // load the hit count $query = "SELECT hits FROM counters WHERE url='$TheData'"; $result = mysql_query($query) or die ("MySql Error. Please Inform YOUR_INFO_HERE if this problem persists."); $j = 1; while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) { foreach ($row as $colname => $value) { $array[$j][$colname] = $value; $value = strtoupper($array[$j][$colname]); } $j++; } $count = (int)$array['1']['hits']; // if this is a new url we need to create the record in the table. if ($count < 1) { $query = "INSERT INTO counters (url, hits, protected) VALUES ('$TheData', '0', '0')"; } // increase the hit count then update the table $count++; mysql_query("UPDATE counters SET hits = '$count' WHERE url = '$TheData'"); $CountDisplay = "0000000" . $count; $CountDisplay = substr($CountDisplay, -7); for ( $counter = 1; $counter <= 7; $counter++) { $worker = -8 + $counter; $Display = substr($CountDisplay, $worker, 1); ?> <img src="http://www.YOUR_URL_HERE/images/glass_numbers_<?php echo $Display; ?>.png" width="30" height="30"> <?php } ?> </body> </html> This is the file that connects to the database: /includes/connect.php You will have to fill in your database login info. <?php // First make sure this script isnt called from anywhere except our script. if ( !defined('SECURITY') ) { die("Hacking attempt"); } // // ======----------->>> define variables here <<<----------======= // // This is the MySql setup $host = "localhost"; $user = "username"; $password = "password"; $db = "database"; // // ======----------->>> end define variables <<<----------======= // // End Variables. Do not edit any code below this line. // Connect to the database $con = mysql_connect($host,$user,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } // Select the database mysql_select_db($db, $con); // unset variables to detour hacking attempts. unset ($host, $user, $password); ?> This will create the table for you: (SQL Code) CREATE TABLE IF NOT EXISTS `counters` ( `index` int(6) NOT NULL auto_increment, `url` varchar(120) collate latin1_general_ci NOT NULL, `hits` int(7) NOT NULL, `lasthit` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `protected` varchar(3) collate latin1_general_ci NOT NULL default '0', PRIMARY KEY (`index`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=6 ; I downloaded the icons here: http://www.google.com/imgres?imgurl=http://www.bittbox.com/wp-content/uploads/2007/02/glass_numbers.png&imgrefurl=http://www.bittbox.com/freebies/free-glass-number-icons&h=76&w=465&sz=11&tbnid=Oq40Jksb-sW-VM:&tbnh=21&tbnw=128&prev=/images%3Fq%3Dglass%2Bnumbers&usg=__7idxAuEpllD9HtLDhHzYTq1uudI=&ei=2OOnS9jAMpKYsgPukIHbAw&sa=X&oi=image_result&resnum=2&ct=image&ved=0CBMQ9QEwAQ And I call it using an iframe: (HTML) <iframe src= "http://YOUR_URL_HERE/counter.php"width="285" height="50" frameborder=0><p>YOUR BROWSER DOES NOT SUPPORT THIS FUNCTION.</p></iframe> Dont be too tough on me! I have not been writting PHP for a very long time.. But im open to suggestions! If you use it lemme know how it works out for ya. Link to comment https://forums.phpfreaks.com/topic/196176-mysql-stored-page-views/#findComment-1030224 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.