1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Always 1 user online :'( - Script Problem

Discussion in 'PHP' started by khan11, Oct 22, 2007.

  1. #1
    hi guys

    i'm using following script to show online users on my site: http://fonebuzz.net

    
    <?php
    /*
    usersOnline.class.php
    Author: Ilir Fekaj
    Contact: tebrino@hotmail.com
    Last updated: July 28, 2005
    Version: 1.1
    Latest version & info: http://www.sim-php.info
    Support: http://forum.sim-php.info/ (if you find bugs or you need help with installation)
    Demo: http://www.free-midi.org
    
    This very simple class enables you to track number of visitors online in
    an easy and accurate manner. It's free for all purposes, just please don't
    claim you wrote it. If you have any problems, please feel free to contact me.
    Also if you like this script please put link to http://www.sim-php.info. Thanks
    
    Simply paste this code where you wish your users online count to appear:
    
    include_once ("index.php");
    $visitors_online = new usersOnline();
    
    if (count($visitors_online->error) == 0) {
    
    	if ($visitors_online->count_users() == 1) {
    		echo "There is " . $visitors_online->count_users() . " visitor online";
    	}
    	else {
    		echo "There are " . $visitors_online->count_users() . " visitors online";
    	}
    }
    else {
    	echo "<b>Users online class errors:</b><br /><ul>\r\n";
    	for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
    		echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
    	}
    	echo "</ul>\r\n";
    
    }
    
    
    Important: You need to create database connection and select database before creating object! Example connection would look like this:
    
    $host = "localhost"; // your MySQL host i.e. the server on which the database is, usually localhost 
    $user = "attaulla_fbuzz"; // your MySQL username 
    $pass = "0000"; // your MySQL password 
    $db = "attaulla_fbuzz"; // the database to which you're trying to connect to
    
    $conn = mysql_connect("$host", "$user", "$pass") or die ("Unable to connect to database."); 
    mysql_select_db("$db", $conn);
    
    --------------------------------------------
    Table structure (paste this code in PHPMyAdmin or whatever program you use for db management):
    CREATE TABLE `useronline` (
      `id` int(10) NOT NULL auto_increment,
      `ip` varchar(15) NOT NULL default '',
      `timestamp` varchar(15) NOT NULL default '',
      PRIMARY KEY (`id`),
      UNIQUE KEY `id`(`id`)
    ) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;
    
    */
    
    class usersOnline {
    
    	var $timeout = 600;
    	var $count = 0;
    	var $error;
    	var $i = 0;
    	
    	function usersOnline () {
    		$this->timestamp = time();
    		$this->ip = $this->ipCheck();
    		$this->new_user();
    		$this->delete_user();
    		$this->count_users();
    	}
    	
    	function ipCheck() {
    	/*
    	This function will try to find out if user is coming behind proxy server. Why is this important?
    	If you have high traffic web site, it might happen that you receive lot of traffic
    	from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
    	This function tryes to get real IP address.
    	Note that getenv() function doesn't work when PHP is running as ISAPI module
    	*/
    		if (getenv('HTTP_CLIENT_IP')) {
    			$ip = getenv('HTTP_CLIENT_IP');
    		}
    		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
    			$ip = getenv('HTTP_X_FORWARDED_FOR');
    		}
    		elseif (getenv('HTTP_X_FORWARDED')) {
    			$ip = getenv('HTTP_X_FORWARDED');
    		}
    		elseif (getenv('HTTP_FORWARDED_FOR')) {
    			$ip = getenv('HTTP_FORWARDED_FOR');
    		}
    		elseif (getenv('HTTP_FORWARDED')) {
    			$ip = getenv('HTTP_FORWARDED');
    		}
    		else {
    			$ip = $_SERVER['REMOTE_ADDR'];
    		}
    		return $ip;
    	}
    	
    	function new_user() {
    		$insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
    		if (!$insert) {
    			$this->error[$this->i] = "Unable to record new visitor\r\n";			
    			$this->i ++;
    		}
    	}
    	
    	function delete_user() {
    		$delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
    		if (!$delete) {
    			$this->error[$this->i] = "Unable to delete visitors";
    			$this->i ++;
    		}
    	}
    	
    	function count_users() {
    		if (count($this->error) == 0) {
    			$count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
    			return $count;
    		}
    	}
    
    }
    
    ?>
    
    PHP:
    check at bottom of my site, it always shows 1 user, i tried a lot. I also asked few of my friends to visit my site at same time, still shows 1 visitor online.

    Please tell me where is the problem?

    More:
    I added the bottom file as include
    Like:

    Myindex.php

    i added footer.php

    in footer.php

    the include of that script..



    any suggestion.??
     
    khan11, Oct 22, 2007 IP
    Professional Dude likes this.
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    It's probably as simple as when you look at the script, there is one person online - namely YOU ... or do you mean the number never increases ?
     
    krakjoe, Oct 22, 2007 IP
  3. ForgottenCreature

    ForgottenCreature Notable Member

    Messages:
    7,436
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    260
    #3
    Does it ever increase? Open up another browser and see if it increase.
     
    ForgottenCreature, Oct 22, 2007 IP
  4. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #4
    no it doesn't

    i almost tried to access site with 4 users but the number never increases.

    script file = online.php
    footer file = footer.php (included online.php here)
    main file = index.php (included footer.php here)
     
    khan11, Oct 22, 2007 IP
  5. robca

    robca Well-Known Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #5
    if i read good, he says several friends visit the site at the same time but it doesn't count...
     
    robca, Oct 22, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Have you checked that the users are being added to the useronline table. I would suggest checking the contents of the table when you have more than one user logged in.

    Brew
     
    Brewster, Oct 22, 2007 IP
  7. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #7
    well its not sessioned script. I'm just counting all users who are currently online:

    I've changed the script now but still the same error:

    new script
    
    <?
    /**
     *
     * TG WHO'S ONLINE
     * Copyright 2005 - 2006 (c) TOXIC GOBLIN
     * http://www.toxicgoblin.com
     * 
     */
     
    //Optional Database Connection Information
    //**Uncomment the following 2 lines and edit the values if you do not already have an active database connection**
    //
    $db = mysql_connect("localhost", "attaulla_online", "0000") or die("Could not connect");
    mysql_select_db("attaulla_online");
    
    //Fetch Time
    $timestamp = time();
    $timeout = $timestamp - 900;
    
    //Insert User
    $insert = mysql_query("INSERT INTO TG_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!");
    //Delete Users
    $delete = mysql_query("DELETE FROM TG_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!");
    //Fetch Users Online
    $result = mysql_query("SELECT DISTINCT ip FROM TG_whos_online") or die("Error in who's online result query!");
    $users = mysql_num_rows($result);
    
    //Show Who's Online
    if($users == 1) {
    print("$users Visitor Online \n");
    } else {
    print("$users Visitors Online \n");
    }
    ?>
    
    PHP:
    included this to my footer file, but still the same.

    here is the table created:
    
    CREATE TABLE `TG_whos_online` (
      `id` bigint(20) NOT NULL auto_increment,
      `timestamp` int(15) NOT NULL default '0',
      `ip` varchar(40) NOT NULL default '',
      `file` varchar(100) NOT NULL default '',
      PRIMARY KEY  (`id`),
      KEY `ip` (`ip`),
      KEY `file` (`file`),
      KEY `timestamp` (`timestamp`)
    ) TYPE=MyISAM;
    
    Code (markup):
     
    khan11, Oct 22, 2007 IP
  8. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Have you checked the database to make sure that the entries are being added properly? Make sure there is more than one entry in the table.
     
    phpl33t, Oct 22, 2007 IP
  9. phpl33t

    phpl33t Banned

    Messages:
    456
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Add this to your .htaccess and then see what error(s) might be reported:


     
    phpl33t, Oct 22, 2007 IP
  10. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #10
    well no error i found in the script.

    Yes, you are right the entry in mysql is not entered correctly..

    What the problem i see is the "IP" if i enter some ip myself then it shows 2 users but that is totally manual..

    The problem with it is that it doesn't add new ip in database to show current online users..
     
    khan11, Oct 22, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    After all of your mysql_query() statements, place: OR die(mysql_error());, and see if that gives us a hint.

    
    mysql_query("SQL string here") OR die(mysql_error());
    
    PHP:
     
    nico_swd, Oct 22, 2007 IP
  12. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #12
    well i have OR die(mysql_error()); after each mysql query..

    Near me, mysql is working fine. I think their is something wrong with IP fetcher.. Whenever someone visits the script doesn't update the ip..
     
    khan11, Oct 22, 2007 IP
  13. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #13
    guys !! i have tried almost the third script which runs as same showing 1 user online all the time but still the same problem.

    This means the script has nothing wrong but my site. I think something's gone wrong with my site or included files.. :(

    messing a lot but no solution..

    one more thing: is .htaccess has any effect or not?

    coz i changed the .htaccess rewrite so my site moves from www.fonebuzz.net to fonebuzz.net..

    any ideas???
     
    khan11, Oct 22, 2007 IP