Jump to content

MySQL stored page views?


ChrisMartino

Recommended Posts

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.