Beginner PHP Tutorial - 126 - Creating a Database Hit Counter Part 4

Beginner PHP Tutorial - 126 - Creating a Database Hit Counter Part 4

thenewboston

13 лет назад

38,796 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Anurag
Anurag - 29.07.2017 10:09

here is the working code for latest versions of PHP:
////////////FILE to connect to database(connect2.inc.php ::::: in my case)


<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link= mysqli_connect("localhost", "root", "", "adatabase");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}


?>


////////////THIS IS the main file i.e hit_counter.php
<?php

require 'connect2.inc.php';
$user_ip = $_SERVER['REMOTE_ADDR'];

function ip_exist($ip){
global $user_ip;
global $link;
$query = "SELECT `IP` FROM `hits_ip` WHERE `IP` = '$user_ip'";
$query_run = mysqli_query($link , $query);
$query_num_rows = mysqli_num_rows($query_run);
if($query_num_rows == 0){
return false;
}
else{
return true;
}
}

function add_ip($ip){
global $link;
$query = "INSERT INTO `hits_ip` VALUES('$ip')";
$query_run = mysqli_query($link , $query);
}

function update_count(){// will select count and then increment
global $link;

$query = "SELECT `Count` FROM `hits_count`";

if($query_run = mysqli_query($link , $query)){

$count = mysqli_fetch_row($query_run);

$count[0] = $count[0] + 1;

$query_update = "UPDATE `hits_count` SET `Count` = '$count[0]' ";

$query_update_run = mysqli_query($link , $query_update);


}
}
if(!ip_exist($user_ip)){
update_count();
add_ip($user_ip);
}
?>

HOPE THIS WILL HELP :)

Ответить
Parviz Karimli
Parviz Karimli - 02.03.2017 10:11

In fact, adding the line `global $user_ip` to the function `ip_exists` is nonsense cos you're passing an argument. So you didn't use it at all.

Ответить
Guram Kankava
Guram Kankava - 07.12.2016 17:39

Thank you very much.

Ответить
Gregor Kovačič
Gregor Kovačič - 03.06.2016 15:42

Here is updated version of the code that works:

<?php
require 'connect.inc.php';
$user_ip = $_SERVER['REMOTE_ADDR'];

function ip_exists($ip) {
global $user_ip;
global $conn;
$query = "SELECT `ip` FROM `hits_ip` WHERE `ip`='$user_ip'";
$query_run = mysqli_query($conn,$query);
$query_num_rows = mysqli_num_rows($query_run);
if ($query_num_rows==0) {
return false;
} else if ($query_num_rows>=1) {
return true;
}
}

function ip_add($ip) {
global $conn;
$query = "INSERT INTO `hits_ip` VALUES ('$ip')";
$query_run = mysqli_query($conn,$query);
}

function update_count() {
global $conn;
$query = "SELECT `count` FROM `hits_count`";
$query_run = mysqli_query($conn,$query);

if($query_run)
{
$count = mysqli_fetch_row($query_run);
$count_inc = $count[0]+1;

$query_update = "UPDATE `hits_count` SET `count` = '$count_inc'";
$query_update_run = mysqli_query($conn,$query_update);

}
}

if (!ip_exists($user_ip)) {
update_count();
ip_add($user_ip);
}

?>

<h1>My page</h1>

If you are wondering how my connect.inc.php works, check previous video (part 2). For some reason when I was setting count value to '0' in phpMyAdmin, it wouldn't increment it. It would increment normally, if I would start from value '1'. I figured out that if I just delete value and press go, it would read as '0' and work normally - saying this just in case anyone is wondering, because I was wondering for an hour why would it do that.

Ответить
Paulo Moniz
Paulo Moniz - 11.02.2016 19:50

Cannot edit the tables because they do not have a primary key.

Nice tutorial.

Ответить
Lokesh Sivakumar
Lokesh Sivakumar - 19.01.2016 15:57

Much simple solution:

<?php
require 'connect.inc.php';

$user_ip = $_SERVER['REMOTE_ADDR'];

function ip_exists($ip){

$query = "SELECT `ip` FROM `ip_hits_list` WHERE `ip`='$ip'";
$query_run = mysql_query($query);
$num_rows = mysql_num_rows($query_run);
if ($num_rows==0) {
return false;
}
elseif($num_rows>0){
return true;
}

}


function add_ip($ip){
$query = "INSERT INTO `ip_hits_list` (`ip`) VALUES ('$ip');";
$query_run = mysql_query($query);
}

function update_count(){
$query = "SELECT `count` FROM `count_hits`";
$query_run = mysql_query($query);
$result = mysql_result($query_run, 0);

$update_result = $result + 1;
$update_query = "UPDATE `count_hits` SET `count` = '$update_result'";
$update_query_run = mysql_query($update_query);
}

