-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.php
More file actions
54 lines (45 loc) · 1.39 KB
/
Main.php
File metadata and controls
54 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
//Example program which incrments a counter in a database everytime a user with a new IP address visits
require_once 'Connect.php'; //Requiring Connect.php which connects to the database
$userIP = $_SERVER['REMOTE_ADDR']; //Get the current users IP address
function ipExists($ip)
{
$query = "SELECT `IP` FROM `userip` WHERE `IP` = '$ip'";
$queryRunner = mysql_query($query);
if(mysql_num_rows($queryRunner) == 0)//Counts the number of rows returned from the query and if there are none
{
return false;
}
else
{
return true;
}
}
function ipAdd($ip)
{
$query = "INSERT INTO userip (IP) VALUES ('$ip');";
$queryRunner = mysql_query($query);
}
function updateCounter()
{
$query = "SELECT count FROM hitcounter;";//Creating an SQL query
if(@$queryRunner = mysql_query($query))//Checking if the query is okay and returns the data from the query
{
$count = mysql_result($queryRunner, 0, 'Count');//Using the sql_result function to retrieve value of the query data.
//in row 0 column 'Count'
++$count; //Increment the count variable
$updateQuery = "UPDATE hitcounter SET `Count` = '$count'"; //Update the database with the new value. ($count)
@$queryUpdateRunner = mysql_query($updateQuery);
}
}
if(ipExists($userIP))
{
echo 'IP Exists';
}
else
{
ipAdd($userIP);
updateCounter();
echo 'New IP address, counter has been incremented, IP has been stored.';
}
?>