Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
php stats engine
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
joshdr77
Apprentice
Apprentice


Joined: 03 Jan 2003
Posts: 180

PostPosted: Mon Oct 06, 2003 6:56 am    Post subject: php stats engine Reply with quote

hey dudes

can you guys plz help me refine this code, make it more effcient. its seems alittle messy to me, but i dont know enough about php to improve it - thanks dude

Code:

function stats() {

connectdb();


//stuff we look for

$agent = $_SERVER["HTTP_USER_AGENT"];
$ip = $_SERVER["REMOTE_ADDR"];
$referer = parse_url($_SERVER["HTTP_REFERER"]);
$site = $_SERVER["REQUEST_URI"];


if ($ip != '192.168.1.10') {

    //work out current people online
    $query = ("CREATE TABLE IF NOT EXISTS online (
                id int(11) NOT NULL auto_increment,
                agent varchar(255) default NULL,
                host varchar(255) default NULL,
                ip varchar(15) NOT NULL default '',
                port int(11) default NULL,
                referer varchar(255) default NULL,
                site varchar(255) default NULL,
                time varchar(255) NOT NULL default '',
                PRIMARY KEY  (id)
    ) TYPE=MyISAM;");

    mysql_query($query) or die("MySQL Error: " . mysql_error());

    $host = gethostbyaddr($ip);
    $port = $_SERVER["REMOTE_PORT"];
    list($utime, $time) = explode(" ", microtime());

    $query = ("SELECT * FROM online WHERE ip = '$ip';");
    $result = mysql_query($query) or die("MySQL Error: " . mysql_error());


    if (!mysql_num_rows($result)) {
        $query = ("INSERT INTO online (id, agent, host, ip, port, referer, site, time) VALUES(NULL, '$agent', '$host', '$ip', '$port', '$referer[host]', '$site', '$time');");
        mysql_query($query) or die("MySQL Error: " . mysql_error());
    }

    $query = ("DELETE FROM online WHERE time < ($time - 900);");
    mysql_query($query) or die("MySQL Error: " . mysql_error());
    $query = ("UPDATE online SET agent = '$agent', host = '$host', port = '$port', referer = '$referer[host]', site = '$site', time = '$time' WHERE ip = '$ip';");
    mysql_query($query) or die("MySQL Error: " . mysql_error());

    //check to see if ip exists
    $exists = mysql_query("SELECT * FROM stats_ip WHERE ip = '$ip';") or print "dead";
    if (!mysql_num_rows($exists)) {
    //new entry
    $query = ("INSERT INTO stats_ip (ip, hits) VALUES('$ip', '1')");
    mysql_query($query) or die("MySQL Error: " . mysql_error());}
        else {
            //increment of one
            $query = ("UPDATE stats_ip SET hits=(hits + 1) WHERE ip = '$ip'");
            mysql_query($query) or die("MySQL Error: " . mysql_error());
        }

    //check to see if referer exists
    $exists = mysql_query("SELECT * FROM stats_referer WHERE referer = '$referer[host]';") or print "dead";
    if (!mysql_num_rows($exists)) {
    //new entry
    $query = ("INSERT INTO stats_referer (referer, hits) VALUES('$referer[host]', '1')");
    mysql_query($query) or die("MySQL Error: " . mysql_error());}
    else {
        //increment of one
        $query = ("UPDATE stats_referer SET hits=(hits + 1) WHERE referer = '$referer[host]'");
        mysql_query($query) or die("MySQL Error: " . mysql_error());
    }

    //check to see if site exists
    $exists = mysql_query("SELECT * FROM stats_site WHERE site = '$site';") or print "dead";
    if (!mysql_num_rows($exists)) {
    //new entry
    $query = ("INSERT INTO stats_site (site, hits) VALUES('$site', '1')");
    mysql_query($query) or die("MySQL Error: " . mysql_error());}
        else {
            //increment of one
            $query = ("UPDATE stats_site SET hits=(hits + 1) WHERE site = '$site'");
            mysql_query($query) or die("MySQL Error: " . mysql_error());
        }
    }
}
Back to top
View user's profile Send private message
ragnoroc
n00b
n00b


Joined: 08 May 2003
Posts: 49
Location: Columbus, OH

PostPosted: Mon Oct 06, 2003 6:59 am    Post subject: Reply with quote

You might have more luck posting a question like this to a forum like http://www.devshed.com/ .

That site specializes in programming so you would probably get some good feedback from all the programmers there.
Back to top
View user's profile Send private message
joshdr77
Apprentice
Apprentice


Joined: 03 Jan 2003
Posts: 180

PostPosted: Mon Oct 06, 2003 7:37 am    Post subject: Reply with quote

i did yesterday - no one replied :( i guess they look at my code,and cant be bothered helping me - but people here at gentoo are usually more friendly then ne other forum im with :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum