Jump to content

How can you tell how many people are on your site?


env3rt

Recommended Posts

I need to know how to tell how many people are viewing your site. I imagine the code would be something like this:

<?php
session_start();  
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
echo "
<script type='text/javascript' for='window' event='onunload'>
$_SESSION['views'] = $_SESSION['views']- 1;
</script>";

echo "views = ". $_SESSION['views']; 
?>

(the above doesn't work)

Unless there is a php onunload function.. please help :)

 

No, that won't work.  Each user has their own session, and different sessions cannot directly interact with each other.

 

What you would need to do is use session_set_save_handler.  This will store all session data in a table in your database.  You can then count the number of active sessions by querying that table.  This not only allows extra functionality, but increases security, as well.

Learn.  Really, if your site is to store any information at all, you need a database of some kind.

 

As far as liking "php files more than databases", it's not an either or type of thing.  PHP is a hammer.  A database is a drill.  They're both extremely useful, but do completely different jobs.  If you want work with PHP, learning to use a database is essential.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.