user1644622
user1644622

Reputation: 33

saving current ip addresses in mysql table

I am looking for a good way, to show how much people are accessing my website currently (live).

I figured out, that it would be probably the easiest way, when i save the ip address of the person, that accessing my server/site, in an mysql table.

so what i probably have to do is:

creating an well configured mysql table

adding the feature (i guess in php) of saving the ip address

adding the feature of deleting the ip address, if it isn't needed any longer

making the process realtime or close to realtime

Can somebody help me?

Upvotes: 3

Views: 16198

Answers (5)

Arek van Schaijk
Arek van Schaijk

Reputation: 1442

Store your visitors into the database (like the other answers). Use a datetime field wich holds the date of the last request of the visitor (you have to update the row every pageview).

Now u can calculate the "currently online visitors" by using MySQL interval.

For example:

SELECT
COUNT(1) AS `online_visitors`
FROM `visitors`
WHERE `datetime` > NOW() - INTERVAL 30 SECONDS

Upvotes: 2

RobMasters
RobMasters

Reputation: 4148

Firstly, may I suggest that this question isn't titled very well? It's a typical mistake for people to 2nd guess what the solution for a problem is when asking for help - I see this all the time in current/previous companies I've worked when staff are submitting bug reports.

The thing that you're trying to achieve is implementing some form of analytics so you can determine how many people are visiting your site, how many of them are unique visitors, and also being able to see realtime data of who is currently using the site. So a better title would be something like "How can I get information about who is visiting my site?".

Storing ip addresses in a table is a valid way of achieving this, but is far from ideal. I'd highly suggest using Google Analytics (or something along those lines) as this will enable you to see all of this information and more, plus it won't require very much effort on your part. You don't even have to be a coder - you just copy and paste the snippet they provide into your HEAD tag. There is plenty of information out there about how to do this so I won't go into detail, but don't hesitate to ask if you're having any problems.

Upvotes: 1

John Woo
John Woo

Reputation: 263933

Here is a quick example of how you might go about getting the real IP address of a client and then storing and retrieving its value from the mysql DB

//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
  $ip=$_SERVER['REMOTE_ADDR'];
}
//The value of $ip at this point would look something like: "192.0.34.166"
$ip = ip2long($ip);
//The $ip would now look something like: 1073732954

Now that you have the real IP address of the client converted to the INT format, you can write it into the DB as you normally would

$sql = "INSERT INTO user(ip) VALUES('$ip')";
$dbQuery = mysql_query($sql,$dbLink);

More on this link: Best way to store IP addresses in MySQL

additional information: to make you code safe from SQL Injection. Use PDO or MYSQLI

Example of using PDO extension:

<?php

    $stmt = $dbh->prepare("INSERT INTO user(ip) VALUES(?)");
    $stmt->bindParam(1, $ip);

    $stmt->execute();
?>

Upvotes: 5

Phil
Phil

Reputation: 11175

This is functionality that is already built in with Google Analytics, IP addresses aren't the best way of keeping track of visitor count.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

Everytime a page is requested, you can push the IP Address to the MySQL Database, so that it would be unique.

<?php
    $ipaddress = $_SERVER["REMOTE_ADDR"];
    mysql_connect();
    mysql_select_db("visitors");
    mysql_query("INSERT INTO `visitors` (`ipaddress`) VALUES ('$ipaddress')");
    mysql_close();
?>

Upvotes: 0

Related Questions