Showing posts with label IP. Show all posts
Showing posts with label IP. Show all posts

Aug 22, 2011

How To Make A Shoutbox Using PHP & MYSQL | Create A Shout Box Using PHP MYSQL | Make A Simple Shout Box

First of all, let me explain you what is a shoutbox.

A Shoutbox is nothing but a chat program, where anyone can chat with any anonymous user.
You can read more about shoutbox at this link : http://en.wikipedia.org/wiki/Shoutbox

So lets make a shoutbox.

We will make this shoutbox in basic php and mysql.

First you need to make a database where you will store this chats, so from phpmyadmin you can create a databse.
Next we will add a table, so in sql query window, just paste the below code:

[php]
CREATE TABLE `shoutbox` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`email` VARCHAR(60) NOT NULL,
`post` TEXT NOT NULL,
`ipaddress` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
);
[/php]

PhpMyAdmin

Here we made a table with name, email, id and ipaddress fields, where all values is must, id is set to auto increement and we are using IP address to track the user.

Next we will create a file named as db.php, where we make our MYSQL Connection, so just paste the below code in your file db.php

[php][/php]


<?php
$host = 'localhost'; //Normally localhost
$username = 'root'; //Username which is added to your database, root is default
$password = ''; //Password for your user, for root password is null
$database = 'shoutbox'; //your database name
?>

[php][/php]

Now we will create our actual shoutbox code file, make a file and name it to index.php, and add following code in it

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shoutbox From WebsTutorial</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="container">

<h1>Shoutbox</h1>
<h5><a href="http://www.webstutorial.com" title="WebsTutorial">WebsTutorial</a></h5>

<div id="boxtop"></div>
<div id="content">

<?php
$self = $_SERVER['PHP_SELF']; //the $self variable equals this file
$ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP
include ('db.php'); // for db details

// defines a $connect variable, which when used
// will attempt to connect to the databse using
// details provided in config.php
// if it fails, will display error - or die();
$connect = mysql_connect($host,$username,$password) or die('<p>Unable to connect to the database server at this time.</p>');

// connect to database using details provided
// and uses the $connect variable above
// if it fails, will return error - or die();
mysql_select_db($database,$connect) or die('<p>Unable to connect to the database at this time.</p>');

// checks the POST to see if something has been submitted
if(isset($_POST['send'])) {
// are any of the fields empty? the || means 'or'
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['post'])) {
echo('<p>You did not fill in a required field.</p>');
} else {

// if there are no empty fields, insert into the database:

// escape special characters to stop xss and sql injecting
// we take the 'name' and 'post' parts from the POST
// and run it through htmlspecialchars()
// this stops users sending HTML code, as it could be malicious
//
// also runs through mysql_real_escape_string()
// stops users sending SQL code, which could be used to access the db
$name = htmlspecialchars(mysql_real_escape_string($_POST['name']));
$email = htmlspecialchars(mysql_real_escape_string($_POST['email']));
$post = htmlspecialchars(mysql_real_escape_string($_POST['post']));

// this is our SQL string to insert shouts into db
$sql = "INSERT INTO shouts SET name='$name', email='$email', post='$post', ipaddress='$ipaddress';";

// we run the SQL string now
// if it succeeds, display message
if (@mysql_query($sql)) {
echo('<p>Thanks for shouting!</p>');
} else {
// if it errors, send message
echo('<p>There was an unexpected error when posting your shout.</p>');
}
}
}

// now we retrieve the 8 latest shouts from the db
$query = "SELECT * FROM shouts ORDER BY `id` DESC LIMIT 8;";

// run the query. if it fails, display error
$result = @mysql_query("$query") or die('<p>There was an unexpected error grabbing shouts from the database.</p>');

?><ul><?php
// while we still have rows from the db, display them
while ($row = mysql_fetch_array($result)) {

$ename = stripslashes($row['name']);
$eemail = stripslashes($row['email']);
$epost = stripslashes($row['post']);

// Woop! We can use Gravatars aswell!!
$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70";

echo('<li><div><p>'.$ename.'</p><img src="'.$grav_url.'" alt="Gravatar" /></div><div><p style="padding:20px 0 0 0;">'.$epost.'</p></div></li>');

}
?></ul>

<!-- at the bottom of the page, we display our comment form -->
<form action="<?php $self ?>" method="post">
<h2>Shout!</h2>
<div><label for="name"><p>Name:</p></label><input name="name" type="text" cols="20" /></div>
<div><label for="email"><p>Email:</p></label><input name="email" type="text" cols="20" /></div>
<textarea name="post" rows="5" cols="40"></textarea>
<input name="send" type="hidden" />
<p><input type="submit" value="send" /></p>
</form>

</div><!--/content-->
<div id="boxbot"></div>

</div><!--/container-->

