Deviant Login Shop  Join deviantART for FREE Take the Tour
×



Details

Submitted on
October 18, 2007
Image Size
42.3 KB
Resolution
900×700
Link
Thumb
Embed

Stats

Views
1,128
Favourites
0
Comments
10

License

Creative Commons License
Some rights reserved. This work is licensed under a
Creative Commons Attribution-Noncommercial 3.0 License.
×
counter.php by rotane counter.php by rotane
While I am by no means a programmer, I do get in touch with code every once in a while. Be it clumsy attempts in writing my own userscripts to customize dA, be it modding userscripts to further enhance and personalize my experience with websites like dA, be it copying lines of code off tutorial sites to enhance the experience of websites I create, be it HTML and CSS "code" when writing websites in the first place. But this is as far as it goes.

This few lines of code above I wrote while following a tutorial from 3D Buzz some time ago, trying to get into the php stuff a bit. What does it do? Simple, it's a webcounter, telling you how many people visited this page.

Info
- Editor: UltraEdit-32 10.10b (with customized colours)-
- Visual Style: b0se Classic Final by ~b0se.

This is a submission for and tribute to #CodeIsArt.
Add a Comment:
 
:iconcodeisart:
CodeIsArt Featured By Owner Jun 8, 2008
I was thinking about this one, and I came up with an idea, where you could use the write functions to simply append a byte to a file, and then use the file size function to find out how many bytes long it was. As long as you used the session to only rack up the counter once per session, 2 million visits would only be 2MB, and you could add in a counter-reset after, say 10MB.
Reply
:iconrotane:
rotane Featured By Owner Jun 8, 2008   General Artist
That is a pretty cool idea :plotting:
Reply
:iconcodeisart:
CodeIsArt Featured By Owner Oct 19, 2007
I'm not personally an expert in PHP. There's a great deal of commands and tricks one can employ for ultimate compactness, but I think this code is lacking in personality. For example:

* Why define $query, when you could just put it into the function call?

* Why use a database, which can be unreliable, when a text file could suffice? Also, running two SQL queries every time the page is viewed puts unnecessary strain on the DB under heavy load.

* You could also use the session to remember a viewer and prevent them from racking up the count just by pressing refresh.

* Why kill the whole PHP page when an SQL error occurs for the page view counter? This seems over-kill when the rest of the page may work fine or not even use the database. I would place the counter in a function, and just exit the function on an error so that people could still use the site, even if the counter was not working.

* You shouldn't do "SELECT *" it's extra load on the server, always select just the fields you need.

* The second query could be built directly off of the first one, like thus:

$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++.";";
mysql_query("UPDATE `counter` SET `num_views`=$views;");


This would replace lines 6-13 with two lines, and I'm certain there's an even more compact way to do that in PHP.

Thanks very much for your time and effort to submit your code, I've enjoyed looking at it :)
Reply
:iconrotane:
rotane Featured By Owner Oct 23, 2007   General Artist
Wow, what an extensive review, I'm overwhelmed!

I'll do my best to incorporate these into a possible future version, and then maybe make a direct comparison. Should be interesting. For now I can only say, I'm probably much more of a newb in PHP than you might think. But who knows, I'm always trying to learn.

Again, thanks! :thumbsup:
Reply
:iconcodeisart:
CodeIsArt Featured By Owner Oct 19, 2007
Correction, that should be:

$views=mysql_fetch_row(mysql_query("SELECT `num_views` FROM `counter`;",$connection))[0]++;
mysql_query("UPDATE `counter` SET `num_views`=$views;");
Reply
:iconkaleidokittles:
KaleidoKittles Featured By Owner Oct 19, 2007
Doesn't look like a lot of code when you glance at it, but yet it creates a web counter which is pretty nifty. PHP is just too much for me and that's why I respect anyone that can get to grips with it - much like yourself.

Nice one hon :thumbsup:
Reply
:icontrlcks:
TRlCKS Featured By Owner Oct 18, 2007  Professional Interface Designer
Ever tried using notepad++ rotane? my brother finds it extremely usefull and he's doing a degree in IT Management at the moment so should know exactly how this works more so than me :P

Very usefull code tho :) Perhaps adding on how long the page has taken to load might be a nice addon to this?
Reply
:iconrotane:
rotane Featured By Owner Oct 18, 2007   General Artist
Noo, I haven't even heard about it before, but it sure looks nice -- will check it out, mate, thanks for the hint. :thumbsup: Say, is your brother on dA? If so, he might be interested in the ~CodeIsArt project..

And thanks, I'll see what I can do.
Reply
:icontrlcks:
TRlCKS Featured By Owner Oct 18, 2007  Professional Interface Designer
Unfort not and he's in his final year at uni so he's pretty busy with coding for fake websites for his course lol well actually real ones now i guess XD but ill pass on the msg for sure. and [link] link to notepad++ if you hadnt already googled it :)
Reply
:iconrotane:
rotane Featured By Owner Oct 18, 2007   General Artist
Too bad, but I can understand. Oh well, there's always a tomorrow :)
Yep, tried it already, looks promising!
Reply
Add a Comment: