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 :)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.