Forum Topic: Shoutbox (php/mysql)
Home > Forums > Tutorials > Shoutbox (php/mysql) (70 views - 0 replies) |
||
| Author | Topics: |
|
First off we need to create our database. Then add tables.. You can copy/paste this into a file from your computer, save it as tbl.sql and then upload it to your phpMyAdmin...
here is the create table code: CREATE TABLE `data` ( `sb_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY , `sb_name` VARCHAR(35) NOT NULL , `sb_body` TEXT NOT NULL , `sb_date` VARCHAR(20) NOT NULL , `sb_ipa` VARCHAR(70) NOT NULL , `sb_email` VARCHAR(100) NOT NULL ); now create a file called mysql.php & copy/paste this: <?php $db_mysql_h = "localhost"; $db_mysql_u = "databaseuser_username"; $db_mysql_p = "database password"; $db_mysql_n = "databaseuser_name"; $connection = mysql_connect('$db_mysql_h' , '$db_mysql_u' , '$db_mysql_p' , '$db_mysql_n') or die("Connect: ".mysql_error()." in mysql.php" ;if (!$connection) { echo("We have an error!" ;exit; }else{ mysql_select_db('$db_mysql_n', $connection) or die("Select Database: ".mysql_error()." in mysql.php" ;} ?> Our database file will connect, it will show us our error along with our custom warning. If no errors, then we will select our database and connect! Now create submit.php <?php $db_file = "mysql.php"; if (!file_exists($db_file)) { echo "Warning, our database file is missing.!\n"; exit; }else{ function checkEmail($email) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) { return FALSE; } list($Username, $Domain) = split("@",$email); if(getmxrr($Domain, $MXHost)) { return TRUE; }else{ if(fsockopen($Domain, 25, $errno, $errstr, 30)) { return TRUE; }else{ return FALSE; } } } if (isset($_POST['submit'])) { /* our variables from our form */ $name = $_POST['sb_name']; $body = $_POST['sb_body']; $email = $_POST['sb_email']; $date = date("m/d/Y" ;$ip = $_SERVER['REMOTE_ADDR']; /* we'll run an error check, if no errors then insert the data, redirect you to the form! */ if (empty($_POST['sb_name'])) { // did they enter thier name? echo "Enter your name! <a href='".$_SERVER['PHP_SELF']."'>Return</a>\n"; exit; } if (empty($_POST['sb_body'])) { // did the user enter a message? echo "Enter a message! <a href='".$_SERVER['PHP_SELF']."'>Return</a>\n"; exit; } if (empty($_POST['sb_email'])) { // did the user enter their email? echo "Enter your email! <a href='".$_SERVER['PHP_SELF']."'>Return</a>\n"; exit; } if(checkEmail($_POST['sb_email']) == FALSE) { // is our email address valid? echo "Invalid email address! <a href='".$_SERVER['PHP_SELF']."'>Return</a>\n"; exit; } $sql = "INSERT INTO `data` (`id` , `sb_name` , `sb_body` , `sb_date` , `sb_ipa` , `sb_email`) VALUES('' , '$name' , '$body' , '$date' , '$ip' , '$link' , '$email')"; mysql_query($sql) or die("Insert Error: ".mysql_error()." in submit.php" ;$self = $_SERVER['PHP_SELF']; echo "<script>location.href = '$self';</script>"; }else{ // if we have not set the submit button! echo(' <form action="'.$_SERVER['PHP_SELF'].'" method="post" name="form" id="form"> <table> <tr><td>Name:</td><td><input type="text" name="sb_name" size="10"></td></tr> <tr><td>Body:</td><td><input type="text" name="sb_body" size="10"></td></tr> <tr><td>Email:</td><td><input type="text" name="sb_email" size="10"></td></tr> <tr><td></td><td><input type="submit" name="submit" value="Post!"> <input type="reset" value="clear!"></td></tr> </table> </form> <script>document.form.sb_body.focus();</script> '); } } ?> Now we will need to get our data, call this file output.php <?php $sec = "5"; $url = $_SERVER['PHP_SELF']; header("Refresh: $sec; url=$url" ; // reloads output.php every 5 seconds.$db_file = "mysql.php"; if (!file_exists($db_file)) { echo "Warning, our database file is missing.!\n"; exit; }else{ $list = "DESC"; // DESC = Descending (msgs will scroll newest to oldest) ASC = Ascending (msgs will scroll oldest to newest) $query = mysql_query("SELECT * FROM `data` ORDER BY `sb_id` $list" or die("Select Error: ".mysql_error()." in output.php" ;while ($fetch = mysql_fetch_array($query)) { $id = $fetch['sb_id']; $name = $fetch['sb_name']; $body = $fetch['sb_body']; $date = $fetch['sb_date']; $email = $fetch['sb_email']; /* remove this if you want to allow swear words */ $words = array( 'ass' => '***' , 'bitch' => '*****' , 'cunt' => '****' , 'damn' => '****' , 'fuck' => '****' , 'milf' => '****' , 'nigger' => '******' , 'pussy' => '*****' , 'slut' => '****' , 'tits' => '****' , ); foreach ($words as $word => $rep) { $body = str_replace($word, $rep, $body); // will get rid of the bad word with the *'s } $smile = array( ': )' => 'smile url here' , '; )' => 'wink url here' , ': D' => 'big smile url here' ); foreach ($smile as $key => $url) { $body = str_replace($key, $rep, $body); } if ($email == NULL) { $name = "$name"; }else{ $name = '<a href="mailto:'.$email.'">'.$name.'</a>'; } echo('<p>'.$name.' : '.$body.'</p>'); // will output all the data } } ?> Now what you need to do is make a file that will display the form, and the messages something like this (iframes) <iframe src="output.php" width="150" height="300" scroll="auto"></iframe> <iframe src="submit.php" width="150" height="100" scroll="auto"></iframe> So there you have it! your very own shout box.... enjoy! |
| Toon-Workshop |



























;