AZForums: Counting unique hits (PHP) - AZForums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Counting unique hits (PHP)

#1 User is offline   Strum Icon

  • AZ Student
  • PipPipPipPipPip
  • View blog
Group:
Members
Posts:
229
Joined:
22-April 06

Posted 06 June 2006 - 12:09 AM

Ok, let's start with the SQL. Put this into your phpMyAdmin after you have made the database & the user.

CREATE TABLE supporters_ips(
id VARCHAR( 15 ) NOT NULL,
ip VARCHAR( 100 ) NOT NULL
);

CREATE TABLE supporters(
id INT( 11 ) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( id ) ,
site_adress VARCHAR( 100 ) NOT NULL ,
site_title VARCHAR( 100 ) NOT NULL ,
hits VARCHAR( 100 ) NOT NULL DEFAULT '0'
);


That creates the tables.

Now for supporters_config.php

<?php
$db_user = "YOUR USERNAME";
$db_pass = "YOUR PASS";
$db_host = "localhost";
$db_name = "YOUR DATABASE";

$mysql_access = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
?>


That just connects to the database.

Now view_hits.php this is your admin panel to view the number of hits that the site sends to you, and to add new sites.

<body bgcolor="#000000">
<b>
<center><table width="500" border="1" bgcolor="#2A7FFF">
<tr>
<td width="66%"><center>
<b><u>Site Name:</u></b>
</center></td>
<td width="33%"><center>
<b><u>Number of sent hits</u></b>:
</center></td>
</tr>
<?php
include 'supporters_config.php';

//select the table
$result = mysql_query("select * from supporters");

//grab all the content
while($r=mysql_fetch_array($result))
{
$site_title=$r["site_title"];
$site_address=$r["site_adress"];
$number_of_hits=$r["hits"];
$id=$r["id"];
//display the row in a table
echo "<tr>
<td width=\"66%\"><center><b><a href='$site_address' target='_blank'>$site_title</a></b></center></td>
<td width=\"33%\"><center><b>$number_of_hits</b></center></td>
</tr> ";
}
?>
</table>
<p>
<a href="add_new.php">Add a new site</a>
</center>
</b>
</body>


Now add_new.php, this adds a new site (what a suprise).

<body bgcolor="#030003">
<b>
<center>
<table border="0" bgcolor="#2A7FFF">
<tr>
<th scope="row"><b>
<?php
include 'supporters_config.php';
if($_POST['submit']){
$site_name = $_POST['site_name'];
$site_address = $_POST['site_address'];

if ($site_name == ""){
echo "One or more fields are left blank.";
}
elseif($site_address == ""){
echo "One or more fields are left blank.";
} else {
$sql=mysql_query("INSERT INTO `supporters` ( `site_title` , `site_adress` ) VALUES ('$site_name', '$site_address')");

$result = mysql_query("select * from supporters limit 1");

//grab all the content
while($r=mysql_fetch_array($result))
{
$id=$r["id"];
//display the row
echo "Congratulations, you have successfully added a new site. That new site's id is $id.<p><a href=\"view_hits.php\">Go back?</a>";
}
}
} else {
?>
<form action="?submit=true" method="post" name="form1" id="form1">
<label>Site Name:
<input type="text" name="site_name" />
</label>
<p>
<label>Site Address:
<input type="text" name="site_address" />
</label>
</p>
<p>
<label>
<input type="submit" name="submit" value="Submit" />
</label>
</p>
</form>
<?php
}
?>
</b></th>
</tr>
</table>
</center>
</b>
</body>



Now in.php, this page is what counts the incoming hits.

<?php
ob_start();
include('supporters_config.php');
$id=$_GET['id'];
if (is_numeric($id)) {
$ip = $_SERVER['REMOTE_ADDR'];

$sql22 = @mysql_query("SELECT * FROM supporters_ips where id = '$id' and ip = '$ip'");
$unique_visitors = @mysql_num_rows($sql22);
if ($unique_visitors == 0){


$sql2=mysql_query("INSERT INTO `supporters_ips` ( `id` , `ip` ) VALUES ('$id', '$ip')");
$sql=@mysql_query("SELECT * FROM supporters WHERE id = $id LIMIT 1");
$r=@mysql_fetch_array($sql);
$hits=$r['hits'];
$hits++;
mysql_query("UPDATE supporters SET hits = '$hits' WHERE id = '$id'");
//Everything is successfull!
header("Location: index.html");
} else {
header("Location: index.html");
}
} else {
header("Location: index.html");
}
ob_end_flush();
?>


Remember to change the header("Location: index.html") to header("Location: YOUR_SITE'S_HOMEPAGE").

Well that is about the whole tutorial. Please try to leave credit, as this took time to write.

:D
RedLine Media™
Anizone Gift : [X] | [X]
MOTW #2 Winner [X]

Love Mozilla : Posted Image
0

#2 User is offline   Rokan Icon

  • AZ Guru
  • PipPipPipPipPipPipPipPipPipPip
  • View blog
Group:
Disabled Account
Posts:
6,665
Joined:
29-August 05

Posted 07 June 2006 - 08:35 AM

Another Great Tutorial Strum. I'll move this along with your others to the web tutorials forum.

Thank you for submitting it :)
Enjoy your hypocritical forum.

#3 User is offline   devcline Icon

  • AZ Beginner
  • PipPip
  • View blog
Group:
Members
Posts:
21
Joined:
11-November 08

Posted 11 November 2008 - 03:29 PM

looks like affilate script?
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users