This guy has faster fingers than me.This has to be done at server level.
Huh, variable as in? JS variable? It will not remember anything after one page view.You can declare a variable that will be incremented every time a unique visitor (determined by his IP and UserAgent for example) hits your front page or whatever. Then you can check this visit's number and trigger the wanted bihavior for x visit.
the ideal approach will be using redis for storing your count.
if (Math.floor(Math.random() * 10) + 1 == 1) {
your code here
}
No serverside stuff neededOver time it will average out to execute on every 10th user.
The way I think it can be done easily is by using the database. Or some file. His solution won't be accurate.Ha, this is actually pretty good provided you don't need to be super accurate! Way less work than interacting with a cache.
Huh, variable as in? JS variable? It will not remember anything after one page view.![]()
Ahh ok I give u the benefit of doubt (weird how the client side language got into server side but... let's not debate that).Server side ofc... ( he was talking JS, i assumed it was Node.js )
Eh it depends. If the site gets a lot of traffic (e.g. 10 per second) this would easily be accurate enough. Also if he just wanted the code to execute for every 10% of users, it will do exactly that. It wasn't really clear from his question if that's enough for him.The way I think it can be done easily is by using the database. Or some file. His solution won't be accurate.
For accuracy, definitely store the counter in db table or a file (file has more problems than db).
Still, it's a random number. He is not executing it every 10th request. It's not the same, ultimately.Eh it depends. If the site gets a lot of traffic (e.g. 10 per second) this would easily be accurate enough. Also if he just wanted the code to execute for every 10% of users, it will do exactly that. It wasn't really clear from his question if that's enough for him.
(weird how the client side language got into server side but... let's not debate that).![]()
Hehe. Seriously, I dunno why someone would run a browser engine on the server, except for surfing.Oh the typical JS shaming. :-D
Yes, like I said, it depends on what he uses it for and how often it gets executed.Still, it's a random number. He is not executing it every 10th request. It's not the same, ultimately.
Statistically though, it might actually be close to the same depending on the number of execution and accuracy.
Hehe. Seriously, I dunno why cnick79 would run a browser engine on the server, except for surfing.![]()