Sponsored by HowLifeWorks
How Private Online Shopping Clubs Work view!
howlifeworks.com - How to become a member and get discounts of up to 80% on must-have luxury goods
26 Comments
- hijinks, on 10/12/2007, -1/+18its not really a great idea to do this kind of thing based on IP.. the main reason is that most large companies and even colleges now have everyone behind a gateway. so you can have multiple users updating the same row in the database.
Its probably a better idea to set a random session/cookie hash and update off that - ThinkFr33ly, on 10/12/2007, -0/+8Wow, talk about an absolutely horrible way to do things.
First of all, under and decent load this thing will cause a crap load of problems. The database operations are not within a transaction and therefore you're likely to get plenty of nice errors when multiple requests come in from the same IP in close succession.
Not to mention the fact that IP address is an incredibly unreliable way to count who is online. This guy should instead throw a cookie down with a unique ID in it.
Also, it is completely retarded to hit the DB every time a user visits your page, much less hit the database 2 or 3 times. No wonder so many people can't handle a slashdotting or the digg effect when tutorials like this are around.
If your server is a single machine, use an in memory hashtable to store this kind of stuff. Write a cookie down to the user's machine with a globally unique ID and set it to expire when the user closes their web browser (or whatever you want.) Store that same ID in the hashtable and only increment your counter if the ID isn't in the hashtable.
Be sure to update your counter in a thread safe manner (I assume PHP has something like that) otherwise you'll miss some increments under high load.
If your server is a web farm the things get more tricky and you *may* have to use an external store of some kind, like a database, to store your ID index. In this situation it is *highly* recommended to use a message queue of some kind and keep the processing asynch. Yes, this will result in the counter being "lagged", but it's better than your server going down under load or not being able to handle more than 5 req/s.
Uniquely identifying computers is very, very hard... if not impossible without code on the client, but using IP address is just silly. - ers35, on 10/12/2007, -1/+8http://www.phpit.net/demo/creating%20a%20whos%20online%20script/simpledisplay.php
We can see all the ip addresses of Digg members. :) - hijinks, on 10/12/2007, -1/+4this is untested.. but to set a random hash in a session at the top of your file you would do like
session_start();
if (!is_set($_SESSION['hash']) $_SESSION['hash'] = md5(substr('123456789', rand(0,9), 1) * time());
then just update off that $_SESSION['hash'] like the tutorial shows you.. but of course replace the ip with the unique hash you just generated.
now this is just like untested code but it gives you an example of how to set a random hash.. there are better ways to do it - hijinks, on 10/12/2007, -3/+6also its not a good idea to delete that users table on every request.. you should do some modulus there and delete on every 10 page requests to take some load off your DB if your site ever gets big..
this is why I HATE these newbie php tutorials they show the basics but not the best practices. - hijinks, on 10/12/2007, -0/+2glob() takes up no system resources?? what have you been smoking.. so everytime a visitor joins it your server has to do another filesystem read.. sure its fast but db is much more efficent since it could be stored in memory
- ThinkFr33ly, on 10/12/2007, -0/+2FOBL - Honestly I'm not sure I'm the best one to help you with PHP. I'm a .NET/C# guy with some Java when I have to. I'm sure there are plenty of people on Digg that are knowledgable regarding PHP, although judging from the fact this article got 300+ diggs, I'm not so sure.
As far as .NET/ASP.NET stuff, I can definitly help you there. Dino's new ASP.NET book is great (by MS Press), and Jeff Richter's new C#/CLR book is even better than his .NET 1.1 book. (Also MS Press.) You should also check out Fritz Onion's ASP.NET book(s) as they are all excellent. (APress, I think.) - reldren, on 10/12/2007, -1/+3Good lord. Do people actually put crap like this into production? I'd fire any coder that committed garbage like that.
- jeremymcanally, on 10/12/2007, -2/+4Why are these really terrible, really commonplace tutorials getting dugg to the front page?
- Cakey, on 10/12/2007, -0/+1There must be a bug becuase my IP is not showing... EDIT: Takes some time but it is now showing...
Is it wishful thinking or is that an actuall script installed for Digg?
EDIT AGAIN: I see! People are directed from Digg to that demo page! Thanks for standing by; as I didn't get it for a sec! - vafada, on 10/12/2007, -0/+1the 1x1 image is a good idea since browser makes a separate request for images... this means you wont delay the main php code in case the process takes a lot of time.
- golddigga, on 10/12/2007, -0/+1why write it yourself when you can skip to the next page and click download
- Splizxer, on 10/12/2007, -0/+1Whats the purpose of making a web bug like this? Why not just do a php include in the page instead of making it an image?
Unless I'm totally misunderstanding the point. - FOBL, on 10/12/2007, -2/+3ThinkFr33ly,
In the vein of the above comment by jer2eydevil88, thanks for the informative post, but what are some resources you would recommend for a beginner to learn the techniques you describe? If the linked tutorial is deficient, what tutorials/books would you recommend for a beginner to learn how to do things the right way? - NielsT, on 10/12/2007, -0/+0An OK beginners tutorial. However, I do not like the 'hidden feature' in their query function. Adding showqueries=1 to a url displays the queries being executed (you can also get an EXPLAIN of the query). Not a big security problem in this case but something you want to remember if you reuse their code (and have register_globals enabled).
- KJay, on 10/12/2007, -1/+1Yea, definitely better to do it with a session ID. Also, if you are already serving up pages via PHP, you can just throw the scripting in there instead of doing the whole 1x1 gif thing.
- danglerman, on 10/12/2007, -1/+1i wish digg would do this
- FOBL, on 10/12/2007, -2/+1Thanks, ThinkFr33ly. I'll check those out.
- mudx, on 10/12/2007, -1/+0Why not just count the sessions that are running in the /tmp/ directory on your server? They're just serialized data.
What you do is run glob() on the /tmp/ directory (I would recommend setting up a custom sessions directory if you have more than one website on your server). Then run unserialize() on each session file, the ones with data in them are your online users (you know what data you're storing in your sessions, so you should know what to look for). The ones without, are your offline users...
This will take basically NO system resources, and does the exact same thing. This tutorial is lame. - jer2eydevil88, on 10/12/2007, -4/+2Not be ungrateful for the advice but could you supply any example script for that?
- pixelmixer, on 10/12/2007, -3/+1Awesome tutorial.. I like the approach..
But wouldnt it be easier to add a viewing flag to the users stats and just update that when a new page is requested.
I've never done anything with a bug but it is a good idea.
Great tut IMO. - gharding, on 10/12/2007, -2/+0Author apparently has never heard of INSERT ... ON DUPLICATE KEY UPDATE. Although that was only added in 4.1 (are people still seriously using 4.0?)
- rusty_g, on 10/12/2007, -6/+3i always like a well done tutorial.. always good to learn more
- exussum0, on 10/12/2007, -3/+0nuuuuuuuuuuuuu.. dont' do this. because php does not provide caching, you'll require a LOT of db interaction per request. php has no way of caching the retrieved data, even for a short while.
- fatdog789, on 10/12/2007, -8/+3All these beginners php tutorials are really starting to get lame. This is not a php tutorial site.
Marked lame. - inactive, on 10/12/2007, -16/+0WOAH!!!!!!!!!!!!!!!!!!!!
What is Digg?