Jump to content

Let certain usernames and Messages appear in its own color


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi I'm trying to make use of an array with usernames in it and want to make use of it to display users with those usernames in a certain color in this case Im trying red.

 

I've made an array with username in it

$admin = array("SYSOP","~cobusbo~");

and I've tried to  print these messages with color with the If function without luck

if ($name == $admin) {
$name = print '<span style="color:red">' . $_SERVER["HTTP_X_MXIT_NICK"] . '</span>';
}
else
{
$name = print $_SERVER["HTTP_X_MXIT_NICK"];
}

Here is my full code

<?php
        /*** begin the session ***/
        session_start();

        /*** create the form token ***/
        $form_token = uniqid();

        /*** add the form token to the session ***/
        $_SESSION['form_token'] = $form_token;



define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);



// database connection info
$conn = mysql_connect('**********','********','**********') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('u506124311_cobus',$conn) or trigger_error("SQL", E_USER_ERROR);


// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM StringyChat";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;




    /*
     * StringyChat
     *
	 * Please refer to readme.txt supplied with the StringyChat distribution for information on
	 * installing and configuring. 
	 *
     */








define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);

    include("chat_code_header.php");

    $result = mysql_query("SELECT * FROM ".$ConfigTable, $db); 
    $myrow = mysql_fetch_array($result); 

    $domain_installed = $myrow["domain_installed"]; // The domain StringyChat is installed on
	$install_url = $myrow["install_url"]; // URL to install dir of StringyChat
    $name_size = $myrow["name_size"]; // Maximum size of the name
    $message_size = $myrow["message_size"]; // Maximum message size. Do not exceed 250 as this is the database limit.
    $line_length = $myrow["line_length"]; // Maximum length of words in a line. Anything above this value will be split.
    $ShowPostNum = $myrow["show_posts"]; // The number of historic posts to load and display.
    $email_notification = $myrow["email_notification"]; // Send email to administrator when new posts are made. 0 = No, 1 = Yes
	$email_notification_to = $myrow["email_notification_to"]; // The email address to send notifications to

    
	if ($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['StringyChat_name'])) 
    { 
    $StringyChat_name = $_POST['StringyChat_name'];
    $StringyChat_message = $_POST['StringyChat_message'];
    }
	
?>

<div id="StringyChat">

