Breakdown of admin.php
We can skip the “building block” because it’s the same as the one at the start of shout.php. The first block of code in our admin.php file defines the login credentials, i.e. the username and password. Pretty simple really:
CODE
$username = "adminuser";
$password = "password";
CODE
Next in line is the login checking function:
Code:
if ( isset ( $_POST['login'] ) )
{
if (( $_POST['username'] === $username ) && ( $_POST['password'] === $password ))
{
$_SESSION['admin_logged_in'] = 'true';
}
}
This function checks that the login button has been pressed, and the user came from the right place (i.e. OUR script). It then checks that the username and password are correct before setting the session so that the user can see the admin cp.
function selectAction ( $mode )
{
switch ($mode)
{
case '':
echo 'Welcome to the administration panel, make the selection above.';
break;
case 'add':
echo '
<form action="admin.php?mode=posting" method="post" name="addSmilie" enctype="multipart/form-data">
<input name="symbol" type="text" value="=)" size="25" maxlength="4"><br>
<input name="image" type="file"><br>
<input name="addsmilie" type="submit" value="Add Smilie!"><br><br>
Check your symbol and filename, I couldnt be bothered writing an "edit smilie" function. Please note, as this is not a gdlib tutorial, there are no file dimensions protections. Please only upload 15x15 pixel smilies, if they are not this size, they will be skewed when they are resized when displayed.
</form>
';
break;
case 'delete':
$query = mysql_query("SELECT * FROM smilies") or die(mysql_error());
while($row = mysql_fetch_array($query)){
echo '<a href="admin.php?mode=posting&smilie='.$row['id'].'">
<img src="smilies/'.$row['URL'].'" border="0" width="15" height="15" alt="'.$row['Alt'].'">
</a><br><br>
';
}
break;
case 'clear':
mysql_query("TRUNCATE TABLE shouts") or die(mysql_error());
echo 'Shoutbox cleared successfully!';
break;
case 'logout':
$_SESSION['admin_logged_in'] = '';
header("Location: admin.php");
break;
case 'posting':
if(isset($_POST['addsmilie'])){
$uploaddir = 'smilies/';
$uploadfile = $uploaddir . $_FILES['image']['name'];
//echo '<br><br>'.$uploaddir.'<br>'.$uploadfile.'<br><br>';
$upload = move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);
echo '<pre>';
if( $upload == TRUE ) {
echo 'Success';
} else {
echo 'Error';
print_r($_FILES);
exit;
}
print "</pre>";
$alt = $_FILES['image']['name'];
$symbol = $_POST['symbol'];
$url = $_FILES['image']['name'];
mysql_query("INSERT INTO smilies(Symbol, URL, Alt) VALUES('$symbol','$url','$alt')") or die(mysql_error());
echo '<br><br>Successfully inserted smilie!<br><br><a href="admin.php">Admin</a> | <a href="shout.php">Shouts</a>';
exit;
}
if(isset($_GET['smilie'])){
$smilie = $_GET['smilie'];
mysql_query("DELETE FROM smilies WHERE id = '$smilie' LIMIT 1") or die(mysql_error());
echo 'Successfully deleted smilie!<br><br><a href="admin.php">Admin</a> | <a href="shout.php">Shouts</a>';
}
break;
default:
} // end switch
} // end if
A simple switch function has 4 or 5 cases, each of which are similar to using an IF statement. To put it simply, our switch checks which option the user has chosen (i.e. add smilie, delete smilie, clear shoutbox, logout, nothing or posting) and then processes the correct code. The add smilie and delete smilie options are the only two that need further processing - the clear shoutbox and logout cases just handle themselves in a line or two.
The add smilie case provides the user with a basic form to input the original symbol i.e. =) or =O, and then a box to upload a replacement smilie image. There are no dimension protections in our script, but we should limit ourselves to images 15x15 pixels in size, or they will look silly when displayed in the shoutbox text. Transparent GIF’s are a good option, as they allow a whole multitude of different background colours.
The delete smilie case provides the user with a list all of the current smilies and, when the user clicks one, the posting case authenticates the click and deletes the appropriate smilie.
The posting case is based on the add smilie case. It first authenticates that the user wants to upload a smilie, then defines the directory to upload to, and uploads the file. It then puts the filename into the ALT and URL fields in the database with the Symbol equal to the symbol field on the form.
Emptying the shoutbox uses the SQL query: TRUNCATE TABLE [tablename] to completely empty the table, removing all shouts in the database in milliseconds.
CODE
if ( $_SESSION['admin_logged_in'] === 'true' )
{
Well… this just makes sure the user is logged in with administrator permissions before continuing. Now we can finally get out of functions and into the actual HTML of the page:
CODE
<html>
<head>
<title>Shoutbox Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Smilie administration: <a href="admin.php?mode=add">add smilie</a> | <a href="admin.php?mode=delete">delete smilie</a></p>
<p>Shoutbox administration: <a href="admin.php?mode=clear">clear shoutbox</a> | <a href="admin.php?mode=logout">logout</a></p>
<table width="600" border="1" cellpadding="5" bordercolor="#ccc">
<tr>
<td><?php selectAction($_GET['mode']); ?></td>
</tr>
</table>
</body>
</html>
This is just basic HTML with a title, some links and a content area that calls the selectAction function, providing it with the URL variable ‘mode’. This page probably won't validate through a HTML standards checker, but it is only meant as an example anyway.
CODE
<?php
// showing login form
} else {
echo '
<form action="admin.php" method="post" name="login">
<input name="username" type="text" value="username" size="25" maxlength="32"><br>
<input name="password" type="password" value="password" size="25" maxlength="32"><br>
<input name="login" type="submit" value="login">
</form>
';
}
mysql_close($l);
?>
This final snippet of code ensures that if the user isn’t logged in, he/she will be given a nice login form to inwardly digest instead...
I hope this walkthrough has helped you in your quest for shoutbox-dom.