</body>
</html>
[/html]

Explanation:
OK, in this file first we get the IP address of the person and open the db connection by including our first created db.php file.

Then we pass a check condition to check the emptiness in the textboxes. And we also store the values in respective variables, here while storing them we are using php functions htmlspecialchars, to avoid html characters and mysql_real_escape_string to avoid sql statements, this two are very important in terms of security else anyone will pass html and sql codes and pamper the database or server.

After storing them, we finally code the insert query and echoes a custom message of successful execution.

Wait a minute, this is not the end, now we will also have to display the recently 8 shouts, so we fire a select query and  store the array in a variable.

And finally in a while loop we echo the email, name and the shout message.

Woila, done, but arghhhhhhhhhhh without CSS, its like image without color, so make a file style.css and add the following css code in it.

[css]
/* Shoutbox PHP tutorial from WebsTutorial */

* {
margin: 0;
padding: 0;
}

body {
background: #323f66 top center url("images/back.png") no-repeat;
color: #ffffff;
font-family: Helvetica, Arial, Verdana, sans-serif;
}

h1 {
font-size: 3.5em;
letter-spacing: -1px;
background: url("images/shoutbox.png") no-repeat;
width: 303px;
margin: 0 auto;
text-indent: -9999em;
color: #33ccff;
}

h2 {
font-size: 2em;
letter-spacing: -1px;
background: url("images/shout.png") no-repeat;
width: 119px;
text-indent: -9999em;
color: #33ccff;
clear: both;
margin: 15px 0;
}

h5 a:link, h5 a:visited {
color: #ffffff;
text-decoration: none;
}

h5 a:hover, h5 a:active, h5 a:focus {
border-bottom: 1px solid #fff;
}

p {
font-size: 0.9em;
line-height: 1.3em;
font-family: Lucida Sans Unicode, Helvetica, Arial, Verdana, sans-serif;
}

p.error {
background-color: #603131;
border: 1px solid #5c2d2d;
width: 260px;
padding: 10px;
margin-bottom: 15px;
}

p.success {
background-color: #313d60;
border: 1px solid #2d395c;
width: 260px;
padding: 10px;
margin-bottom: 15px;
}

#container {
width: 664px;
margin: 20px auto;
text-align: center;
}

#boxtop {
margin: 30px auto 0px;
background: url("images/top.png") no-repeat;
width: 663px;
height: 23px;
}

#boxbot {
margin: 0px auto 30px;
background: url("images/bot.png") no-repeat;
width: 664px;
height: 25px;
}

#content {
margin: 0 auto;
width: 664px;
text-align: left;
background: url("images/bg.png") repeat-y;
padding: 15px 35px;
}

#content ul {
margin-left: 0;
margin-bottom: 15px;
}

#content ul li {
list-style: none;
clear: both;
padding-top: 30px;
}

#content ul li:first-child {
padding-top:0;
}

.meta {
width: 85px;
text-align: left;
float: left;
min-height: 110px;
font-weight: bold;
}

.meta img {
padding: 5px;
background-color: #313d60;
}

.meta p {
font-size: 0.8em;
}

.shout {
width: 500px;
float: left;
margin-left: 15px;
min-height: 110px;
padding-top: 5px;
}

form {
clear: both;
margin-top: 135px !important;
}

.fname, .femail {
width: 222px;
float: left;
}

form p {
font-weight: bold;
margin-bottom: 3px;
}

form textarea {
width: 365px;
overflow: hidden; /* removes vertical scrollbar in IE */
}

form input, form textarea {
background-color: #313d60;
border: 1px solid #2d395c;
color: #ffffff;
padding: 5px;
font-family: Lucida Sans Unicode, Helvetica, Arial, Verdana, sans-serif;
margin-bottom: 10px;
}

ul li{
border: 1px solid #2A3556;
float: left;
margin-right: 10px;
padding: 10px;
position: relative;
right: 12px;
margin-bottom:5px;
}
[/css]

This was your last step by styling the shoutbox.
Feel free to ask any questions if you didn't understand any point or if I missed something

Final Layout:

WebsTutorial Shoutbox

download

Jul 3, 2011

Centos Virtual hosting | Enabling multi apache hosting through 1 IP

OK, so now you decided to have one VPS, one IP, and many sites?
Cool, its very easy

First in the root folder, create your website folders, like you have 2 websites, so name them as site1.com and site2.com

So now you'll be having two website folders site1.com and site2.com in your root directory.

Now navigate to /etc/httpd/conf/ and start editing httpd.conf file,

At the end of the page, you will find a line, just like below

#NameVirtualHost *:80

Just uncomment that by removing # from it, So that it becomes like the below one:-

NameVirtualHost *:80

Then below their will be more lines which will be commented so uncomment them also, and make them look something like this