<?
    // Check if visitor's IP is banned.  If so, do not display the form,
    // show a banned IP message instead.


    $ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM StringyChat_IPBan WHERE ip=\"$ip\"";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    if($myrow["ip"] == "") {	// Checks if IP not found in banned list




?>
<html><form name="StringyChat_form" method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
      <br>
      <input type="hidden" name="StringyChat_name" class="StringyChatFrm" value="<?php $name ?>1" size="20">
      <br>
      <textarea name="StringyChat_message" class="StringyChatFrm" cols="20" rows="4"></textarea>
      <br>
      <input name="StringyChat_submit" class="StringyChatFrm" type="submit" value="Post Message">
    </form>
</html>

<?

    } else {
      echo "Posting disabled - Your IP has been banned.";
    }
    
    // Should we try to create a post?
    if (isset($StringyChat_name) && isset($StringyChat_message)) {

        // Remove whitespaces and slashes.
        $name = trim(stripslashes($StringyChat_name));
        $message = trim(stripslashes($StringyChat_message));

        
            // Check name and message have been entered.
            if (strlen($name) > 0 && strlen($message) > 0) {
                
                // Limit the size of the fields as per variable defnitions.
                if (strlen($name) > $name_size) {
                    $name = substr($name, 0, $name_size);
                }
                if (strlen($message) > $message_size) {
                    $message = substr($message, 0, $message_size);
                }
                
                // Remove new lines from name.
                $name = str_replace("\n", " ", $name);
                
				// Stripping out \r's so email formattnig appears correctly.
				$message = str_replace("\r", "", $message);
				
				// Create an email-friendly version of the message.
				$message_emailable = str_replace("<br>", "\n", $message);
				$result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
                while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	              $the_word = $myrow_wordswap["word"];
                  $message_emailable = ereg_replace($the_word, "!*#$%",$message_emailable);
                }

                // Replace the new lines with encoded line breaks for HTML (thanks milahu).
                $message = str_replace("\n", "c#lb", $message);

                // Use HTML encoding on ame and message so database doesn't misinterpret data.
	            $name = htmlentities($name);
                $message = htmlentities($message, ENT_COMPAT);
				
                
                
                // IP address of submitter and time of post.
$ip = $_SERVER["REMOTE_ADDR"];
$name = $_SERVER["HTTP_X_MXIT_NICK"];
$msg = $_POST['StringyChat_message'];
$post_time = date("U");
$admin = array("SYSOP","~cobusbo~");
$mxitid = $_SERVER["HTTP_X_MXIT_USERID_R"];



if ($name == $admin) {
$name = print '<span style="color:red">' . $_SERVER["HTTP_X_MXIT_NICK"] . '</span>';
}
else
{
$name = print $_SERVER["HTTP_X_MXIT_NICK"];
}

if(!isset($mxitid, $name ))
{
	$mxitid = "DEFAULT";
	$name = "SYSOP";

}

                // check to see if a duplicate exists
                $sql = "SELECT * FROM StringyChat WHERE StringyChat_ip=\"$ip\" AND StringyChat_message=\"$msg\" AND StringyChat_time>($post_time - 3600 )";
                $result = mysql_query($sql);
                $myrow = mysql_fetch_array($result);

                if($myrow["StringyChat_message"] == "") {	// Checks if record not matching in db
                  // Save the record
                  $sql = "INSERT INTO StringyChat (StringyChat_ip,StringyChat_name,StringyChat_message,StringyChat_time,mxit_id) VALUES (\"$ip\",\"$name\",\"$msg\",$post_time,$mxitid)";
                  $result = mysql_query($sql);
				  
				  $theTo = $email_notification_to;
				  $theSubject = "New StringyChat post at ".$domain_installed;
				  $theMessage = "A new StringyChat post has been made.\n\n";
				  $theMessage .= $name . "\n";
				  $theMessage .= date("H:i - d/m/y", $post_time) . "\n";
				  $theMessage .= $message_emailable . "\n\n";
  				  $theMessage .= "Visit ".$domain_installed." to view StringyChat and much more!";
				  $theHeaders = "From: StringyChat at ".$domain_installed." <".$email_notification_to.">\r\n";
				  
				  mail($theTo,$theSubject,$theMessage,$theHeaders);

                } else {
                  echo "Duplicate post detected<br>";
                }                
            }
            else {
                echo "<font color=\"red\">You must Type a message</font><br><br>";
            }

      unset($_POST["StringyChat_name"]);
      unset($_POST["StringyChat_message"]);
      unset($StringyChat_ip);
      unset($StringyChat_name);
      unset($StringyChat_message);
      unset($StringyChat_time);
    }
    
// get the info from the db 
$sql = "SELECT StringyChat_time, StringyChat_name, StringyChat_message FROM StringyChat ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);



function filterBadWords($str)
{
	
	
    $result1 = mysql_query("SELECT word FROM StringyChat_WordBan") or die(mysql_error()); 
    $replacements = ":-x";
    
    while($row = mysql_fetch_assoc($result1))
    {
          $str = eregi_replace($row['word'], str_repeat(':-x', strlen($row['word'])), $str);
    }  
    
    return $str;
}



// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) 
//while (($pmsg = $list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])
 {
   // echo data
   //echo ($pmsg = ($list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])


   print '<span style="color:#828282">' . '(' . date( 'D H:i:s', $list['StringyChat_time'] )  . ') ' . '</span>' . '<b>' . $list['StringyChat_name'] . '</b>' . ' : ' . filterBadWords($list['StringyChat_message']) . '<br />';


}






    // Load up the last few posts.  The number to load is defined by the "ShowPostNum" variable.
    $result = mysql_query("SELECT * FROM ".$dbTable." ORDER BY StringyChat_time DESC LIMIT " . $ShowPostNum,$db);

    include("sort_widths.php");

    while ($myrow = mysql_fetch_array($result)) {
      $msg = $myrow["StringyChat_message"];
	  
	  // Convert the encoded line break into an actual <br> tag (thanks milahu)
      $msg = str_replace("c#lb", "<br>", $msg);

      // Convert the encoded image tag into a html tag
      $msg = eregi_replace("im#([a-z]{3})", "<img src=\"http://".$install_url."images/\\1.gif\" alt=\"emoticon\">",$msg);
	  
	  // split the lines
      $msg = htmlwrap($msg, $line_length);

      $result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
      while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	    $the_word = $myrow_wordswap["word"];
        $msg = ereg_replace($the_word, ":-x",$msg);
      }

   
    }
   ?>     
