Комментарии:
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 :)
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.
ОтветитьThank you very much.
Ответить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.
Cannot edit the tables because they do not have a primary key.
Nice tutorial.
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.";
}
?>
function ip_exists($ip) -> i don't see where the $ip parameter is used in the function, or maybe i am in wrong
Ответитьi got two data for two ip 127.0.0.1 and ::1
ОтветитьHow This tut dynamic page?
Ответить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.
Ответить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.
ОтветитьWon't it be more efficient code just to say
if (mysql_num_rows($query_run)==1) {
ip_add()
}
gives me that error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given on line 10
ОтветитьTry to use "127.0.01" instead of "localhost" in your browser... it should fix that problem.
ОтветитьAnother way to create a database hit counter
Ответить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?
Ответитьstandard ipv6 default
ОтветитьCan't i just put unique function in phpmyadmin?
Ответитьok that was a bit late but thanks!
Ответить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 ?
ОтветитьFor those unfamiliar with boolean return statements, you could eliminate the if/else in ip_exists by just writing: return $query_num_rows == 1
Ответить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?
Ответить@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.
Ответить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..??
Ответить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.
Ответить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.
Ответитьu r awesome alex..
Ответитьwhy is my ip ::1?
Ответить