Home arrow PHP arrow Page 4 - Creating an Administration Area for a Simple Threaded Discussion Forum

Database connection, bad words - PHP

Discussion forums usually need an administrator to handle various tasks that you wouldn't want just any forum user doing, such as banning other users. This article, the second of two parts, will show you how to set up an administration area for a threaded discussion forum, and some of the functions you might want an administrator to manage.

TABLE OF CONTENTS:
  1. Creating an Administration Area for a Simple Threaded Discussion Forum
  2. Code
  3. The delall() and deltopic($uid) functions
  4. Database connection, bad words
By: Jacques Noah
Rating: starstarstarstarstar / 6
October 23, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The function below sets the database connection details:

function forumdb(){
$dbname="forum";
$host="localhost";
$dbh=mysql_connect($host) or die ('I cannot connect to the
database because: ' . mysql_error());
mysql_select_db ($dbname) or die('I cannot select the database
because: ' . mysql_error());
}

Remember to add your connection details to the this function.

This function shows a form that will enable you to enter a word that you want banned from the forum.

function showfrm(){
echo '<center> <form name="form1" method="post" action="' .$_SERVER['PHP_SELF'].'?action=writewords"> ';
echo 'Enter word to ban:<br>
    <input type="text" name="word"><br>
    <input type="Submit" name="Submit" value="Submit"
class="altButtonFormat"> ';
 echo '</center></form>';
 echo '<center><font color="#FF0000">Banned words will be replace
with XXXX.</font></center>';
}

Here's what the form looks like:

 

Fig. 3 The form is presented when you want a new word to be added to the banned word list.

The "getword()" function retrieves all the banned words:

function getword(){
echo "<center><b><h3>Banned Words</h3></b></center>";
echo "<center>---------------------</center>";
 $words=file('badwords.txt');
for($i=0; $i < count($words); $i++){
echo "<center>$words[$i]</center><br>";
}
}

The function interacts with a text file on the disk and uses PHP's very handy file handling functions to retrieve a list of banned words. Here's a screen shot of the above function result:

Fig. 4 A list of  banned words.

The writeword() function writes all the words to a file called "badwords.txt."

function writeword(){
if(isset($_POST['Submit'])){
$word=$_POST['word'];
if($fp=fopen('badwords.txt', 'a+')){
fwrite($fp, "$wordrn");
fclose($fp);
echo "<center>The word <b>$word</b> has now been
banned.</center>";
}else{
echo "could not open file";
}
}
}

As I mentioned before we will use a $action variable to receive the value passed from the link that has been clicked, and then use the switch command to handle the value accordingly. Here's a snippet of the switch command:

if(isset($_GET['action'])){
$action=$_GET['action'];
switch($action)
{
    Case "gettopics";
        getall();
    break;
  Case "deleteall";
        delall();
        break;

As the code above shows, once the user clicks on the "get all main topics" link the "getall();" function is called. The same thing happens when the user clicks on the "Delete all topics" link; the "delall()" function is called.

Conclusion

That's it for the admin section of the forum. Here are a few suggestions to improve its performance: create a table to store the "bad words." This will enable you to add/remove words more easily than you would with a file-based system. Try to set up a login script to control access to the admin area, as I'm sure you would not want just anyone to have access to the functions on that page!



 
 
>>> More PHP Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: