Jump to content

PHP Userlist Code [Kinda Newbie]


DootThaLoop

Recommended Posts

Hi, I haven't been doing PHP for very long, anyway more seriously into it lately than ever, all I basically know how to do is edit and manipulate pre-written scripts and run pre-written MySQL queries. So I'm kind of a beginner. At any rate, I setup a PHP login/registration system for my site (I will provide the link if you need the example), and I was thinking of getting a userlist script going, you know, so on one page it will provide what members are on what pages, since I have so many pages within the site. I managed to find one script so far that was what I was looking for, and it seems all too confusing to me. (By the way, if anyone can provide a better script, I would much appreciate it.) Anyway, here is the script I have so far, customized with my database info and all (password hidden of course) and the instructions are within the script:

 

<?php

  /******************************************************
    Who is online 1.01
    Written by Erik Holman at 19 September 2003
    Started at 12:00 and ended at 14:45

    This scripts tracks how many visitors there are
    online at the moment of visiting.

    ======= HOW TO WORK =======
    ===========================
    include somewhere in your page track( "action" [, $member_id] );
    There are some optional parameters.
    For example:
    <?php
      include( "who.php" );
      track( "Visiting guestbook" , $_SESSION['member_id'] );
      // OR
      track( "Browsing forum" );
      // OR
      track( );
    ?>
    ===========================
    ===========================
    BUG UPDATES
    22 September 2003
      - Always one visitor with no SESSIONID is online :S This one won't be shown
    ===========================
    21 May 2004
      - $visitors changed in $visitor (thanks by Matthias H.)   at line 92
    ===========================
    ===========================
    SQL Query to create table
    CREATE TABLE myphp_who (
      who_sessid varchar(32) NOT NULL default '',
      who_mem bigint(20) NOT NULL default '0',
      who_what text NOT NULL,
      who_date varchar(12) NOT NULL default ''
    )
    ===========================

    Visit www.my-php.tk for more free PHP scripts.
  ******************************************************/

  $interval = 1;    //in minutes           //The interval before a row will be deleted from
                                           //the database. (Timer for Inactivity)
  $default_action   = "Visiting Homepage"; //Default action when no action is given

  $td_background_img = "background.gif";   //Background image for the Column
  $td_background_clr = "white";            //Background color for the column
                                           //When leaving empty, defaults will be used.

  $class_header     = "boxmain";           //The classname for the header text of the table
  $class_normal     = "boxmain";           //The classname for the normal text of the table

  $use_member_table = 1;                   //Leave empty or set to 0 if you don't want/have members

  $database_members = "helloism_login";            //The database if you want to have their name
  $table_members    = "users";        //The table name for your members
  $row_member_id    = "userid";           //The row name for their ID
  $row_member_name  = "username";         //The row name for their nick or real name

  $database_who     = "helloism_login";            //The database where the who is online is placed

  $server_name      = "localhost";      //The name of the server for the sql connection
  $sql_user         = "helloism_hellois";             //Username for the sql connection
  $sql_passwd       = "********";         //password for the sql connection

  /******************************************************
    Please don't edit anything underneath this line 
  ******************************************************/

  session_start();

  function now_online()
  {
    mysql_select_db( $GLOBALS['database_who'] ) 
      or die( mysql_error() );

    $query   = "SELECT * FROM myphp_who
                WHERE who_sessid != ''
                ORDER BY who_date DESC";

    $result  = query($query);
    $member  = 0;
    $visitor = 0;
    while($record = mysql_fetch_object($result))
    {
      if( $record->who_mem )  $member++;
      else                    $visitor++;
    }
    $buffer .= "Online: ";
    if( $member )  $buffer .= $member  . " Members, ";
    if( $visitor ) $buffer .= $visitor . " Visitors";
    return( $buffer );
  }

  //In this function there will be displayed which members are online and 
  //what they or your visitors are watching.
  function who()
  {
    mysql_select_db( $GLOBALS['database_who'] )
      or die(mysql_error());

    $query = "SELECT * FROM myphp_who
              WHERE who_sessid != '' 
              ORDER BY who_date DESC";
    $result = query($query);
    while($record = mysql_fetch_object($result))
    {
      mysql_select_db( $GLOBALS['database_members'] )
        or die(mysql_error());

      if( $GLOBALS['use_member_table'] )
      {
        $query2 = "SELECT " . $GLOBALS['row_member_name'] . 
                  " FROM " . $GLOBALS['table_members'] .
                  " WHERE " . $GLOBALS['row_member_id'] . " = '" . $record->who_mem ."'";
        $result2 = query( $query2 );
        $record2 = mysql_fetch_object( $result2 );
      }
      $highlight_you = "";
      $highlight_you_end = "";

      if( session_id() == $record->who_sessid )
      {
        $highlight_you = "<a>";
        $highlight_you_end = "</a>";
      }

      if( ! $record->who_mem OR ! $GLOBALS['use_member_table'] )
        $buffer .= "<tr><td class=\"" . $GLOBALS['class_normal'] . "\"
                          background=\"" . $GLOBALS['td_background_img'] . "\" 
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">" .
                            $highlight_you . "<em>Visitor</em>
                        </td>\n
                        <td class=\"" . $GLOBALS['class_normal'] . "\" 
                          background=\"" . $GLOBALS['td_background_img'] . "\"
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">" .
                            $record->who_what . $highlight_you_end . "
                        </td>\n
                        <td width=108 class=\"" . $GLOBALS['class_normal'] . "\" 
                          background=\"" . $GLOBALS['td_background_img'] . "\" 
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">".
                            substr($record->who_date,3,2) . "-" . 
                            substr($record->who_date,4,2) . "-" . 
                            substr($record->who_date,0,4) . " " .
                            substr($record->who_date,8,2) . ":" .
                            substr($record->who_date,10,2) .
                        "</td>\n
                   </tr>\n";

      elseif( $record->who_mem AND $GLOBALS['use_member_table'] )
        $buffer .= "<tr><td class=\"" . $GLOBALS['class_normal'] . "\"
                          background=\"" . $GLOBALS['td_background_img'] . "\" 
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">" .
                            $highlight_you . "
                            <strong><em>" . $record2->$GLOBALS['row_member_name'] . "</em></strong>
                        </td>\n
                        <td class=\"" . $GLOBALS['class_normal'] . "\"
                          background=\"" . $GLOBALS['td_background_img'] . "\" 
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">" .
                            $record->who_what . $highlight_you_end . "
                        </td>\n
                        <td width=108 class=\"" . $GLOBALS['class_normal'] . "\"
                          background=\"" . $GLOBALS['td_background_img'] . "\"
                          bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">".
                            substr($record->who_date,3,2) . "-" . 
                            substr($record->who_date,4,2) . "-" . 
                            substr($record->who_date,0,4) . " " .
                            substr($record->who_date,8,2) . ":" .
                            substr($record->who_date,10,2) .
                        "</td>\n
                   </tr>\n";
    }
    echo "<table cellspacing=0 cellpadding=0 bgcolor=#000000>
            <tr>
              <td>
                <table cellspacing=1 cellpadding=3 width=100%>
                  <tr>
                    <td class=\"" . $GLOBALS['class_header'] . "\"
                      background=\"" . $GLOBALS['td_background_img'] . "\"
                      bgcolor=\"" . $GLOBALS['td_background_clr'] . "\"><strong>Who</td>
                    <td class=\"" . $GLOBALS['class_header'] . "\"
                      background=\"" . $GLOBALS['td_background_img'] . "\"
                      bgcolor=\"" . $GLOBALS['td_background_clr'] . "\"><strong>What</td>
                    <td class=\"" . $GLOBALS['class_header'] . "\"
                      background=\"" . $GLOBALS['td_background_img'] . "\"
                      bgcolor=\"" . $GLOBALS['td_background_clr'] . "\"><strong>Last action</td>
                  </tr>" .
                  $buffer . "
                  <tr> 
                    <td colspan=3 align=center class=\"" . $GLOBALS['class_normal'] . "\"
                      background=\"" . $GLOBALS['td_background_img'] . "\" 
                      bgcolor=\"" . $GLOBALS['td_background_clr'] . "\">
                        You are indicated with this <a>color</a>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>";
  }

  //The visit will be logged to track the visitor
  function track( $action = "" , $id = 0)
  {
    if( $action == "" )  $action = $GLOBALS['default_action'];
    $query = "SELECT * FROM myphp_who WHERE who_sessid = \"" . session_id() . "\" ";
    $result = query( $query );

    //When the user's session id exists in database do:
    if( mysql_num_rows( $result ) ) 
    {
      $query = "UPDATE `myphp_who`
                SET who_sessid = \"". session_id() ."\",
                    who_mem = '" . $id . "',
                    who_what = \"" . addslashes($action) . "\",
                    who_date = \"". date( "YmdHi" ) . "\"
                WHERE who_sessid = \"" . session_id() . "\"";
      $result = query( $query );
    } else

    //Else add him with a new session id
    {
      $query = "INSERT INTO myphp_who
                ( `who_sessid` , `who_mem` , `who_what` , `who_date` ) 
                VALUES ( \"" . session_id() . "\", \"" . $id . "\", \"" . $action . "\" , \"" .
                date( "YmdHi" ) . "\")";
      $result = query( $query );
    }
  }


  //Understanding function will delete the inactive visitors from the database
  function find_delete()
  {
    $now = date("YmdHi");   // yyyymmddhhmm, ex. 200009021431 
    $timer_ago = date("YmdHi", mktime(date("H"), date("i") - $GLOBALS['interval'], 0, 
                 date("m"), date("d"), date("Y"))); 
    $query = "DELETE FROM myphp_who
              WHERE who_date < " . $timer_ago ;
    query($query);
  }

  //Connect to the database
  function connect( $db , $server , $user , $pw )
  {
    $dbServer = mysql_connect( $server , $user , $pw );
    mysql_select_db( $db , $dbServer );
  }

  //The query will be runned here
  function query( $query )
  {
    connect( $GLOBALS['database_who'] , $GLOBALS['server_name'] ,
             $GLOBALS['sql_user'] , $GLOBALS['sql_passwd'] );
    $result = mysql_query( $query ) or die( mysql_error() . "<br>query: " . $query );
    return (  $result );
  }

  find_delete();

  //Reffer to the function who() when requested http://www.site.com/who.php?action=who
  if($HTTP_GET_VARS['action'] == "who")  who();
?>

 

 

Okay so I ran the aforementioned MySQL query, and it created a table in helloism_login. So then I uploaded the who.php (this file) and I created another file, userlist.php, with the following code:

 

<?php
    
  include("who.php");
  track("Viewing Main", $_SESSION['member_id']);
  echo now_online();
  who();

?>

 

This resulted in the following: http://www.helloismileyou.com/ynw/login/userlist.php

 

As you can see, it constantly says 1 member online, and that member is 'Visitor', of course, I don't have a member named Visitor. The problem here is, it won't display my members' names that are online, when I checked the online user part of my login database and I saw several members online. Also, I have no idea how to display what members are on what page. I'm not even sure if this is the right script for me, once again, if you can find a better one let me know, but this is the only one I could find. Help would be much appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/105110-php-userlist-code-kinda-newbie/
Share on other sites

this is what i think you are looking for, you will need to change it for your needs, but this is what i use as my member page.

it lists the members , and then gives each one a dynamic link (?id=11) and this is it's profile page

 


<?php
if ($_SESSION['is_valid'] == true){
if (isset($_GET['id'])) {
if ((int) $_GET['id'] > 0) {
$userid = $_GET['id'];
$sql = "SELECT * FROM $user WHERE `user_id`='{$userid}' LIMIT 0,1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$userprofname = $row['user_name'];
$profemail = $row['user_email'];
echo "$userprofname<br>";
$show_email = $row['show_email'];
if ($show_email == 1)
{ 
echo 'Email:<a href="mailto:'.$profemail.'">    Email Me</a>';
}
elseif ($show_email == 0)
{
echo "Email:   Hidden";
}
exit();
}
else {
echo "Invalid user! <br />";
echo "<a href=\"members.php\">Return to Members</a>";
exit();
}
}
//No ID passed to page, display user list:
$query = "SELECT user_id, user_name FROM $user";
$result = mysql_query($query) or die("Error:" . mysql_error());
if (mysql_num_rows($result) > 0) {
echo "User List:<br />";
while ($row = mysql_fetch_assoc($result)) {
  echo '<a href="?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a><br />';
}
}
}
else
{
echo "Please login to view this page.";
}
?>

 

if you need more help, just ask

Sorry but that doesn't seem to work for me... at first I tried the script as a whole, then it kept saying "Please login to view this page", even after I had logged into my site. So I took out the following in hopes it would display what I wanted:

 

else
{
echo "Please login to view this page.";

 

However, it just came up as a blank page. I tried changing everything I could think of in the code, but I'm still lost...

Archived

This topic is now archived and is closed to further replies.

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