display users online

  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Functional Code Updated below...

Code written in classes, see my followup post hooch, should be copy paste... tested to :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

I am trying to fix up your code.
There was some obvious errors that even I could fix...yay me.
But I am getting an error that I cannot fix.
1st here's the updated code
PHP Code: [ Select ]
<?php
 
// All you need to know  Still requires some work on the "returns" and the sql queries needed.
 
// But then again some work should be done by yourself too...  
 
// good luck.
 
 
 
function handle_session($session_iid){  
 
     
 
    // Set stuff to work with //  
 
    $raw_time = mktime();  
 
    $timestamp = $raw_time['hour'].$raw_time['minute'].$raw_time['second'];  
 
    $cur_day = date(d);  
 
    $user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];  
 
     
 
// Old session clean up so we keep valid data //
 
    $sql = 'select * from session';  
 
    $result = mysql_query($sql);  
 
    while($row = mysql_fetch_array($result)){
 
 
 
        if($row['session_di'] == $cur_day){  
 
            $val_session_time = (($row['session_ti'] - $timestamp) < 1500) ? true :  
 
        }else{  
 
            $am = "600000" - $row['session_ti'];                          
 
            $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
 
        }  
 
         
 
    // Found old session! Delete the B#$ch //          
 
        if($val_session_time == false){  
 
        $sql = 'delete from session where session_id ='.$row['session_id'];
 
        if($result = mysql_query($sql){
 
        next;
 
            }
 
        }  
 
    }  
 
    // Check Current Session //
 
    if(isset($session_iid) ){  
 
           // Find the session in the table
 
           $sql = 'select * from session where session_id ='.$session_iid;  
 
           $result = mysql_query($sql);  
 
           $row = mysql_fetch_array($result);  
 
           
 
        // validate session age //          
 
           if($row['session_di'] == $cur_day){  
 
                   $val_session_time = (($row['session_ti'] - $timestamp)) < 1500) ? true :  
 
           }else{  
 
                   $am = "600000" - $row['session_ti'];  
 
                   $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
 
           }  
 
                     
 
           if($val_session_time == true){    
 
               $sql = 'update session set session_ti = '.$timestamp.', session_di = '.$cur_day.' where session_id = '.$session_iid;
 
               mysql_query($sql);
 
               return($session_iid);  
 
           }else{  
 
          // Remove the session and create a new one as guest. using these sql statements //
 
             $sql = 'delete from session where session_id ='.$session_iid;
 
             $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
 
         $sql = 'select session_id from session where session_ip = '.$user_ip;              
 
 
 
 
 
             return ($row['session_id']);  
 
           }  
 
     }else{  
 
         // We have a new user  (guest) so create a new session.//  
 
         $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
 
     $sql = 'select session_id from session where session_ip = '.$user_ip;              
 
         return($row['session_id']);  
 
    }  
 
}  
 
 
 
Function get_sessions(){
 
    $sql = 'count session_id as total,
 
               count distinct($session_us) as registered
 
               from session
 
               where session_id is not null';
 
    $result = mysql_query($sql);
 
    $row = mysql_fetch_array($result);
 
     
 
    $guests = $row['total'] - $row['registered'];
 
    $online['guests'] = $guests;
 
    $online['registered'] = $row['registered'];
 
    return($online);
 
}
 
 
 
 
 
 
 
 
 
// Start of the page / website  //
 
session_start();  
 
$_SESSION['session_id'] = handle_session($_SESSION['session_id']);
 
$who_online = get_sessions();
 
?>
 
 
 
<html>
 
  <table width="40">
 
    <tr>
 
        <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>
 
    </tr>
 
    <tr>
 
        <td>Registered</td><td> :</td><td><?php echo "$who_online['registered']"; ?></td>
 
        </tr>
 
  </table>
 
