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.

This part is for Horizontal Menu using CSS.

The below one will come under the Head Tag.


The below one will come under Body Tag.



This code was taken from http://www.w3schools.com/css/tryit.asp?filename=trycss_float5

Hi guys,

Generally we require that all the contents of our website should be centered to display the website properly.
For this we can make use of this code so that your website will be in the center of the browser, doesnt matter what is the resolution.

We will create a div with id "outer-div" in the body tag of our webpage.




Now we will apply a CSS styling to the "outer-div" div.




The full code to test is here

Hi,

Today i will be showing you how to create a simple 1 level vertical navigation menu with the help of css.
It is very easy and same like the 1 level horizontal navigation menu.

So lets start,

For this we will be using one image


first we will create a simple code to display "unordered list" with ul and li 





So now our display of the ul will be like this



Now we will apply our css to this ul



After applying this css our menu will look like this



Full code of the vertical menu with css is as, just copy and paste it, and modify links and its over.



Hi friends,

here is a simple example of 1 level horizontal navigation menu.
for this i am using 2 images - one is orange and another is blue

you can copy these images from here 


For creating a "1 level horizontal navigation menu" we are going to use "unordered list"




Now this will create an unordered list.





Now we will apply a CSS to this unordered list to make it a horizontal navigation menu.




Hey ! Its ready ! See it !





Completed ready use script and code is here

We always require some kind of login box for any system which has members.

Here i will be showing a simple example with the help of CSS and HTML to create a attractive login box.

To begin with We will create a login box first and then apply the CSS to it

Replace "youfilename.php" with your script which has the logic to check the login.





When you will add this code to your login file then display will be like this - fully stretched on the page with a simple look.

Now we will add a css to this login box to make it look nice.





Now after applying this css see at our login box.



Now combining all together we have created a very simple nice looking login box without using any other images.
Full source code is given below. Just copy and paste it in your project, if necessary just modify the css.