<?









// end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?><br>
    <html>  <i>Type your Message here...</i>:<br></html>

$admin is an array. It doesn't make sense to compare a string to an array.

 

You want in_array.

Ok thanks I've implemented it but seems like I have done something wrong

 

here is my current code

<?php
        /*** begin the session ***/
        session_start();

        /*** create the form token ***/
        $form_token = uniqid();

        /*** add the form token to the session ***/
        $_SESSION['form_token'] = $form_token;

	require_once('common.php');
	checkUser();


define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);
?>
<html><br>
<span style="color:red"><b><u>Chat Beta: Errors may apply please send feedback with contact us regarding problems!</u></b></span>
</html>
<?
// database connection info
$conn = mysql_connect('**********','u506124311_***********') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('u506124311_cobus',$conn) or trigger_error("SQL", E_USER_ERROR);


// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM StringyChat";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;




    /*
     * StringyChat
     *
	 * Please refer to readme.txt supplied with the StringyChat distribution for information on
	 * installing and configuring. 
	 *
     */





define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);



    include("chat_code_header.php");

    $result = mysql_query("SELECT * FROM ".$ConfigTable, $db); 
    $myrow = mysql_fetch_array($result); 

    $domain_installed = $myrow["domain_installed"]; // The domain StringyChat is installed on
	$install_url = $myrow["install_url"]; // URL to install dir of StringyChat
    $name_size = $myrow["name_size"]; // Maximum size of the name
    $message_size = $myrow["message_size"]; // Maximum message size. Do not exceed 250 as this is the database limit.
    $line_length = $myrow["line_length"]; // Maximum length of words in a line. Anything above this value will be split.
    $ShowPostNum = $myrow["show_posts"]; // The number of historic posts to load and display.
    $email_notification = $myrow["email_notification"]; // Send email to administrator when new posts are made. 0 = No, 1 = Yes
	$email_notification_to = $myrow["email_notification_to"]; // The email address to send notifications to

    
	if ($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['StringyChat_name'])) 
    { 
    $StringyChat_name = $_POST['StringyChat_name'];
    $StringyChat_message = $_POST['StringyChat_message'];
    }
	
?>

<div id="StringyChat">