</html>
 
 
  1. <?php
  2.  
  3. // All you need to know  Still requires some work on the "returns" and the sql queries needed.
  4.  
  5. // But then again some work should be done by yourself too...  
  6.  
  7. // good luck.
  8.  
  9.  
  10.  
  11. function handle_session($session_iid){  
  12.  
  13.      
  14.  
  15.     // Set stuff to work with //  
  16.  
  17.     $raw_time = mktime();  
  18.  
  19.     $timestamp = $raw_time['hour'].$raw_time['minute'].$raw_time['second'];  
  20.  
  21.     $cur_day = date(d);  
  22.  
  23.     $user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];  
  24.  
  25.      
  26.  
  27. // Old session clean up so we keep valid data //
  28.  
  29.     $sql = 'select * from session';  
  30.  
  31.     $result = mysql_query($sql);  
  32.  
  33.     while($row = mysql_fetch_array($result)){
  34.  
  35.  
  36.  
  37.         if($row['session_di'] == $cur_day){  
  38.  
  39.             $val_session_time = (($row['session_ti'] - $timestamp) < 1500) ? true :  
  40.  
  41.         }else{  
  42.  
  43.             $am = "600000" - $row['session_ti'];                          
  44.  
  45.             $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
  46.  
  47.         }  
  48.  
  49.          
  50.  
  51.     // Found old session! Delete the B#$ch //          
  52.  
  53.         if($val_session_time == false){  
  54.  
  55.         $sql = 'delete from session where session_id ='.$row['session_id'];
  56.  
  57.         if($result = mysql_query($sql){
  58.  
  59.         next;
  60.  
  61.             }
  62.  
  63.         }  
  64.  
  65.     }  
  66.  
  67.     // Check Current Session //
  68.  
  69.     if(isset($session_iid) ){  
  70.  
  71.            // Find the session in the table
  72.  
  73.            $sql = 'select * from session where session_id ='.$session_iid;  
  74.  
  75.            $result = mysql_query($sql);  
  76.  
  77.            $row = mysql_fetch_array($result);  
  78.  
  79.            
  80.  
  81.         // validate session age //          
  82.  
  83.            if($row['session_di'] == $cur_day){  
  84.  
  85.                    $val_session_time = (($row['session_ti'] - $timestamp)) < 1500) ? true :  
  86.  
  87.            }else{  
  88.  
  89.                    $am = "600000" - $row['session_ti'];  
  90.  
  91.                    $val_session_time = (($am + $timestamp) < 1500) ?  true : false;            
  92.  
  93.            }  
  94.  
  95.                      
  96.  
  97.            if($val_session_time == true){    
  98.  
  99.                $sql = 'update session set session_ti = '.$timestamp.', session_di = '.$cur_day.' where session_id = '.$session_iid;
  100.  
  101.                mysql_query($sql);
  102.  
  103.                return($session_iid);  
  104.  
  105.            }else{  
  106.  
  107.           // Remove the session and create a new one as guest. using these sql statements //
  108.  
  109.              $sql = 'delete from session where session_id ='.$session_iid;
  110.  
  111.              $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
  112.  
  113.          $sql = 'select session_id from session where session_ip = '.$user_ip;              
  114.  
  115.  
  116.  
  117.  
  118.  
  119.              return ($row['session_id']);  
  120.  
  121.            }  
  122.  
  123.      }else{  
  124.  
  125.          // We have a new user  (guest) so create a new session.//  
  126.  
  127.          $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$user_ip.'","'.$timestamp.'","'.$cur_day.'")';
  128.  
  129.      $sql = 'select session_id from session where session_ip = '.$user_ip;              
  130.  
  131.          return($row['session_id']);  
  132.  
  133.     }  
  134.  
  135. }  
  136.  
  137.  
  138.  
  139. Function get_sessions(){
  140.  
  141.     $sql = 'count session_id as total,
  142.  
  143.                count distinct($session_us) as registered
  144.  
  145.                from session
  146.  
  147.                where session_id is not null';
  148.  
  149.     $result = mysql_query($sql);
  150.  
  151.     $row = mysql_fetch_array($result);
  152.  
  153.      
  154.  
  155.     $guests = $row['total'] - $row['registered'];
  156.  
  157.     $online['guests'] = $guests;
  158.  
  159.     $online['registered'] = $row['registered'];
  160.  
  161.     return($online);
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // Start of the page / website  //
  174.  
  175. session_start();  
  176.  
  177. $_SESSION['session_id'] = handle_session($_SESSION['session_id']);
  178.  
  179. $who_online = get_sessions();
  180.  
  181. ?>
  182.  
  183.  
  184.  
  185. <html>
  186.  
  187.   <table width="40">
  188.  
  189.     <tr>
  190.  
  191.         <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>
  192.  
  193.     </tr>
  194.  
  195.     <tr>
  196.  
  197.         <td>Registered</td><td> :</td><td><?php echo "$who_online['registered']"; ?></td>
  198.  
  199.         </tr>
  200.  
  201.   </table>
  202.  
  203. </html>
  204.  
  205.  

Now the error...
Parse error: parse error, unexpected '}' in /home/online_OZZU.php on line 22

Line 22 is the 1st
}else{
command

Here's hoping I can get this working.
Thanks for all the input may
  • meman
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3432
  • Loc: London Town , Apples and pears and all that crap

Post 3+ Months Ago

It looks like the line before }else{ ends with a : when it should end with a ;

The same it true for the next }else{ statment.
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

meman wrote:
It looks like the line before }else{ ends with a : when it should end with a ;

The same it true for the next }else{ statment.



Parse error: parse error, unexpected ';' in /home/online_OZZU.php on line 22

?
  • meman
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3432
  • Loc: London Town , Apples and pears and all that crap

Post 3+ Months Ago

hmmm.. I don't know.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Functional code below....
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

may wrote:
first of all, not obvious errors, like i stated not complete code. :)


I was poking fun at myself...since I have no clue when it comes to php.
No offense to you and your kindness in helping me.

Now back to business.. I made it to the html part. Here's my latest error
(I did fix a cpl more along the way)
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/online_OZZU.php on line 98
Line 98 is...
Code: [ Select ]
    <td>guests</td><td> :</td><td><?php echo "$who_online['guests']"; ?></td>

Again thank you for any help..Hooch
P.S I'll post the finished code once I (well I hope to) finish.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Admin, can you remove all my "blank" posts? thx..