if (ip_exists($user_ip)) {
echo "IP already exists";
}
else{
add_ip($user_ip);
echo "IP added to the list";
update_count();
echo "'Count' table updated.";

}

?>

Ответить
adminos15
adminos15 - 09.12.2015 18:11

function ip_exists($ip) -> i don't see where the $ip parameter is used in the function, or maybe i am in wrong

Ответить
stephen cabreros
stephen cabreros - 02.10.2015 10:17

i got two data for two ip 127.0.0.1 and ::1

Ответить
Sport Time
Sport Time - 13.08.2015 21:15

How This tut dynamic page?

Ответить
Niall o connor
Niall o connor - 27.07.2015 11:05

Hey guys if using XAMMP like pancakeplease. It will return the ip as a decimal ip = 127.0.0.1. You need to change the type of input of the user id from an interger to decimal so that it will be stored as a decimal(127.0.0.1) and not a whole number 1270.

Ответить
Krishna Saraswat
Krishna Saraswat - 21.03.2015 09:32

global $user_ip in function ip_exists isn't required as we are already fetching the user id from the parameter $ip passed in the ip_exists function.

Ответить
POS
POS - 06.12.2013 14:20

Won't it be more efficient code just to say
if (mysql_num_rows($query_run)==1) {
           ip_add()
}

Ответить
Mohamed Samir
Mohamed Samir - 13.08.2013 04:44

gives me that error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given on line 10

Ответить
Žiga Kokelj
Žiga Kokelj - 14.07.2013 22:37

Try to use "127.0.01" instead of "localhost" in your browser... it should fix that problem.

Ответить
Danish Shaikh
Danish Shaikh - 08.07.2013 18:35

Another way to create a database hit counter

Ответить
daniel live
daniel live - 05.07.2013 15:14

Actually, what is the use of return false? I can just left it blank over there and it will get the same result, isnt it?

Ответить
DarkHenmore
DarkHenmore - 27.05.2013 21:14

standard ipv6 default

Ответить
ArnoldsK
ArnoldsK - 08.04.2013 19:52

Can't i just put unique function in phpmyadmin?

Ответить
paulceltics
paulceltics - 30.03.2013 23:29

ok that was a bit late but thanks!

Ответить
MrSupaxxx
MrSupaxxx - 03.12.2012 16:38

Alex, why do you create a separate if statement at the end? Why not make use of your if statement in the function ip_exists($ip) and just call the function ?

Ответить
padredoom
padredoom - 09.11.2012 23:19

For those unfamiliar with boolean return statements, you could eliminate the if/else in ip_exists by just writing: return $query_num_rows == 1

Ответить
JMRC
JMRC - 11.09.2012 20:12

Question: Isn't this better then storing it in a file as you did in a previous tutorial, sine a file shouldn't be opened more than once at the time?

Ответить
Anarchy
Anarchy - 03.03.2012 04:57

@PancakePlease VARCHAR shud work. This would be a great opportunity to practice troubleshooting. What you do first is check if your IP address is actually stored into your database. Visit your page and check if its stored. If it didnt store any IP, then you know theres an issue with your INSERT query. If it did store ur IP then you know something either went wrong with reading the IP from your database (so check by echoing that out) or your if statement in which you compare both IP's misbehaves.

Ответить
pancake
pancake - 23.02.2012 08:48

want to update and say that it worked after I uninstalled MAMP and reinstalled my XAMPP. With MAMP (on my osx) it was showing the IPV6 ip address as ::1, not the IPV4 which was 127.0.0.1. With XAMPP, it shows 127.0.0.1 Running the same code, it no longer keeps adding the ip to hits_ip and increasing the count. It stops like it should. Not sure why though, checking for "::1" wouldn't be the same thing as checking for 127.0.0.1?? Or is ::1 not VARCHAR but nothing else in MYSQL..??

Ответить
pancake
pancake - 23.02.2012 08:27

damn this doesnt work for me. type it verbatim what you wrote and while i receive no errors, the hit count keeps going up and my hit_ip keeps writing my IP over and over and over. I tried messing around with the return false/true statement in the function ip_exists() but nothing. My IP is also coming out as ::1 and not 127.0.0.1. I'm not sure why though. I understand it is the equivalent of localhost but maybe that's the problem to why this isnt working for me.

Ответить
Paweł Dziubałka
Paweł Dziubałka - 18.12.2011 00:20

it's really helpfull tutorial, but there is a little glitch. in function ip_exists($ip), you don't need to have the $ip part, because you don't really use supplied ip, but the global variable $user_ip.

Ответить
sutistalica
sutistalica - 12.11.2011 15:54

u r awesome alex..

Ответить
paulceltics
paulceltics - 19.10.2011 07:37

why is my ip ::1?

Ответить