Welcome Guest. Please Login or Register  


You are here: Index > General Stuff > Scripting Help > Topic : AJAX (PHP) Active Users Script


News:

Group Message
Hello Guest,

Welcome to the AEF Board. AEF is getting better and better and is also becoming more popular.
Take a few seconds to Register, its free, and it inspires us to work harder and improve on whatever we can improve on.
Give us some feedback, and tell your buddies about AEF!

Thanks,
The AEF Team



Threaded Mode | Print  

 AJAX (PHP) Active Users Script, AJAX (PHP) Active Users Script (2 Replies, Read 2661 times)
ChrisVinecombe
Group: Member
Post Group: Newbie
Posts: 2
Status:
I\\\'m trying to develop a script to show which users are active on the site.  I\\\'m using a simple database called ActiveUsers that has two fields - Username and Timestamp.  The script works in firefox, but the second time it reloads the list of users it comes up blank, after that it seems to reload the information fine.  In IE it loads the list once, but afterwards it just keeps the same information up on the page.  The code is below.

PHP Code

<?php
    
include "./protected/dbConn.inc\";

    $mysqlConn = mysql_connect(\"$host\", $username, $password) or die(\"Couldn\'t connect to database\");
    mysql_select_db($database, $mysqlConn);

    if(isset($_COOKIE[\'uID\'])) {
    $un = $_COOKIE[\'un\'];
    $uID = $_COOKIE[\'uID\'];
    $query_session = \"select * from Users where username=\'$un\' and userid=\'$uID\'\";
    $session_result = mysql_query($query_session, $mysqlConn);
    if(!$session_result && mysql_numrows($session_result) < 1) {
        header(\"Location: login.html\");
    }
    } else {
    header(\"Location: login.html\");
    }
?>

<html>
    <head>
    <title>ChrisVinecombe.com - Active Users</title>
    <link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" charset=\"utf-8\" />
    <script language=\"JavaScript\">
    var user;
    var AJAX;

    function initAJAX() {
        user = document.getElementById(\'user\').value;
        try {
        AJAX = new XMLHttpRequest();
        } catch(e) {
        try {
            AJAX = new ActiveXObject(\"Msxml2.XMLHTTP\");
        } catch(e) {
            try {
            AJAX = new ActiveXObject(\"Microsoft.XMLHTTP\");
            } catch(e) {
            alert(\"Your browser can\'t use AJAX!\");
            return false;
            }
        }
        }
        setRefresh(1);
        setRefresh(30000);
    }
        

    function setRefresh(num) {
        setTimeout(\"UpdateActive()\", num);
    }

    function UpdateActive() {
        AJAX.onreadystatechange = function() {
        if(AJAX.readyState == 4) {
            var disp = document.getElementById(\'ShowUsers\');
            disp.innerHTML = AJAX.responseText;
        }
        }

        var ran= Math.floor(Math.random() * 1001);
        AJAX.open(\"GET\", \"getactive.php?user=\"+user+\"&ie=\"+ran, true);
        AJAX.send(null);
        setRefresh(30000);
    }

        
    </script>
    </head>
    <body onload=\"initAJAX()\">
    <div id=\"header\">
        <h1>Page</h1>
    </div>
    <div id=\"nav\">
        <ul>
        <li class=\"first\"><a href=\"index.html\"><em>H</em>ome</a></li>
        <li><a href=\"scripts.html\"><em>S</em>cripts</a></li>
        <li><a href=\"/forum/\"><em>F</em>orum</a></li>
        <li><a href=\"/hireme/\"><em>H</em>ire Me</a></li>
        </ul>
    </div>
    <div id=\"content\"><div class=\"i\">
    <form>
        <?php print \"<input type=\'hidden\' id=\'user\' value=\'$un\'>\"; 
    </form>
    <h2>Online Users</h2>
    <div id=\"ShowUsers\"></div>
    </div></div>
    <div id=\"buffer\"></div>
    <div id=\"footer\"></div>
    </body>
</html>



IP: --   

AJAX (PHP) Active Users Script
ChrisVinecombe
Group: Member
Post Group: Newbie
Posts: 2
Status:
Forgot to add the other script - getactive.php

PHP Code

<?php
    
include "./protected/dbConn.inc";

    
$user $_GET['user'];

    
$mysqlConn mysql_connect("$host"$username$password) or die("Couldn't connect to database");
    
mysql_select_db($database$mysqlConn);

    
$time time() - 60;
    
$timest time();

    
$query_updateUser "update ActiveUsers set timestamp=$timest where username='$user'";
    
mysql_query($query_updateUser$mysqlConn);

    
$query_removeTimedout "delete from ActiveUsers where timestamp < $time";
    
mysql_query($query_removeTimedout$mysqlConn);

    
$query_getActive "select * from ActiveUsers where timestamp > $time";
    
$getActive_result mysql_query($query_getActive$mysqlConn);

    while(
$row mysql_fetch_assoc($getActive_result)) {
    print 
"$row[username]<br/>";
    }
?>


IP: --   

AJAX (PHP) Active Users Script
Will
Group: Member
Post Group: Newbie
Posts: 29
Status:
are you sure your not missing another file?


-----------------------
-Moderator for commx.info
-helped script RADIO.BLOG.CLUB
IP: --   

« Previous    Next »

Threaded Mode | Print  



Jump To :


Users viewing this topic
1 guests, 0 users.

Rollup Banner Stands  cheap car insurance   


AEF web hosting


All times are GMT. The time now is July 29, 2010, 12:01 pm.

  Powered By AEF 1.0.9 © 2007-2009 Electron Inc.Queries: 11  |  Page Created In:0.074