greets may,
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Finished the code by adding a mysql class and pouring the session stuff in a class also, before more people start "fixing" incomplete code :P (not a personal note btw)

Hopes this solves your problem hooch, if you have any questions then just request tutorials on rabiddog its tutorial site, found in its "nick" ;)

but on the side, I see loads of questions from your part.. what are you creating anyways? Is it a school project or something?


PHP Code: [ Select ]
 
/**/
 
/*
 
 *   Session handling classes written for dear Hooch
 
 *   1 Mysql connection class (also see <!-- w --><span class="postlink">http://www.rabiddog.co.za</span><!-- w -->)      
 
 *   1 Session_handling class
 
 */
 
 
 
/* create table session(
 
 *   session_id INT unsigned not null auto_increment primary key,
 
 *   session_ip varchar(15) not null,
 
 *   session_ti INT(6) not null,
 
 *   session_di INT(2) not null,
 
 *   session_us varchar(30) null);
 
 */
 
 
 
class mysql{
 
   
 
   var $username;
 
   var $password;
 
   var $hostname;
 
   var $database;
 
   var $error;
 
   
 
   //Get vars from user (somewhere) or just here (savest)//
 
   function set_vars($host, $user, $pass, $db){
 
      $this->hostname = (!empty($host)) ? $host : "127.0.0.1";
 
      $this->username = (!empty($user)) ? $user : "someuser";
 
      $this->password = (!empty($pass)) ? $pass : "somepassword";
 
      $this->database = (!empty($db)) ? $db : "somedatabase";
 
   }
 
   
 
   function unset_vars(){
 
      unset($this->hostname);
 
      unset($this->username);
 
      unset($this->password);
 
      unset($this->database);
 
   }
 
   
 
   //Initialise the database connection//
 
   function init_con(){
 
      if($con_str = mysql_connect($this->hostname, $this->username, $this->password) ){
 
         if(mysql_select_db($this->database, $con_str)){
 
            return($con_str);
 
         }else{
 
            return(false);
 
         }
 
      }else{
 
         return(false);
 
      }
 
   }
 
   
 
   function close_con($con_str){
 
      if(!empty($con_str) ){
 
         mysql_close($con_str);
 
      }
 
   }
 
   
 
   function query($sql){
 
      $con_str = $this->init_con();
 
      if(is_resource($con_str)){
 
         $result = mysql_query($sql);
 
         if(is_resource($result)){
 
            $this->close_con($con_str);
 
            return($result);
 
         }else{
 
            $this->close_con($con_str);
 
              return(false);
 
         }
 
      }else{
 
         return(false);
 
      }  
 
   }
 
}
 
 
 
class Session{
 
   
 
   var $timestamp;
 
   var $cur_day;
 
   var $user_ip;
 
   var $max_time;
 
   
 
   function get_data(){
 
         
 
         $this->timestamp = date('His');  
 
         $this->cur_day = date('d');  
 
         $this->user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];
 
         $this->max_time = '500'; //5 mins MMSS/
 
   }
 
   
 
   function val_session($day, $time){
 
     
 
      if($day == $this->cur_day){  
 
            $val_session_time = (($this->timestamp - $time) < $this->max_time) ? true : false;
 
        }else{  
 
            $am = "600000" - $time;                          
 
            $val_session_time = (($am + $this->timestamp) < $this->max_time) ?  true : false;            
 
        }
 
        return($val_session_time);
 
     }
 
     
 
     function create_session(){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$this->user_ip.'","'.$this->timestamp.'","'.$this->cur_day.'")';
 
      $oMYSQL->query($sql);
 
         
 
      $sql = 'select session_id from session where session_ip = "'.$this->user_ip.'"';
 
      $result = $oMYSQL->query($sql);
 
      $row = mysql_fetch_array($result);
 
      return($row['session_id']);
 
     }
 
     
 
     function update_session($session_id){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      if(!empty($session_id)){
 
         $sql = 'update session
 
               set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'"
 
               where session_id = "'.$session_id.'"';
 
         if($oMYSQL->query($sql)){
 
            return(true);
 
         }else{
 
            return(false);
 
         }
 
      }
 
     }
 
     
 
     function delete_session($session_id){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
      if(!empty($session_id)){
 
         $sql = 'delete from session where session_id = '.$session_id;
 
         if($oMYSQL->query($sql)){
 
            return(true);
 
         }else{
 
            return(false);
 
         }
 
      }
 
     }
 
     
 
     function clean_up_db(){
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
     
 
      $sql = 'select * from session';
 
     
 
      $result = $oMYSQL->query($sql);
 
      while($row = mysql_fetch_array($result)){
 
         
 
         $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
         
 
         if($validate == false){
 
            if($this->delete_session($row['session_id'])){
 
               unset($validate);
 
            }else{
 
               // This is not nice.. but hey <img src="./images/smilies/icon_wink.gif" alt=";)" title="Wink" /> //
 
               unset($validate);
 
            }
 
         }else{
 
            unset($validate);
 
         }
 
      }
 
     }     
 
     
 
     function handle_session(){
 
      // Get the data //
 
      $this->get_data();
 
     
 
      // First cleanup the database //
 
      $this->clean_up_db();
 
     
 
      // Start the session prepare mysql //
 
      $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
     
 
      if(isset($_SESSION['session_id']) ){
 
         $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
 
         $result = $oMYSQL->query($sql);
 
         $row = mysql_fetch_array($result);
 
         $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
         
 
         if($validate == true){
 
            $this->update_session($_SESSION['session_id']);
 
         }else{
 
            // Might be used to strip the "login" then extend this part//
 
            // Now the "guest" will only get a new id, usually someone //
 
            // Is altered from "reg" status to "guest" status          //
 
            $this->delete_session($_SESSION['session_id']);
 
            $_SESSION['session_id'] = $this->create_session();
 
         }
 
      }else{
 
         $_SESSION['session_id'] = $this->create_session();
 
      }  
 
      }
 
     
 
     
 
      Function get_sessions(){
 
         $oMYSQL = new mysql;
 
      $oMYSQL->set_vars("","","","");
 
         $sql = 'select count(session_us) as registered,
 
            count(session_id) as total
 
            from session';
 
         if($result = $oMYSQL->query($sql)){
 
            $row = mysql_fetch_array($result);
 
      }
 
     
 
      if(empty($row['registered']) ){
 
         $guests = $row['total'];
 
      }else{
 
         $guests = $row['total'] - $row['registered'];
 
      }
 
         $online['guests'] = $guests;
 
         $online['registered'] = $row['registered'];
 
         
 
         return($online);
 
   }
 
}
 
   
 
