There is no perfect way of doing this as even an IP Address can change. If the user has a Dynamic IP Address
, the address can change frequently.
Either way, using the user's IP Address
, you will have to store the address somewhere on your server. If you have a database set up, like MySQL
, you can store the IP Address
in a table.
See the MySQLi
documentation page in the PHP
manual.
Once you are familiar with MySQL
, you can write code similar to this:
$mysqli = new mysqli("example.com", "user", "password", "database");
if (!$mysqli->connect_errno) {
//You should validate the IP Address
$ip = $_SERVER['REMOTE_ADDR'];
$res = $mysqli->query("SELECT IPAddress FROM tbIP WHERE IPAddress='{$ip}' LIMIT 1")
if($res !== false){
if($res->num_rows > 0){
//Found
echo "<script type='text/javascript'>alert('YES');</script>";
} else {
//Not found
echo "<script type='text/javascript'>alert('NO');</script>";
//Here you will probably want to INSERT a new record into the Database
//tracking the current user.
}
}
}
Again, I'd recommend reading up on MySQL
.
If you don't have access to a database and you have eliminated cookies
, you could write the IP Addresses
to a file instead (replacing the MySQL
calls with calls to read from the file. This is not usually a good choice, however if you have no alternatives, you don't have many options.