<?
    // Check if visitor's IP is banned.  If so, do not display the form,
    // show a banned IP message instead.


    $ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM StringyChat_IPBan WHERE ip=\"$ip\"";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    if($myrow["ip"] == "") {	// Checks if IP not found in banned list




?>
<html><form name="StringyChat_form" method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
      <input type="hidden" name="StringyChat_name" class="StringyChatFrm" value="<?php echo $_SESSION['userName']; ?>" size="20">
      <textarea name="StringyChat_message" class="StringyChatFrm" cols="20" rows="1"></textarea>
      <br>
      <input name="StringyChat_submit" class="StringyChatFrm" type="submit" value="Post Message">
    </form>
</html>

<?

    } else {
      echo "Posting disabled - Your IP has been banned.";
    }
    
    // Should we try to create a post?
    if (isset($StringyChat_name) && isset($StringyChat_message)) {

        // Remove whitespaces and slashes.
        $name = trim(stripslashes($StringyChat_name));
        $message = trim(stripslashes($StringyChat_message));

        
            // Check name and message have been entered.
            if (strlen($name) > 0 && strlen($message) > 0) {
                
                // Limit the size of the fields as per variable defnitions.
                if (strlen($name) > $name_size) {
                    $name = substr($name, 0, $name_size);
                }
                if (strlen($message) > $message_size) {
                    $message = substr($message, 0, $message_size);
                }
                
                // Remove new lines from name.
                $name = str_replace("\n", " ", $name);
                
				// Stripping out \r's so email formattnig appears correctly.
				$message = str_replace("\r", "", $message);
				
				// Create an email-friendly version of the message.
				$message_emailable = str_replace("<br>", "\n", $message);
				$result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
                while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	              $the_word = $myrow_wordswap["word"];
                  $message_emailable = ereg_replace($the_word, "!*#$%",$message_emailable);
                }

                // Replace the new lines with encoded line breaks for HTML (thanks milahu).
                $message = str_replace("\n", "c#lb", $message);

                // Use HTML encoding on ame and message so database doesn't misinterpret data.
	            $name = htmlentities($name);
                $message = htmlentities($message, ENT_COMPAT);
				
                
                
                // IP address of submitter and time of post.
$ip = $_SERVER["REMOTE_ADDR"];
$name = $_SESSION['userName'];
$msg = $_POST['StringyChat_message'];
$post_time = date("U");
$admin = array("SYSOP","~cobusbo~");
$mxitid = $_SERVER["HTTP_X_MXIT_LOGIN"];

if (in_array("SYSOP", "cobusbo", $admin))
{
print '<span style="color:red">' . $name . '</span>';
}
else
{
print '$name'; 
}


if(!isset($mxitid))
{
	$mxitid = "DEFAULT";

}
if(!isset($name))
{
	$name = "SYSOP";
}




                // check to see if a duplicate exists
                $sql = "SELECT * FROM StringyChat WHERE StringyChat_ip=\"$ip\" AND StringyChat_message=\"$msg\" AND StringyChat_time>($post_time - 1800 )";
                $result = mysql_query($sql);
                $myrow = mysql_fetch_array($result);

                if($myrow["StringyChat_message"] == "") {	// Checks if record not matching in db
                  // Save the record
                  $sql = "INSERT INTO StringyChat (StringyChat_ip,StringyChat_name,StringyChat_message,StringyChat_time) VALUES (\"$ip\",\"$name\",\"$msg\",$post_time)";
                  $result = mysql_query($sql);
				  
				  $theTo = $email_notification_to;
				  $theSubject = "New StringyChat post at ".$domain_installed;
				  $theMessage = "A new StringyChat post has been made.\n\n";
				  $theMessage .= $name . "\n";
				  $theMessage .= date("H:i - d/m/y", $post_time) . "\n";
				  $theMessage .= $message_emailable . "\n\n";
  				  $theMessage .= "Visit ".$domain_installed." to view StringyChat and much more!";
				  $theHeaders = "From: StringyChat at ".$domain_installed." <".$email_notification_to.">\r\n";
				  
				  mail($theTo,$theSubject,$theMessage,$theHeaders);

                } else {
                  echo "Duplicate post detected<br>";
                }                
            }
            else {
                echo "<font color=\"red\">You must Type a message</font><br><br>";
            }

      unset($_POST["StringyChat_name"]);
      unset($_POST["StringyChat_message"]);
      unset($StringyChat_ip);
      unset($StringyChat_name);
      unset($StringyChat_message);
      unset($StringyChat_time);
      unset($mxit_id);

    }
    
// get the info from the db 
$sql = "SELECT StringyChat_time, StringyChat_name, StringyChat_message FROM StringyChat ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);



function filterBadWords($str)
{
	
	
    $result1 = mysql_query("SELECT word FROM StringyChat_WordBan") or die(mysql_error()); 
    $replacements = ":-x";
    
    while($row = mysql_fetch_assoc($result1))
    {
          $str = eregi_replace($row['word'], str_repeat(':-x', strlen($row['word'])), $str);
    }  
    
    return $str;
}



// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) 
//while (($pmsg = $list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])
 {
   // echo data
   //echo ($pmsg = ($list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])


   print '<span style="color:#828282">' . '(' . date( 'D H:i:s', $list['StringyChat_time'] )  . ') ' . '</span>' . '<b>' . $list['StringyChat_name'] . '</b>' . ' : ' . filterBadWords($list['StringyChat_message']) . '<br />' . '<p>';


}






    // Load up the last few posts.  The number to load is defined by the "ShowPostNum" variable.
    $result = mysql_query("SELECT * FROM ".$dbTable." ORDER BY StringyChat_time DESC LIMIT " . $ShowPostNum,$db);

    include("sort_widths.php");

    while ($myrow = mysql_fetch_array($result)) {
      $msg = $myrow["StringyChat_message"];
	  
	  // Convert the encoded line break into an actual <br> tag (thanks milahu)
      $msg = str_replace("c#lb", "<br>", $msg);

      // Convert the encoded image tag into a html tag
      $msg = eregi_replace("im#([a-z]{3})", "<img src=\"http://".$install_url."images/\\1.gif\" alt=\"emoticon\">",$msg);
	  
	  // split the lines
      $msg = htmlwrap($msg, $line_length);

      $result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
      while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	    $the_word = $myrow_wordswap["word"];
        $msg = ereg_replace($the_word, ":-x",$msg);
      }

   
    }
   ?>     
