Forum Moderators: coopster
However, I am looking to optimize it and convert it all over so it looks professsional, it's pretty much 'Jerry Rigged' with bits of code all over the place, not very neat indenting and so on.
I was wondering how PHP aspects such as functions, globals and all that works, can anyone provide a reference I can learn from? Thanks, below is a small snippet of the chatbox I made for my website :).
Snippet from chatbox form - (Type chat)
<h1>Chat Box</h1>
<div>
<a href="<? $_SERVER['PHP_SELF']; ?>">Refresh</a> - Show top 10 records
</div>
<form method="post" action="chatbox_process.php">
<table class="chatbox">
<tr>
<td>
<input type="text" name="message" id="message" style="height:22px; width:500px;" />
</td>
<td>
<input type="image" name="submit" id="submit" src="./images/buttons/submit.png" />
<input type="hidden" name="username" id="username" value="<? echo $_SESSION['username']; ?>" />
<input type="hidden" name="time" id="time" value="<? echo date('G:i'); ?>" />
<input type="hidden" name="date" id="date" value="<? echo date('d-M-Y'); ?>" />
</td>
</tr>
</table>
</form>
Snippet from chatbox processing page
<?
require('./database.php');$username = $_POST['username'];
$message = $_POST['message'];
$time = $_POST['time'];
$date = $_POST['date'];
if(eregi('http:', $message)) {
die ("#*$!!");
}
if(empty($message)) {
echo('<meta http-equiv="Refresh" content="3; url=./?p=members&cat=chatbox" />');
echo('You must put in a message!<br />');
echo('You will be redirected in 3 seconds');
}
else {
$sql_chatbox = "INSERT INTO web_chatbox(username, message, time, date) VALUES('$username', '$message', '$time', '$date')";
$result_chatbox = mysql_query($sql_chatbox);
if($result_chatbox) {
header('location:'.$dir.'/?p=members&cat=chatbox');
}
}
?>
It's very basic but tell me what you think, it's my first ever group of PHP pages with MYSQL databases :p.
You may want to have a look at OOP (Object Orientated Programing) as that may allow you to better organize your code. OOP usually slows down the code processing, as there is more of an overhead for the server; however it tends to speed up your work with the code, as it is a lot easier to organize everything.
You may also want to have a google for articles about why you may want to use $_SERVER['SCRIPT_NAME'] as opposed to $_SERVER['PHP_SELF'].
$_SERVER['PHP_SELF']
* http://www.example.com/ -- -- -- /index.php
* http://www.example.com/index.php -- -- -- /index.php
* http://www.example.com/index.php?a=test -- -- -- /index.php
* http://www.example.com/index.php/dir/test -- -- -- /dir/test
$_SERVER['REQUEST_URI']
* http://www.example.com/ -- -- -- /
* http://www.example.com/index.php -- -- -- /index.php
* http://www.example.com/index.php?a=test -- -- -- /index.php?a=test
* http://www.example.com/index.php/dir/test -- -- -- /index.php/dir/test
$_SERVER['SCRIPT_NAME']
* http://www.example.com/ -- -- -- /index.php
* http://www.example.com/index.php -- -- -- /index.php
* http://www.example.com/index.php?a=test -- -- -- /index.php
* http://www.example.com/index.php/dir/test -- -- -- /index.php
[edited by: PHP_Chimp at 6:39 pm (utc) on June 5, 2008]