Jump to content

IP Scripting Help


ChompGator

Recommended Posts

Hey,

 

Below I have put my script up, what it does is, show users their IP Address, and the date, and what visitor number they are.

What Im requesting help with is, what would I add to this script that is say you visit the site, and click on 'overview' and say your visitor 54, when you click that 'overview link' your now visitor '55' if their IP address is the same, I dont want the visitor number to keep going up, so if anyone could add to my script to help me  out, or give me any tips to help me out that would be great! Thanks

 

<?php
$host="***"; // Host name 
$username="**"; // Mysql username 
$password="**"; // Mysql password 
$db_name="**"; // Database name 
$tbl_name="counter"; // Table name 

// Connect to server and select database.
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 have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}

echo "You are visitor number: $counter <br>Your IP Address: ";
echo $_SERVER['REMOTE_ADDR'],"<br>\n";
echo date("m/d/y");

// count more value
$addcounter=$counter+1;
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);

mysql_close();
?>

Link to comment
Share on other sites

Well, to prevent the user from being counted again, you'll probably want to set a cookie so that the user is not counted if that cookie exists.

 

Try this:

<?php
$host="***"; // Host name 
$username="**"; // Mysql username 
$password="**"; // Mysql password 
$db_name="**"; // Database name 
$tbl_name="counter"; // Table name 

// Connect to server and select database.
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 have no counter value set counter = 1
if(empty($counter)){
$counter=1;
$sql1="INSERT INTO $tbl_name(counter) VALUES('$counter')";
$result1=mysql_query($sql1);
}

$addcounter=$counter+1;

// If the cookie 'site_counter' does not exist, or is null; Set a new cookie and add 1 to the counter in the database.
if(!isset($_COOKIE['site_counter']) || $_COOKIE['site_counter'] == null) {
setcookie("site_counter", $addcounter, time()+5400); // set the cookie to expire in one and a half hours (change this to whatever you want)
// count more value
$sql2="update $tbl_name set counter='$addcounter'";
$result2=mysql_query($sql2);

echo "You are visitor number: ".$addcounter." <br>Your IP Address: ";
echo $_SERVER['REMOTE_ADDR'],"<br>\n";
echo date("m/d/y");
}
// If the cookie exists, there is no need to create a cookie OR add a visit to the counter. Show the user their number.
else {
echo "You are visitor number: ".$_COOKIE['site_counter']." <br>Your IP Address: ";
echo $_SERVER['REMOTE_ADDR'],"<br>\n";
echo date("m/d/y");
}

mysql_close();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.