<?









// end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/

?><br>
    <html>  <i>Type your Message here...</i>:<br></html>

<?
include "menu3.php";
?>

Seems like I did something wrong over here

              // IP address of submitter and time of post.
$ip = $_SERVER["REMOTE_ADDR"];
$name = $_SESSION['userName'];
$msg = $_POST['StringyChat_message'];
$post_time = date("U");
$admin = array("SYSOP","~cobusbo~");
$mxitid = $_SERVER["HTTP_X_MXIT_LOGIN"];

if (in_array("SYSOP", "cobusbo", $admin))
{
print '<span style="color:red">' . $name . '</span>';
}
else
{
print '$name'; 
}

Im getting the following error each time Im trying to post a message

 

 

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/u506124311/public_html/ag/page.php on line 185

The error message tells you what you did wrong.

 

If you have read the documentation for in_array you would understand what values it expects.

Ok I'm going to pass on the arrays I tried to do it with one value only

if ($name = "cobusbo")
{
$name = '<span style="color:red">' . '$name' . '</span>';
}
else
{
$name = '$name'; 
}

What im basically trying to do is to specify admin users and moderator users. So if their name is cobusbo I want their message to appear in red. But currently the above insertion has no impact what so ever on my chat. all colors stil display in black...

 

I'm not sure what I should do regarding this problem...

  • Solution

 

 

Ok I'm going to pass on the arrays

Why?

 

The code you posted in reply #3 is on the right track. The problem is you are giving in_array the wrong values. 

 

You should pass $name as the first argument and $admin as the second argument.

if (in_array($name, $admin))
{
    print '<span style="color:red">' . $name . '</span>';
}
else
{
   print $name; 
}

A better way would be to add another column in your users table in the database. The value stored in this column will determine the role the user has. Example normal users have the role set to 1, 2 is for moderators and 3 is for admins.

 

Then all you need to do is check the role to determine the color of the users name. Example code

// an array storing the colors for each role. The role is used as the key
$colors = array(
   1 => 'black',
   2 => 'blue',
   3 => 'red'
);

$name = 'johnDoe';
$role = 3;

// dynamically assigns the color based on role
echo '<span style="color: ' . $colors[$role] . '">' . $name . '</span>'; // will show as red

 

Why?

 

The code you posted in reply #3 is on the right track. The problem is you are giving in_array the wrong values. 

 

You should pass $name as the first argument and $admin as the second argument.

if (in_array($name, $admin))
{
    print '<span style="color:red">' . $name . '</span>';
}
else
{
   print $name; 
}

A better way would be to add another column in your users table in the database. The value stored in this column will determine the role the user has. Example normal users have the role set to 1, 2 is for moderators and 3 is for admins.

 

Then all you need to do is check the role to determine the color of the users name. Example code

// an array storing the colors for each role. The role is used as the key
$colors = array(
   1 => 'black',
   2 => 'blue',
   3 => 'red'
);

$name = 'johnDoe';
$role = 3;

// dynamically assigns the color based on role
echo '<span style="color: ' . $colors[$role] . '">' . $name . '</span>'; // will show as red

Thank you but im stil receiving the same error

 

Here is my code

<?php
        /*** begin the session ***/
        session_start();

        /*** create the form token ***/
        $form_token = uniqid();

        /*** add the form token to the session ***/
        $_SESSION['form_token'] = $form_token;

	require_once('common.php');
	checkUser();


define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);
?>
<html><br>
<span style="color:red"><b><u>Chat Beta: Errors may apply please send feedback with contact us regarding problems!</u></b></span>
</html>
<?
// database connection info
$conn = mysql_connect('*******','******','*****') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('u506124311_cobus',$conn) or trigger_error("SQL", E_USER_ERROR);


// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM StringyChat";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;




    /*
     * StringyChat
     *
	 * Please refer to readme.txt supplied with the StringyChat distribution for information on
	 * installing and configuring. 
	 *
     */





