Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

hit counter php if else or session

Featured Replies

hi again i have a php script for a basic hit counter,the only problem is when ever i change paget it increases by 1 giving a false reading of the ammount of visiters how can i change it so that it only registers the 1 hit no matter how many pages are viewed thanks again

 

<?php

$host="localhost";

$username="******";

$password="******";

$db_name="test";

$tbl_name="counter";

 

mysql_connect("$host", "$username", "$password")or die("cannot connect to server ");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name";

$result=mysql_query($sql);

 

$rows=mysql_fetch_array($result);

$counter=$rows['counter'];

 

if(empty($counter)){

$counter=1;

$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";

$result1=mysql_query($sql1);

}

 

echo "You are visitor No. ";

echo $counter;

 

 

$addcounter=$counter+1;

$sql2="update $tbl_name set counter='$addcounter'";

$result2=mysql_query($sql2);

 

mysql_close();

?>

 

can i add a session or a if else function to make this work thank you

Two ways of doing it really. You can either set a cookie that tells the counter that they have already been logged down, or a more basic solution would be to check where the user has come from using HTTP_REFERRER.

 

So if the previous page was outside of your domain, it'll log a hit, if the previous page is within your domain, it won't. It'll still log multiple hits if someone leaves your site then comes back, but thats better than it logging on every page.

 

You'll want something along the lines of...

 

if (($_SERVER['HTTP_REFERER']) !== ($_SERVER[HTTP_HOST]))
{
// Log down a hit
}
else
{
// Do nothing
}

 

That won't work how you want it, but with some tweaking you should be on your way.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.