views:

51

answers:

4

Is there some way to store:

  • A visitor's IP Address
  • What time a visitor visited my site
  • How many times they visited

I know there is a way to do this without PHP, just with Javascript and some text files.

Edit 1: How it would I do this in PHP?

+5  A: 

There's no way to store anything on a client computer using pure javascript. It runs in a sandboxed environment that doesn't allow you to perform such tasks. You could send an AJAX request to a server side script that will perform the necessary steps to persists the data. Another option of course is to use some service like Google Analytics which will take care of persisting statistics about your site visits by including a simple script at the end of your pages.

Darin Dimitrov
+1 for Google Analytics - it's the gold standard of activity tracking these days.
Ryan Kinal
A: 

you will need some kind of server side processing if you want to write to files on the server. if you want client-side storage, it is a relativley new feature (HTML5), but doesn't allow access to the filesystem directly.

ozk
A: 

Technically, though not by best practice, you could track this via cookie, thereby allowing you to use javascript. However, this would be destroyed in the event that the client deleted the cookie.

In any server side environment, you can write a tracking element of some sort based on one of several factors. sessionid is still usually dependent on a cookie, so you may be able to track unique sessions per an IP address, if you see periodic non simultaneous sessions you can reasonably deduce that this is a single client behind a public IP. However if you notice multiple simultaneous sessions behind the public IP, there is a private network with some sort of translation, or an enthusiast/developer looking at your site across different browsers.

Another way to get more granular with client data is to analyze the user agent string. This allows you to handle the bots/spiders and do a little more to enforce your robots.txt (of course nothing is 100%)

Just remember, the server is your domain, but the client belongs to the client. They ultimately get to choose what to do with the data on their machine. s/He who controls the spice... erm data, controls the universe.

fauxtrot
A: 

In PHP, If I understand what you are trying to do, what you are trying to accomplish is very easy.

To get the visitors IP Address, you would simply use the $_SERVER superglobal, like so:

<?php $_SERVER['ip_address']; ?> 

Then, to get the time, you would simply use:

<?php time(); ?> 

Now, the slightly more tricky part, how many times they visited. You need to put the above into a database, e.g. mysql.

Create a database schema, with the following columns: id, ipAddress, time and numOfVisits. You would need to have some php logic at the start of your webpage to check if the users IP Address is already in the database, and if it is, increment the numOfVisits column. You would obviously need to log the output of time() into the time column also.

In essence, that's how you would do what you are asking in PHP. Hope this helped :)

James Eggers