define('TIMEZONE', 'Africa/Harare');
date_default_timezone_set(TIMEZONE);



    include("chat_code_header.php");

    $result = mysql_query("SELECT * FROM ".$ConfigTable, $db); 
    $myrow = mysql_fetch_array($result); 

    $domain_installed = $myrow["domain_installed"]; // The domain StringyChat is installed on
	$install_url = $myrow["install_url"]; // URL to install dir of StringyChat
    $name_size = $myrow["name_size"]; // Maximum size of the name
    $message_size = $myrow["message_size"]; // Maximum message size. Do not exceed 250 as this is the database limit.
    $line_length = $myrow["line_length"]; // Maximum length of words in a line. Anything above this value will be split.
    $ShowPostNum = $myrow["show_posts"]; // The number of historic posts to load and display.
    $email_notification = $myrow["email_notification"]; // Send email to administrator when new posts are made. 0 = No, 1 = Yes
	$email_notification_to = $myrow["email_notification_to"]; // The email address to send notifications to

    
	if ($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['StringyChat_name'])) 
    { 
    $StringyChat_name = $_POST['StringyChat_name'];
    $StringyChat_message = $_POST['StringyChat_message'];
    }
	
?>

<div id="StringyChat">

<?
    // Check if visitor's IP is banned.  If so, do not display the form,
    // show a banned IP message instead.


    $ip = $_SERVER["REMOTE_ADDR"];
    $sql = "SELECT * FROM StringyChat_IPBan WHERE ip=\"$ip\"";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    if($myrow["ip"] == "") {	// Checks if IP not found in banned list




?>
<html><form name="StringyChat_form" method="POST" action="<? echo $_SERVER['REQUEST_URI']; ?>">
      <input type="hidden" name="StringyChat_name" class="StringyChatFrm" value="<?php echo $_SESSION['userName']; ?>" size="20">
      <textarea name="StringyChat_message" class="StringyChatFrm" cols="20" rows="1"></textarea>
      <br>
      <input name="StringyChat_submit" class="StringyChatFrm" type="submit" value="Post Message">
    </form>
</html>

<?

    } else {
      echo "Posting disabled - Your IP has been banned.";
    }
    
    // Should we try to create a post?
    if (isset($StringyChat_name) && isset($StringyChat_message)) {

        // Remove whitespaces and slashes.
        $name = trim(stripslashes($StringyChat_name));
        $message = trim(stripslashes($StringyChat_message));

        
            // Check name and message have been entered.
            if (strlen($name) > 0 && strlen($message) > 0) {
                
                // Limit the size of the fields as per variable defnitions.
                if (strlen($name) > $name_size) {
                    $name = substr($name, 0, $name_size);
                }
                if (strlen($message) > $message_size) {
                    $message = substr($message, 0, $message_size);
                }
                
                // Remove new lines from name.
                $name = str_replace("\n", " ", $name);
                
				// Stripping out \r's so email formattnig appears correctly.
				$message = str_replace("\r", "", $message);
				
				// Create an email-friendly version of the message.
				$message_emailable = str_replace("<br>", "\n", $message);
				$result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
                while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	              $the_word = $myrow_wordswap["word"];
                  $message_emailable = ereg_replace($the_word, "!*#$%",$message_emailable);
                }

                // Replace the new lines with encoded line breaks for HTML (thanks milahu).
                $message = str_replace("\n", "c#lb", $message);

                // Use HTML encoding on ame and message so database doesn't misinterpret data.
	            $name = htmlentities($name);
                $message = htmlentities($message, ENT_COMPAT);
				
                
                
                // IP address of submitter and time of post.
$ip = $_SERVER["REMOTE_ADDR"];
$name = $_SESSION['userName'];
$msg = $_POST['StringyChat_message'];
$post_time = date("U");
$admin = array("SYSOP","cobusbo");
$mxitid = $_SERVER["HTTP_X_MXIT_LOGIN"];



if(!isset($mxitid))
{
	$mxitid = "DEFAULT";

}
if(!isset($name))
{
	$name = "SYSOP";
}




                // check to see if a duplicate exists
                $sql = "SELECT * FROM StringyChat WHERE StringyChat_ip=\"$ip\" AND StringyChat_message=\"$msg\" AND StringyChat_time>($post_time - 1800 )";
                $result = mysql_query($sql);
                $myrow = mysql_fetch_array($result);

                if($myrow["StringyChat_message"] == "") {	// Checks if record not matching in db
                  // Save the record
                  $sql = "INSERT INTO StringyChat (StringyChat_ip,StringyChat_name,StringyChat_message,StringyChat_time) VALUES (\"$ip\",\"$name\",\"$msg\",$post_time)";
                  $result = mysql_query($sql);
				  
				  $theTo = $email_notification_to;
				  $theSubject = "New StringyChat post at ".$domain_installed;
				  $theMessage = "A new StringyChat post has been made.\n\n";
				  $theMessage .= $name . "\n";
				  $theMessage .= date("H:i - d/m/y", $post_time) . "\n";
				  $theMessage .= $message_emailable . "\n\n";
  				  $theMessage .= "Visit ".$domain_installed." to view StringyChat and much more!";
				  $theHeaders = "From: StringyChat at ".$domain_installed." <".$email_notification_to.">\r\n";
				  
				  mail($theTo,$theSubject,$theMessage,$theHeaders);

                } else {
                  echo "Duplicate post detected<br>";
                }                
            }
            else {
                echo "<font color=\"red\">You must Type a message</font><br><br>";
            }

      unset($_POST["StringyChat_name"]);
      unset($_POST["StringyChat_message"]);
      unset($StringyChat_ip);
      unset($StringyChat_name);
      unset($StringyChat_message);
      unset($StringyChat_time);
      unset($mxit_id);

    }
    