session_start();    
 
$oSESSION = new Session;
 
$oSESSION->handle_session();
 
$online = $oSESSION->get_sessions();   
 
 
 
print ('session_id = '.$_SESSION['session_id']);
 
     
 
print_r('
 
 
 
<html>
 
 <table width="40">
 
   <tr>
 
       <td>guests</td><td> :</td><td>'.$online['guests'].'</td>
 
   </tr>
 
   <tr>
 
       <td>Registered</td><td> :</td><td>'.$online['registered'].'</td>
 
       </tr>
 
 </table>
 
</html>
 
 
 
');
 
 
 
 
  1.  
  2. /**/
  3.  
  4. /*
  5.  
  6.  *   Session handling classes written for dear Hooch
  7.  
  8.  *   1 Mysql connection class (also see <!-- w --><span class="postlink">http://www.rabiddog.co.za</span><!-- w -->)      
  9.  
  10.  *   1 Session_handling class
  11.  
  12.  */
  13.  
  14.  
  15.  
  16. /* create table session(
  17.  
  18.  *   session_id INT unsigned not null auto_increment primary key,
  19.  
  20.  *   session_ip varchar(15) not null,
  21.  
  22.  *   session_ti INT(6) not null,
  23.  
  24.  *   session_di INT(2) not null,
  25.  
  26.  *   session_us varchar(30) null);
  27.  
  28.  */
  29.  
  30.  
  31.  
  32. class mysql{
  33.  
  34.    
  35.  
  36.    var $username;
  37.  
  38.    var $password;
  39.  
  40.    var $hostname;
  41.  
  42.    var $database;
  43.  
  44.    var $error;
  45.  
  46.    
  47.  
  48.    //Get vars from user (somewhere) or just here (savest)//
  49.  
  50.    function set_vars($host, $user, $pass, $db){
  51.  
  52.       $this->hostname = (!empty($host)) ? $host : "127.0.0.1";
  53.  
  54.       $this->username = (!empty($user)) ? $user : "someuser";
  55.  
  56.       $this->password = (!empty($pass)) ? $pass : "somepassword";
  57.  
  58.       $this->database = (!empty($db)) ? $db : "somedatabase";
  59.  
  60.    }
  61.  
  62.    
  63.  
  64.    function unset_vars(){
  65.  
  66.       unset($this->hostname);
  67.  
  68.       unset($this->username);
  69.  
  70.       unset($this->password);
  71.  
  72.       unset($this->database);
  73.  
  74.    }
  75.  
  76.    
  77.  
  78.    //Initialise the database connection//
  79.  
  80.    function init_con(){
  81.  
  82.       if($con_str = mysql_connect($this->hostname, $this->username, $this->password) ){
  83.  
  84.          if(mysql_select_db($this->database, $con_str)){
  85.  
  86.             return($con_str);
  87.  
  88.          }else{
  89.  
  90.             return(false);
  91.  
  92.          }
  93.  
  94.       }else{
  95.  
  96.          return(false);
  97.  
  98.       }
  99.  
  100.    }
  101.  
  102.    
  103.  
  104.    function close_con($con_str){
  105.  
  106.       if(!empty($con_str) ){
  107.  
  108.          mysql_close($con_str);
  109.  
  110.       }
  111.  
  112.    }
  113.  
  114.    
  115.  
  116.    function query($sql){
  117.  
  118.       $con_str = $this->init_con();
  119.  
  120.       if(is_resource($con_str)){
  121.  
  122.          $result = mysql_query($sql);
  123.  
  124.          if(is_resource($result)){
  125.  
  126.             $this->close_con($con_str);
  127.  
  128.             return($result);
  129.  
  130.          }else{
  131.  
  132.             $this->close_con($con_str);
  133.  
  134.               return(false);
  135.  
  136.          }
  137.  
  138.       }else{
  139.  
  140.          return(false);
  141.  
  142.       }  
  143.  
  144.    }
  145.  
  146. }
  147.  
  148.  
  149.  
  150. class Session{
  151.  
  152.    
  153.  
  154.    var $timestamp;
  155.  
  156.    var $cur_day;
  157.  
  158.    var $user_ip;
  159.  
  160.    var $max_time;
  161.  
  162.    
  163.  
  164.    function get_data(){
  165.  
  166.          
  167.  
  168.          $this->timestamp = date('His');  
  169.  
  170.          $this->cur_day = date('d');  
  171.  
  172.          $this->user_ip = isset($_SESSION['user_ip']) ? $_SESSION['user_ip'] : $_SERVER['REMOTE_ADDR'];
  173.  
  174.          $this->max_time = '500'; //5 mins MMSS/
  175.  
  176.    }
  177.  
  178.    
  179.  
  180.    function val_session($day, $time){
  181.  
  182.      
  183.  
  184.       if($day == $this->cur_day){  
  185.  
  186.             $val_session_time = (($this->timestamp - $time) < $this->max_time) ? true : false;
  187.  
  188.         }else{  
  189.  
  190.             $am = "600000" - $time;                          
  191.  
  192.             $val_session_time = (($am + $this->timestamp) < $this->max_time) ?  true : false;            
  193.  
  194.         }
  195.  
  196.         return($val_session_time);
  197.  
  198.      }
  199.  
  200.      
  201.  
  202.      function create_session(){
  203.  
  204.       $oMYSQL = new mysql;
  205.  
  206.       $oMYSQL->set_vars("","","","");
  207.  
  208.       $sql = 'insert into session(session_ip, session_ti, session_di) values("'.$this->user_ip.'","'.$this->timestamp.'","'.$this->cur_day.'")';
  209.  
  210.       $oMYSQL->query($sql);
  211.  
  212.          
  213.  
  214.       $sql = 'select session_id from session where session_ip = "'.$this->user_ip.'"';
  215.  
  216.       $result = $oMYSQL->query($sql);
  217.  
  218.       $row = mysql_fetch_array($result);
  219.  
  220.       return($row['session_id']);
  221.  
  222.      }
  223.  
  224.      
  225.  
  226.      function update_session($session_id){
  227.  
  228.       $oMYSQL = new mysql;
  229.  
  230.       $oMYSQL->set_vars("","","","");
  231.  
  232.       if(!empty($session_id)){
  233.  
  234.          $sql = 'update session
  235.  
  236.                set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'"
  237.  
  238.                where session_id = "'.$session_id.'"';
  239.  
  240.          if($oMYSQL->query($sql)){
  241.  
  242.             return(true);
  243.  
  244.          }else{
  245.  
  246.             return(false);
  247.  
  248.          }
  249.  
  250.       }
  251.  
  252.      }
  253.  
  254.      
  255.  
  256.      function delete_session($session_id){
  257.  
  258.       $oMYSQL = new mysql;
  259.  
  260.       $oMYSQL->set_vars("","","","");
  261.  
  262.       if(!empty($session_id)){
  263.  
  264.          $sql = 'delete from session where session_id = '.$session_id;
  265.  
  266.          if($oMYSQL->query($sql)){
  267.  
  268.             return(true);
  269.  
  270.          }else{
  271.  
  272.             return(false);
  273.  
  274.          }
  275.  
  276.       }
  277.  
  278.      }
  279.  
  280.      
  281.  
  282.      function clean_up_db(){
  283.  
  284.       $oMYSQL = new mysql;
  285.  
  286.       $oMYSQL->set_vars("","","","");
  287.  
  288.      
  289.  
  290.       $sql = 'select * from session';
  291.  
  292.      
  293.  
  294.       $result = $oMYSQL->query($sql);
  295.  
  296.       while($row = mysql_fetch_array($result)){
  297.  
  298.          
  299.  
  300.          $validate = $this->val_session($row['session_di'], $row['session_ti']);
  301.  
  302.          
  303.  
  304.          if($validate == false){
  305.  
  306.             if($this->delete_session($row['session_id'])){
  307.  
  308.                unset($validate);
  309.  
  310.             }else{
  311.  
  312.                // This is not nice.. but hey <img src="./images/smilies/icon_wink.gif" alt=";)" title="Wink" /> //
  313.  
  314.                unset($validate);
  315.  
  316.             }
  317.  
  318.          }else{
  319.  
  320.             unset($validate);
  321.  
  322.          }
  323.  
  324.       }
  325.  
  326.      }     
  327.  
  328.      
  329.  
  330.      function handle_session(){
  331.  
  332.       // Get the data //
  333.  
  334.       $this->get_data();
  335.  
  336.      
  337.  
  338.       // First cleanup the database //
  339.  
  340.       $this->clean_up_db();
  341.  
  342.      
  343.  
  344.       // Start the session prepare mysql //
  345.  
  346.       $oMYSQL = new mysql;
  347.  
  348.       $oMYSQL->set_vars("","","","");
  349.  
  350.      
  351.  
  352.       if(isset($_SESSION['session_id']) ){
  353.  
  354.          $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
  355.  
  356.          $result = $oMYSQL->query($sql);
  357.  
  358.          $row = mysql_fetch_array($result);
  359.  
  360.          $validate = $this->val_session($row['session_di'], $row['session_ti']);
  361.  
  362.          
  363.  
  364.          if($validate == true){
  365.  
  366.             $this->update_session($_SESSION['session_id']);
  367.  
  368.          }else{
  369.  
  370.             // Might be used to strip the "login" then extend this part//
  371.  
  372.             // Now the "guest" will only get a new id, usually someone //
  373.  
  374.             // Is altered from "reg" status to "guest" status          //
  375.  
  376.             $this->delete_session($_SESSION['session_id']);
  377.  
  378.             $_SESSION['session_id'] = $this->create_session();
  379.  
  380.          }
  381.  
  382.       }else{
  383.  
  384.          $_SESSION['session_id'] = $this->create_session();
  385.  
  386.       }  
  387.  
  388.       }
  389.  
  390.      
  391.  
  392.      
  393.  
  394.       Function get_sessions(){
  395.  
  396.          $oMYSQL = new mysql;
  397.  
  398.       $oMYSQL->set_vars("","","","");
  399.  
  400.          $sql = 'select count(session_us) as registered,
  401.  
  402.             count(session_id) as total
  403.  
  404.             from session';
  405.  
  406.          if($result = $oMYSQL->query($sql)){
  407.  
  408.             $row = mysql_fetch_array($result);
  409.  
  410.       }
  411.  
  412.      
  413.  
  414.       if(empty($row['registered']) ){
  415.  
  416.          $guests = $row['total'];
  417.  
  418.       }else{
  419.  
  420.          $guests = $row['total'] - $row['registered'];
  421.  
  422.       }
  423.  
  424.          $online['guests'] = $guests;
  425.  
  426.          $online['registered'] = $row['registered'];
  427.  
  428.          
  429.  
  430.          return($online);
  431.  
  432.    }
  433.  
  434. }
  435.  
  436.    
  437.  
  438. session_start();    
  439.  
  440. $oSESSION = new Session;
  441.  
  442. $oSESSION->handle_session();
  443.  
  444. $online = $oSESSION->get_sessions();   
  445.  
  446.  
  447.  
  448. print ('session_id = '.$_SESSION['session_id']);
  449.  
  450.      
  451.  
  452. print_r('
  453.  
  454.  
  455.  
  456. <html>
  457.  
  458.  <table width="40">
  459.  
  460.    <tr>
  461.  
  462.        <td>guests</td><td> :</td><td>'.$online['guests'].'</td>
  463.  
  464.    </tr>
  465.  
  466.    <tr>
  467.  
  468.        <td>Registered</td><td> :</td><td>'.$online['registered'].'</td>
  469.  
  470.        </tr>
  471.  
  472.  </table>
  473.  
  474. </html>
  475.  
  476.  
  477.  
  478. ');
  479.  
  480.  
  481.  
  482.  


I have tested this code, if you want to include the "registered" you should add something like "if isset($_SESSION['username']){ then update with session_us }. Now only the "guests" or viewers are functional. Also reduced the session time to 5 mins, this because 5min of idle time is a long while ;)

ow well, cu and good luck :D

good luck m8

ps. DO add valid mysql connection info in the top function (someuser, somepass, somedatabase, 127.0.0.1)
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

xN3uRoNx wrote:
Hey, this is very simple, and i'm here to help! :D

1st off, add this into your members table, or users (whatever you use.)

Online - Varchar(255) default: No



i dont think its wise to handle session information in the users table, first how are you going to count all the "guests" on you page. Next there is allot more information about "a" session you might want to store during that session, say what page one is browsing at the moment, say how long he was browsing, say what actions he did (secure pages / logging) etc. Dont want to store all this info in your users table ;)

you can use all this info to make your page more attractive, or more accesible (100 users left on that page doint this... error?). Session data might be more usefull that one might think in a glace ;) or just flush it like the above example if you just would like to see the "current" users browsing :)


