PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

Hi everyone,
Today i am going to show you how to Create Guestbook / Shoutbox in php mysql, in a very easy way and with simple code.
First thing that you might be thinking is what is a Guestbook or Shoutbox ?
These are the places where your website visitors or users can type their views or some information in a simple box.
In this program we are going to create a simple shoutbox / Guestbook which will allow your website visitor to enter some simple data about your website.

So lets start,
First we need to create the table which will store our data into database.
Create the table structure in your MySQL database as follows



After creating database and table you need to create a database connection file.
If you don't know how to create a database connection file then you should check this.
Specify the settings in database connection file.

Now we will be creating a form which will accept user's name and their shout or opinion.
So lets design it.
If the form is not posted then we will display the form and show the existing records from the database.



if (!isset($_POST["submit"]))
{
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y') as newdate FROM guests ORDER BY id DESC LIMIT 0, 8";
$result = mysql_query($query);
?>

Post a message

















while ($row = mysql_fetch_object($result))
{
?>

message; ?>


Posted by name; ?> on newdate; ?>

}
?>

}
?>



So now you will see a form which will show you a textbox and a textarea for entering name and some shout. Just enter name and shout and click submit.

Now is the actual part of entering shout into database and come back to entry page.
So now we will be checking if the form is submitted then we will process the form.

For security we will be adding a simple spam comment prevention logic, we will not allow one ip to enter more than 5 shouts, comments to our form on same day.



$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['msg']);


if(isset($_POST["submit"]))
{
// Add new entry to the database
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("Y-m-d");

$query1 = "SELECT * FROM guests WHERE guestip='$ip' and date ='$now'";
$result1 = mysql_query($query1);
$cnt = mysql_num_rows($result1);

if($cnt<5)
{
$now = date("Y-m-d");
$query = "INSERT INTO guests (name, message, date, guestip) VALUES ('$name','$message','$now','$ip')";
$result = mysql_query($query) or die(mysql_error());
// go back to entry page
$referer = $_SERVER['HTTP_REFERER'];
header ("Location: $referer");
}
else
{
echo "Thank you for your comments. But you cannot submit more comments today as its a spam prevention mechanism to avoid spammers.";
echo "
If you are a spammer then just GO away.";
}
}



And you are done. Our shoutbox is ready to accept your website viewers shouts.

Now some interesting part to design your form and display with css






So you are ready with a nice looking shoutbox.
See here is the full source code for your testing


//code
<?php

// Database Settings

include "connect.php";

//connecting to databae and selecting database
mysql_connect($host, $user, $pass) OR die ("Error connecting to the server.");
mysql_select_db($db) OR die("Error selecting the database.");

$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['msg']);


if(isset($_POST["submit"]))
{
// Add new entry to the database
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("Y-m-d");

$query1 = "SELECT * FROM guests WHERE guestip='$ip' and date ='$now'";
$result1 = mysql_query($query1);
$cnt = mysql_num_rows($result1);

if($cnt<5)
{
$now = date("Y-m-d");
$query = "INSERT INTO guests (name, message, date, guestip) VALUES ('$name','$message','$now','$ip')";
$result = mysql_query($query) or die(mysql_error());
// go back to entry page
$referer = $_SERVER['HTTP_REFERER'];
header ("Location: $referer");
}
else
{
echo "Thank you for your comments. But you cannot submit more comments today as its a spam prevention mechanism to avoid spammers.";
echo "
If you are a spammer then just GO away.";
}
}

if (!isset($_POST["submit"]))
{
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y') as newdate FROM guests ORDER BY id DESC LIMIT 0, 8";
$result = mysql_query($query);
?>

Post a message

















<?php
while ($row = mysql_fetch_object($result))
{
?>

<?php echo $row->message; ?>


Posted by <?php echo $row->name; ?> on <?php echo $row->newdate; ?>

<?php
}
?>

<?php
}
?>




Let us know whether you liked it or not. This is just a basic sample example for you.
Its not fully secure. If you know some simple function to prevent bots using this for data entry then let me know.

I'm reading: Create Guestbook / Shoutbox in php, mysql with cssTweet this!

3 comments:

Hey nice post. I think this can be easiest way to create Shoutbox for any php website. Keep up dude.php tuts

Hi, nice post.
The only strange thing for me is the use of Varchar for dates in your SQL table.
DATETIME and DATE, are better because :
-they support comparison, additions and other stuff.
-it's more logical if you use DATEFORMAT (even if the php IntlDateFormatter is better)
-there is new tools on PHP (since 5.3) that support DATE and DATETIME without the constraints which timestamp has (the limits of 1/1/1970 and 2030)

I want to log out from the page and i have invalidate the session but still i can't prevent the user from going previous page because i can't disable the back button of my browser. So can you tell me a javascript or CSS coding for that. As i am working for PHP development India.

Post a Comment