// get the info from the db 
$sql = "SELECT StringyChat_time, StringyChat_name, StringyChat_message FROM StringyChat ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);



function filterBadWords($str)
{
	
	
    $result1 = mysql_query("SELECT word FROM StringyChat_WordBan") or die(mysql_error()); 
    $replacements = ":-x";
    
    while($row = mysql_fetch_assoc($result1))
    {
          $str = eregi_replace($row['word'], str_repeat(':-x', strlen($row['word'])), $str);
    }  
    
    return $str;
}



// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) 
//while (($pmsg = $list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])
 {
   // echo data
   //echo ($pmsg = ($list['StringyChat_message'] == $bwords) ? ":-x" : $list['StringyChat_message'])






if (in_array($name, $admin))
{
    print '<span style="color:red">' . $name . '</span>';
}
else
{
   print $name; 
}










}






    // Load up the last few posts.  The number to load is defined by the "ShowPostNum" variable.
    $result = mysql_query("SELECT * FROM ".$dbTable." ORDER BY StringyChat_time DESC LIMIT " . $ShowPostNum,$db);

    include("sort_widths.php");

    while ($myrow = mysql_fetch_array($result)) {
      $msg = $myrow["StringyChat_message"];
	  
	  // Convert the encoded line break into an actual <br> tag (thanks milahu)
      $msg = str_replace("c#lb", "<br>", $msg);

      // Convert the encoded image tag into a html tag
      $msg = eregi_replace("im#([a-z]{3})", "<img src=\"http://".$install_url."images/\\1.gif\" alt=\"emoticon\">",$msg);
	  
	  // split the lines
      $msg = htmlwrap($msg, $line_length);

      $result_wordswap = mysql_query("SELECT * FROM ".$WordBanTable,$db);
      while ($myrow_wordswap = mysql_fetch_array($result_wordswap)) {
	    $the_word = $myrow_wordswap["word"];
        $msg = ereg_replace($the_word, ":-x",$msg);
      }

   
    }
   ?>     
<?









// end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/

?><br>
    <html>  <i>Type your Message here...</i>:<br></html>

<?
include "menu3.php";
?>

And the error message is 

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/u506124311/public_html/ag/page.php on line 269

 

The code for in_array is fine the problem is with your code.

 

$admin is only defined on line 186 if the condition on line 136 is true

 if (isset($StringyChat_name) && isset($StringyChat_message))

You need to modify your code so $admin is not defined within a condition.

The code for in_array is fine the problem is with your code.

 

$admin is only defined on line 186 if the condition on line 136 is true

 if (isset($StringyChat_name) && isset($StringyChat_message))

You need to modify your code so $admin is not defined within a condition.

Thank you but that wasn't the problem.

$admin = array("SYSOP","cobusbo");

Wasn't in my query so I had to move it down to just before I did my print out and if function.

$admin = array("SYSOP","cobusbo");

if (in_array($name, $admin))
{
    print '<span style="color:red">' . $name . '</span>';
}
else
{
   print $name; 
} 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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