ow ps. if you only like to know if someone is online, then why would you need 255 chars to tell that? INT(1) would be enough... ;)
You can simply validate a bool value like :

0 being False
1 being true

will reduce allot of overhead on you tables..

-Greets
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Yay for may!

I'll have to give he code a try as soon as I get home tonight.
Thank you and everyone else who has been helping me here.

You should put your name at the top of ths script, you deserve credit.
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

HoocH wrote:
You should put your name at the top of ths script, you deserve credit.


just create a link to http://www.rabiddog.co.za he helped me make this possible for you

and on reply to you question how to show the "online users"

update this function and insert the "additional" username in session_us. You shouldnt do it in the "create_session" because the session is allready started before someone logs in. If you use a portal, there is bound to be some user object wich holds the userdata, validate if it`s set if so, update the "session_us" column.

PHP Code: [ Select ]
 
// Additional code has been commented out! //
 
 
 
function update_session($session_id /*, $some_user_id*/ ){
 
         $oMYSQL = new mysql;
 
         $oMYSQL->set_vars("","","","");
 
 
 
         if(!empty($session_id)){
 
             $sql = 'update session
 
                   set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'" /*, session_us = "'.$some_user_id.'" */;
 
                   where session_id = "'.$session_id.'"';
 
             if($oMYSQL->query($sql)){
 
                 return(true);
 
             }else{
 
                 return(false);
 
             }
 
         }
 
     }
 
 
  1.  
  2. // Additional code has been commented out! //
  3.  
  4.  
  5.  
  6. function update_session($session_id /*, $some_user_id*/ ){
  7.  
  8.          $oMYSQL = new mysql;
  9.  
  10.          $oMYSQL->set_vars("","","","");
  11.  
  12.  
  13.  
  14.          if(!empty($session_id)){
  15.  
  16.              $sql = 'update session
  17.  
  18.                    set session_ti = "'.$this->timestamp.'", session_di = "'.$this->cur_day.'" /*, session_us = "'.$some_user_id.'" */;
  19.  
  20.                    where session_id = "'.$session_id.'"';
  21.  
  22.              if($oMYSQL->query($sql)){
  23.  
  24.                  return(true);
  25.  
  26.              }else{
  27.  
  28.                  return(false);
  29.  
  30.              }
  31.  
  32.          }
  33.  
  34.      }
  35.  
  36.  


and

PHP Code: [ Select ]
 
function handle_session(  /*$some_user_id */){
 
         // Get the data //
 
         $this->get_data();
 
         
 
         // First cleanup the database //
 
         $this->clean_up_db();
 
         
 
         // Start the session prepare mysql //
 
         $oMYSQL = new mysql;
 
         $oMYSQL->set_vars("","","","");
 
         
 
         if(isset($_SESSION['session_id']) ){
 
             $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
 
             $result = $oMYSQL->query($sql);
 
             $row = mysql_fetch_array($result);
 
             $validate = $this->val_session($row['session_di'], $row['session_ti']);
 
             
 
             if($validate == true){
 
                 $this->update_session($_SESSION['session_id'] /* , $some_user_obj */);
 
             }else{
 
                 // Might be used to strip the "login" then extend this part//
 
                 // Now the "guest" will only get a new id, usually someone //
 
                 // Is altered from "reg" status to "guest" status          //
 
                 $this->delete_session($_SESSION['session_id']);
 
                 $_SESSION['session_id'] = $this->create_session();
 
             }
 
         }else{
 
             $_SESSION['session_id'] = $this->create_session();
 
         }  
 
      }  
 
 
  1.  
  2. function handle_session(  /*$some_user_id */){
  3.  
  4.          // Get the data //
  5.  
  6.          $this->get_data();
  7.  
  8.          
  9.  
  10.          // First cleanup the database //
  11.  
  12.          $this->clean_up_db();
  13.  
  14.          
  15.  
  16.          // Start the session prepare mysql //
  17.  
  18.          $oMYSQL = new mysql;
  19.  
  20.          $oMYSQL->set_vars("","","","");
  21.  
  22.          
  23.  
  24.          if(isset($_SESSION['session_id']) ){
  25.  
  26.              $sql = 'select * from session where session_id = "'.$_SESSION['session_id'].'"';
  27.  
  28.              $result = $oMYSQL->query($sql);
  29.  
  30.              $row = mysql_fetch_array($result);
  31.  
  32.              $validate = $this->val_session($row['session_di'], $row['session_ti']);
  33.  
  34.              
  35.  
  36.              if($validate == true){
  37.  
  38.                  $this->update_session($_SESSION['session_id'] /* , $some_user_obj */);
  39.  
  40.              }else{
  41.  
  42.                  // Might be used to strip the "login" then extend this part//
  43.  
  44.                  // Now the "guest" will only get a new id, usually someone //
  45.  
  46.                  // Is altered from "reg" status to "guest" status          //
  47.  
  48.                  $this->delete_session($_SESSION['session_id']);
  49.  
  50.                  $_SESSION['session_id'] = $this->create_session();
  51.  
  52.              }
  53.  
  54.          }else{
  55.  
  56.              $_SESSION['session_id'] = $this->create_session();
  57.  
  58.          }  
  59.  
  60.       }  
  61.  
  62.  


and

PHP Code: [ Select ]
 
 
 
/* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */
 
 
 
$oSESSION->handle_session( */ $my_user_id */);
 
 
 
 
  1.  
  2.  
  3.  
  4. /* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */
  5.  
  6.  
  7.  
  8. $oSESSION->handle_session( */ $my_user_id */);
  9.  
  10.  
  11.  
  12.  


hope you are keeping track of this. You need to supply the or "a" username wich can be updated to the session table. Net the username has to be send from the "handle_session" function inside the class to the "update_session" function also in that class. Now if you would like the "usernames" posted you should just "query" these where session_us is not null.

hope you get this ;)

good luck and greets May

ps. If you are actually using a portal (wich hasnt been named up to now) you should be verry carefull with using this code. The posibility is verry real that a session is allready being handled. This would implicate the "current" portal when we are setting "new" $_SESSION vars and might thus cause errors when browsing between you custom page and the "working" portal. And might even implicate the security of that portal.

If this is actually the case i advice you to study the source of the used portal and look if a session class or file is allready being used, and just use that file / class. Chance that would work even better then this "custom" script and wont cause all kinds of "duplicate" data in you database.

BTW Nicest way of doing this is just to pass the "key" of the user row into the session table and fetch the user data based on that key from the users table ;-)

This will allow you to fetch much more data on this user within "this" or "that" session, and make redirects possible to the "members list" when clicking one of the displayed "online" users or other fancy options ;)

-Greets
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Well, I'm trying to follow here.
I have a database setup already.
Code: [ Select ]
$query = 'CREATE TABLE `users` (
id int(11) NOT NULL auto_increment,
username varchar(32) NOT NULL UNIQUE,
password varchar(32) NOT NULL,
firstname varchar(32) NOT NULL,
lastname varchar(32) NOT NULL,
email varchar(32) NOT NULL UNIQUE,
medium varchar(32) NOT NULL,
studio varchar(32) default NULL,
tours varchar(32) default NULL,
website varchar(32) default NULL,
address varchar(32) NOT NULL,
city varchar(32) NOT NULL,
province varchar(32) NOT NULL,
zip varchar(7) NOT NULL,
phone int(13) default NULL,
biography text NOT NULL,
PRIMARY KEY (`id`))
ENGINE=INNODB;';
  1. $query = 'CREATE TABLE `users` (
  2. id int(11) NOT NULL auto_increment,
  3. username varchar(32) NOT NULL UNIQUE,
  4. password varchar(32) NOT NULL,
  5. firstname varchar(32) NOT NULL,
  6. lastname varchar(32) NOT NULL,
  7. email varchar(32) NOT NULL UNIQUE,
  8. medium varchar(32) NOT NULL,
  9. studio varchar(32) default NULL,
  10. tours varchar(32) default NULL,
  11. website varchar(32) default NULL,
  12. address varchar(32) NOT NULL,
  13. city varchar(32) NOT NULL,
  14. province varchar(32) NOT NULL,
  15. zip varchar(7) NOT NULL,
  16. phone int(13) default NULL,
  17. biography text NOT NULL,
  18. PRIMARY KEY (`id`))
  19. ENGINE=INNODB;';

So I am using $username from my database inplace of "/*, $some_user_id*/" you kindly posted.
For "/* , $some_user_obj */" I am completely lost.
And ofcourse this is over my head too..
"/* $my_user_id (isset($some_user_obj['user_id']) ) ? $some_user_obj['username'] : ""; */ "

Man, you have been very helpful. But please don't feel you need to be so quick
on your replies, I'm sure you have better things to do. I am very patient.
Again thank you..Hooch
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

Could you explain this a little better?

Quote:
BTW Nicest way of doing this is just to pass the "key" of the user row into the session table and fetch the user data based on that key from the users table


Are you saying I can merge my existing users database?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Dont ever mention "merge" you are not denormalising you database and you arent a "data warehouse either" so no not "merge" more like "relate" the data together.

By defining the "user id" you are able to make an relation to the user table, this is not merging. It is also related to the "remark" you got based on "form normalisation" the "relative database" is designed for "relations" so use them as well..

Example :

Image

also easy to understand is this ;)

PHP Code: [ Select ]
 
$my_user_id = (isset($some_user_obj['user_id']) ) ? $some_user_obj['user_id'] : "";
 
 
  1.  
  2. $my_user_id = (isset($some_user_obj['user_id']) ) ? $some_user_obj['user_id'] : "";
  3.  
  4.  


and is actually the same as :

PHP Code: [ Select ]
 
 
 
if(isset($some_user_obj['user_id']) ){
 
      $my_user_id = $some_user_obj['user_id'];
 
}else{
 
      $my_user_id = "";
 
}
 
 
 
 
  1.  
  2.  
  3.  
  4. if(isset($some_user_obj['user_id']) ){
  5.  
  6.       $my_user_id = $some_user_obj['user_id'];
  7.  
  8. }else{
  9.  
  10.       $my_user_id = "";
  11.  
  12. }
  13.  
  14.  
  15.  
  16.  


Only difference is that i do the same on one rule.

Greets May
  • HoocH
  • Student
  • Student
  • HoocH
  • Posts: 70

Post 3+ Months Ago

But what does $some_user_obj need to be replaced with?
Or does it need editing?
  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

actually with a var containing the "user_id" of the user opening the page / continueing the session. If the user is logged on he "user_id" is know. So fetch it and just put it in there...

-May

Post Information

  • Total Posts in this topic: 47 posts
  • Users browsing this forum: No registered users and 117 guests
  • 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 post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.