[php]
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/site1.com
ServerName site1.com
ErrorLog logs/site1-error_log
CustomLog logs/site1-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/site2.com
ServerName site2.com
ErrorLog logs/site2-error_log
CustomLog logs/site2-access_log common
</VirtualHost>
[/php]

Thats it, after this just restart your apache by the following command

[php]
service httpd restart
[/php]

Done,
You have successfully enabled multi-site or virtual hosting on apache Centos.

Jun 28, 2011

Configure VPS | Install LAMP on Centos VPS Part 1

Few days before when I had a VPS and needed to install wordpress, I found it very difficult because very few resources were available on google
So I am here now with my new post, On HOW TO INSTALL LAMP ON CENTOS
First buy an unmanaged VPS, and get ssh access
Then install Putty software in your computer, and start it
Then Follow the further steps
Start Putty software and then enter your IP for eg. 255.255.255.254
it will ask for username
generally its root and then enter the password
after that start typing the following code
IMPORTANT: IF YOU ARE NOT FAMILIAR WITH SSH COMMAND THEN BE CAREFUL. JUST COPY PASTE THE CODES.
First we will install yum

[php]
yum install -y yum-priorities

[/php]


After this we need to download updated repositories for php and mysql

[php]
wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

[/php]


Then we will extract that by following command
code:

[php]

rpm -Uvh epel-release-5-4.noarch.rpm

rpm -Uvh remi-release-5.rpm

[/php]

After This we give priorities to repo and exclude php mysql and phpmyadmin from repository
code:

[php]

vi /etc/yum.repos.d/CentOS-Base.repo
[/php]

After this a file will be opened, press I from the keyboard to edit it, and after editing press esc button and type :x to save it
code:

[php]

priority=1
exclude=*php* *mysql* *phpmyadmin*

[/php]

Copy paste this above code five times before the following lines

[php]
#released updates
#packages used/produced in the build but not released
#additional packages that may be useful
#additional packages that extend functionality of existing packages
Copy paste the next lines before this line #contrib - packages by Centos Users

[/php]



[php]
priority=2
exclude=*php* *mysql* *phpmyadmin*

[/php]

Just one line above priority=2 their will be a line enabled=0 just make that to 1
After this press esc and type :x to save the file
Now same thing for epel and remi repository


[php]

vi /etc/yum.repos.d/epel.repo
[/php]

Just Above this line [epel-debuginfo] insert this code(inserting and saving is been explained on top)
Code:

[php]

priority=3

[/php]

Now after saving that, we will now edit the same code with just different filename
Code:

[php]

vi /etc/yum.repos.d/remi.repo

[/php]

And now in this, just above [remi-test] line, change the value of enable=0 to 1
and paste this code exactly above the [remi-test] line
Code:

[php]

priority=3

[/php]

Save it and now we will install apache web server,


[php]
yum install -y httpd

[/php]

After this we will install php,

[php]

yum install php

[/php]

During the installation it will prompt you for importing GPG key for the new repositories, just type y and press enter, this will be come for two times
Now we will start the Apache server
Code:

[php]

/etc/init.d/httpd start

[/php]

Just to test our Apache and php, we will create a phpinfo file


[php]

vi /var/www/html/info.php

[/php]

A new file will be created, insert this code into it, and then save it



[php]

<?
phpinfo();
?>

[/php]

Now in your browser, enter this URL, http://your IP or localhost/info.php
Now a PHP INFO file should open, where it will display php and servers information
if not then restart the apache server,
To restart the server,



[php]

/etc/init.d/httpd restart

[/php]

Now we will install MySql



[php]

yum install -y mysql-server mysql php-mysql

[/php]

Now the next code is used to configure Apache , to start MySql when server is rebooted.


[php]
chkconfig httpd on
chkconfig mysqld on

[/php]

Fire up MySql Server


[php]

/etc/init.d/mysqld start

[/php]

Now we will set the root password of mysql

Code:

[php]

mysql -u root password password
[/php]

Here the second password is the actual root password, you can change it with your's own.

Now its time to do last job, thats installing phpmyadmin



[php]

yum install -y phpmyadmin
[/php]

Now we have to edit phpMyAdmin config file to avoid the Forbidden Access Error



[php]

vi /etc/httpd/conf.d/phpMyAdmin.conf

[/php]

Search for a line,

[php]

deny from all# deny from all

[/php]

Comment the above line just by inserting # before the line, so after that, it should look like the following,

# deny from all

Save it and restart the Apache server

Code:

[php]

/etc/init.d/httpd restart

[/php]

Done
Now goto the browser and type this URL
http://IP or localhost/phpmyadmin
Now the page will appear, just enter username as root and password as password

Check my next post, Part-2, on how to transfer a wordpress site from existing hosting to your newly configured VPS
Feel free to give suggestions.