Tuesday, 26 August 2014

Difference between session and cookie in PHP

Session:

Session is a way to store data or information in the form of variable, which can be use across multiple pages or across the whole project. Unlike cookie, session store data on server site.
Before defining session variable it must be start on the top of the page or in header.
It can be start like this
<?php Session_start() ?>
After start variable can be define as.

<?php $_SESSION[‘data’]= ‘session value’; ?>

And if you want to print the session value then it could be written as
<?php echo “SESSION VALUE=”.$_SESSION[‘data’]; ?>

Cookie:

Cookie also known as web cookie or browser cookie is a small piece of data sent from a website and stored in a user web browser while the user is browsing the website. The main purpose of cookie is to store all activity of the user over that website(e,g history, usename and password etc).
We can set the cookie value and expiry time using the below php code
<?php
$expire =             time()+60*60*24*15  // expiry date set to 15 days
Setcookie(“name”,”something”,$expire);
?>
If you want to echo the cookie data then it will be

<?php echo $_COOKIE[‘name’]?>

Thursday, 21 August 2014

Difference Between CMS and Framework

CMS(CONTENT MANAGEMENT SYSTEM):

CMS stand for Content Management System. The basic purpose of CMS is to control whole website functionality from backend. CMS aim to avoid the need of hand coding, it allow the users to publishing, editing and modifying the content. CMS are often used to run websites containing blogs, news and shopings. Users can modify the functionality of CMS by adding different types of plugins and modules. There are many CMS written in php. 

FRAMWORK:

Building software application can be complex, an time consuming process , but using a framework we can reduce the complexity and time consumption(by reusing the most common and generic modules). Framework is a plateform for developing a software application, its provide a foundation on which software developers build a program.

Top 5 PHP CMS

CMS stand for Content Management System. The basic purpose of CMS is to control whole website functionality from backend. CMS aim to avoid the need of hand coding, it allow the users to publishing, editing and modifying the content. CMS are often used to run websites containing blogs, news and shopings. Users can modify the functionality of CMS by adding different types of plugins and modules. There are many CMS written in php. Here is the top 5 PHP CMS.

1. WORDPRESS:

Wordpress is a free open source content management system (CMS) and blogging tool written in PHP and mySQL. The first version of worpress was released on 27-may-2003 by its founders Matt Mullenweg and Mike Little. It is the most popular and mostly used CMS, there are thousand of themes and plugin are available which extend its functionality and make it more smarts, wordpress community is also very large and more helps are aviable on internet.

ADVANTAGES:

1. Community is very large
2. thousand of themes and plugins available
3. User friendly backend. 
4. easy to understand.


2. JOOMLA: 

Joomla is award winning CMS written in PHP using OOP(Object Oriented Programming) technique for publishing web contents. It build on Model-View-Controller (MVC) architecture. Joomla is the second most popular CMS after wordpress. Till February 2014, joomla has been download over 50 million times, it has over 7,700 free and commercial extensions available on his official extension directory, and more are available on other sites.

ADVANTAGES:

1. secure
2. more then 7,700 extensions are available
3. active user community and nice documentation

3. DRUPAL:

Drupal is the third most popular CMS which is also free and open source, its means you can download and install many times you want, and can spread around your friends and society.  Its also have a large and active community of  630,000+ users and developers. Many of heavy websites including New York Observer, Popular Science and MIT are running on Drupal. Taxanomy module is a great feature in drupal which allows multiple levels and types of categories.

ADVANTAGES:

1. active community support
2. more then 6000 modules extend the functionality
3. commercial support from different companies.

4. SILVERSTRIPE:

Silverstripe is an object oriented php5 content management system, it is also based on MVC architecture. It is popular due the following features.
1. out of box template, support responsive design.
2. rich text editing
3. can embed videos from sites like youtube, slideshare etc.
4. drag and drop tree-based navigation structure.

5. EXPRESSIONENGINE:

Expressionengine is also a CMS written in PHP. Its backend is so easy to understand , programming skill is not mandatory to handle the backend. The base code of  Expressionengine is build by using codeigniter , which is their own open-source php framework. Like other CMS’S Expressionengine did’t have much add-ons and plugins, only 22 add-ons modules and little over than 100 official plugins are available. 

Wednesday, 20 August 2014

Top 5 php framework


Building software application can be complex, an time consuming process , but using a framework we can reduce the complexity and time consumption(by reusing the most common and generic modules). Framework is a plateform for developing a software application, its provide a foundation on which software developers build a program. Here I am listing the top 5 php frameworks that mostly used by developers.

1. Laravel:

Laravel is a new born php framework, as you consider that version 1.0 of laravel was release a couple of years ago. It make a web development joy, by saving your time and effort. According to 2013 developers servey on php frameworks popularity, laravel is listed the most popular framework among all others.

Laravel Release History:



Laravel 1: release data: 9 june 2011 

Laravel 2: release data: 24 november 2011 

Laravel 3: release data: 22 february 2012 

Laravel 4: release data: 28 may 2013 

2. Codeigniter:

As a php web developer I used codeigniter as a framework, and I found it a very useful, simple and fast framework. I love to work in codeigniter because it is based on MVC pattern. In MVC we can kept all code in organized form and code reusability is also very easy. The libraries and helpers include in it make the tasks more simple and easy. 

Advantage:

     1. Simple folder structure.
     2. Easy to configure.
     3. Can construct your own cleaner URL structure.
     4. Simplicity very easy to use. Therefor community and         documentation is also very good.

3. Symfony:

Symfony is also an open source php framework, use to develop web applications. The main aim  of symfony is to cut down the development and maintenance time and to replace repetitive coding tasks. Symfony released his first version October 18, 2005 under the MIT license. It is also work on MVC pattern.


Advantage:


    1.  Easy configuration

    2. Wide set of command line tools
    3. Plugin creation is easy
    4. Extensive cache system

4 . CakePHP:

CakePHP is also an open source php framework. CakePHP development was started on april 2005, and it is now one of the best php framework.

Advantage:


    1.  Easy to configure and plugin your own implementations for external login services.
    2. Form helper use HTML5
    3. Default look taking advantage of css3 features.
    4. Easy session handling.

1.  Yii:

Yii is pronounced as yee or [ji:] and it is acroynym  for “Yes It Is!” yii is an open source , free web application development framework, it is a highly performance, component base framework written in PHP5. Yii is design to develop complex, highly traffic social and ecommerce based website. Yii project development was started on 1-january-2008 and its first version was released on 3-december-2008.

Advantage:

    1.  High performance platform is best for web2.0 application development.
    2. Reduce development time, flexible , modular, efficient    and extensible.
    3. Handling error is quite simple.
    4. Complaint with third party code.

Tuesday, 19 August 2014

Forget Password Code In php

Forget password is important for social and ecommerce website, every user is important for site. If any user forget his/her password then forget password is the only method through which he/she recover his password. In this tutorial I will show you how forget password link works.
In most websites forget password link is available on login page near login button. By Clicking on forgot password link you should redirect to forgot password page. See the image bellow.
After clicking on forgot Your Password link you should redirect to recover_password.php page.




Recover_password.php css code:

<style>
.login-box {
    background: none repeat scroll 0 0 #ffffff;
    height: auto;
    margin: 65px auto;
    max-width: 425px;
    padding: 25px;
    width: auto;
}
.login-box .form-line {
    float: left;
    margin: 13px 0 0;
    width: 100%;
}
.login-box label {
    color: #252525;
    float: left;
    font-size: 14px;
    margin: 17px 0 0;
    text-align: left;
    width: 150px;
}
label {
    display: block;
}

.login-box input[type="text"] {
    margin: 10px 0 0;
    padding: 8px;
    width: 250px;
}
.login-box input[type="password"] {
    margin: 10px 0 0;
    padding: 8px;
    width: 250px;
}

label, input, button, select, textarea {
    font-weight: normal;
    line-height: 20px;
}
label, select, button, input[type="button"], input[type="reset"], input[type="submit"], input[type="radio"], input[type="checkbox"] {
    cursor: pointer;
}
* {
    border: 0 none;
    padding: 0;
    vertical-align: baseline;
}

textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
    background-color: #ffffff;
    border: 1px solid #cccccc;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
    transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
}

.login-box .btn-inverse {
    float: left;
    margin: 9px 0 0 150px;
    padding: 10px 0;
    text-align: center;
    width: 132px;
}
button, html input[type="button"], input[type="reset"], input[type="submit"] {
    cursor: pointer;
}
.btn-inverse {
    background-color: #363636;
    background-image: linear-gradient(to bottom, #444444, #222222);
    background-repeat: repeat-x;
    border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
    color: #ffffff;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
}
.btn {
    -moz-border-bottom-colors: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
    border-image: none;
    border-radius: 4px;
    border-style: solid;
    border-width: 1px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    display: inline-block;
    font-size: 14px;
    line-height: 20px;
    vertical-align: middle;
}
.login-box .forgot-link a {
    color: #3a3a3a;
    text-decoration: none;
}
a {
    cursor: pointer;
}

 .login-box .forgot-link {
    color: #3a3a3a;
    float: left;
    font-size: 12px;
    margin: 16px 0 0 8px;
}
#body-login {
    /*background: url("img/login-body.png") repeat scroll 0 0 rgba(0, 0, 0, 0);
*/    font-family: Arial,Helvetica,sans-serif;
background-color:#999;
    font-size: 14px;
    line-height: 19px;
}

.login-container h1 {
    color: #3a3a3a;
    font-size: 38px;
    margin: 46px 0 0;
    text-align: center;
    text-transform: uppercase;
    width: 100%;
}
.error_msg{ color:red;}

</style> 

recover_password.php html Code:

<body id="body-login">
        <div class="login-header" >
            <div class="container">
                        <div class="logo"><img src="img/logo.png" alt="" title="" ></div>
            </div>
        </div>
        <div class="login-container">
            <div class="container">
                <h1>Recover Password</h1>
                <form method="post" action="email.php">
                <div class="login-box">
                        <div class="form-line">
                        <label>Username email*:</label>
                        <div class="error_msg">
                        </div>
                        <input type="email" value="" id="user_email" name="user_email" placeholder="Enter Your Valid Email" required="required">
                    </div>
                   
                    <div class="form-line">
                        <input name="" type="submit" class="btn btn-inverse" value="Send Password" >
                        
                    </div>
                    <div style="clear:both"></div>
                    <div style="clear:both"></div>
                </div>
                </form>
            </div>
        </div>
    </body>

Output of these code Produce:


In this for we use html5 email validation so if the email field is empty or email format is not correct then it will show a message like showing in the bellow screen image.



now if the user put correct email then it will go to the email.php page.

Email.php Code:


<?php 

session_start();
//include database connecton 
 include "connection.php";

$user_email   = $_POST['user_email'];

$query = mysqli_query($dbCon,"select * from users where user_email='".$user_email."'"); // query to fetch data from db
$result = mysqli_num_rows($query);
$data = mysqli_fetch_row($query);
if($result==1){  //if user exist put the name of user into session and redirect to home page
$email = $data['email']; 
        
$subject ="Password request"; 
$password = $data['password'];
// note: if you save the password in md5 then it will not return into normal form so you can send a temporary link to setup new password
        
$header = "from: phpplannet@gmail.com"; 
        
$body = "Your password is " . $password; 
mail ($email, $subject, $header, $body);  
$_SESSION['message'] = "Password has been send to your mail id";
header('Location:login.php');
}else{
$_SESSION['message'] = 'Email not found!';
header('Location:login.php');
}

?>

Connect.php  Code:

<?php 

$username = "root";  // in localhost username is root by default.
$password = ""; // in localhost passowd is blank by default.
$hostname = "localhost"; 
$db = "login"; 

//connection to the database
$dbCon = mysqli_connect($hostname, $username, $password,$db)
  or die("Unable to connect to MySQL");

?>

When user click on send password button, form will submit and on email.php php code will check that the inputted email is exist in database or not. If it exist in database it will send email to the user and show message on login page that email has been send to the email address, if not exist then it will give not found message.

Friday, 8 August 2014

Login and Logout Script in PHP

In this tutorial we learn how to login and logout page work in php, first we make a login page html design , after that apply validations, and then come into php and database stuff. For convenience I convert these process into steps. Login script protect your web page content and only registered users can see the web page content.

Step 1 html code:

<html>
<body id="body-login">
        <div class="login-header" >
            <div class="container">
            <div class="logo"><img src="img/logo.png" alt="" title="" ></div>
            </div>
        </div>
        <div class="login-container">
        <div class="container">
                <h1>Login</h1>
                <form method="post" action="action.php">
                <div class="login-box">
                <div class="form-line">
                    <label>Username *:</label>
                        <div class="error_msg">
<?php if(@$_SESSION['username_error']!=''){echo @$_SESSION['username_error'];}?>
                        <?php if(@$_SESSION['not_found']!=''){echo @$_SESSION['not_found'];}?>
                        
                        </div>
                        <input type="text" value="" id="username" name="username" placeholder="">
                    </div>
                    <div class="form-line">
                    <label>Password *:</label>
                        <div class="error_msg"><?php if(@$_SESSION['userpass_error']!=''){echo @$_SESSION['userpass_error'];}?></div>
                        <input type="password" value="" id="userpassword" name="userpassword" placeholder="">
                    </div>
                    <div class="form-line">
                    <input name="" type="submit" class="btn btn-inverse" value="Sign In" >
                        <div class="forgot-link">| <a href="#"> Forgot Your Password</a></div>
                    </div>
                    <div style="clear:both"></div>
                    <div style="clear:both"></div>
                </div>
                </form>
            </div>
        </div>
    </body>
</html>

Step 2 CSS of html code:

<style>
.login-box {
    background: none repeat scroll 0 0 #ffffff;
    height: auto;
    margin: 65px auto;
    max-width: 425px;
    padding: 25px;
    width: auto;
}
.login-box .form-line {
    float: left;
    margin: 13px 0 0;
    width: 100%;
}
.login-box label {
    color: #252525;
    float: left;
    font-size: 14px;
    margin: 17px 0 0;
    text-align: left;
    width: 150px;
}
label {
    display: block;
}

.login-box input[type="text"] {
    margin: 10px 0 0;
    padding: 8px;
    width: 250px;
}

label, input, button, select, textarea {
    font-weight: normal;
    line-height: 20px;
}
label, select, button, input[type="button"], input[type="reset"], input[type="submit"], input[type="radio"], input[type="checkbox"] {
    cursor: pointer;
}
* {
    border: 0 none;
    padding: 0;
    vertical-align: baseline;
}

textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
    background-color: #ffffff;
    border: 1px solid #cccccc;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
    transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
}

.login-box .btn-inverse {
    float: left;
    margin: 9px 0 0 150px;
    padding: 10px 0;
    text-align: center;
    width: 132px;
}
button, html input[type="button"], input[type="reset"], input[type="submit"] {
    cursor: pointer;
}
.btn-inverse {
    background-color: #363636;
    background-image: linear-gradient(to bottom, #444444, #222222);
    background-repeat: repeat-x;
    border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
    color: #ffffff;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
}
.btn {
    -moz-border-bottom-colors: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
    border-image: none;
    border-radius: 4px;
    border-style: solid;
    border-width: 1px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    display: inline-block;
    font-size: 14px;
    line-height: 20px;
    vertical-align: middle;
}
.login-box .forgot-link a {
    color: #3a3a3a;
    text-decoration: none;
}
a {
    cursor: pointer;
}

 .login-box .forgot-link {
    color: #3a3a3a;
    float: left;
    font-size: 12px;
    margin: 16px 0 0 8px;
}
#body-login {
    /*background: url("img/login-body.png") repeat scroll 0 0 rgba(0, 0, 0, 0);
*/    font-family: Arial,Helvetica,sans-serif;
background-color:#999;
    font-size: 14px;
    line-height: 19px;
}
.login-container h1 {
    color: #3a3a3a;
    font-size: 38px;
    margin: 46px 0 0;
    text-align: center;
    text-transform: uppercase;
    width: 100%;
}
.error_msg{ color:red;}
</style>    

Output of the above code:

Step 3 Making database:

I suppose you working on localhost. Open localhost/phpmyadmin, make a database named login
 within login make a table named users.

 Insert dummy user name and password, make sure that password should be convert into md5. I put admin,admin as username and password.

Step 4 making database connection:

Make a connection.php file to connect the php page with database.

Code:

<?php 
$username = "root";  // in localhost username is root by default.
$password = ""; // in localhost passowd is blank by default.
$hostname = "localhost"; 
$db = "login"; 
//connection to the database
$dbCon = mysqli_connect($hostname, $username, $password,$db)
  or die("Unable to connect to MySQL");
?>

Step 5 php code:

Creat action.php file in your file and copy the following code.

Code:

<?php 
session_start();
//include database connecton 
  include "connection.php";
$username   = $_POST['username'];
$password = $_POST['userpassword'];
if($username=='' && $password==''){  // if the feilds are blank then show message to user
$_SESSION['username_error'] = 'Username Required!';
$_SESSION['userpass_error'] = 'User Password Required!';
header('Location:login.php');
}else{
unset($_SESSION['username_error']);
unset($_SESSION['userpass_error']);
$query = mysqli_query($dbCon,"select * from users where username='".$username."' and password= '".md5($password)."'"); // query to fetch data from db
$result = mysqli_num_rows($query);
$data = mysqli_fetch_row($query);
if($result==1){  //if user exist put the name of user into session and redirect to home page
$_SESSION['user_id'] = $data[0];
$_SESSION['username'] = $data[1];
header('Location:home.php');
}else{
?> <h1 style="margin-left:200px; margin-top:200px;"><?php echo "Login Failed!";?></h1><?php 
}
}
?>

Code description:

First of all include the connection file to connect with database. Then checking is the username and password field is empty or not? If username or password field is empty it shows message.
If user give wrong credentials then it gives error message.

Message will be


The correct credential is admin, admin so if we give the username=admin and password = admin then it will redirect to the home page.


It will redirect to home page


Home.php code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
       <style>
.header{ width:1020px; height:160px; background-color:#ccc;}
.content{ width:1017px; height:1020px; border:2px solid #999999;}
.logout{ float:right; margin-right:20px; margin-top:20px;}
</style>
    </head>
    <body>
                <div class="header">
        <a class="logout" href="logout.php">Logout</a>
        </div>
        <div class="content">
        <h1>Welcome to Home page</h1>
        </div> 
    </body>
</html>

And when we hit the logout anchor it will go to the logout page where the sessions are unset and redirect to the login page again.

Logout.php code:

<?php 
unset($_SESSION['user_id']);
unset($_SESSION['username']);
header("location:login.php");
?>




Tuesday, 5 August 2014

PHP login Code


Login page is very important in any social or eCommerce website. So today I am going to show you how login page works.

In this tutorial I am going to show you the very simple form of login page. First design the html form for login username and password.

HTML login code:

<!DOCTYPE html>
<html>
<title>Login page tutorial</title>
<body>
<div style="margin-left:250px;">
<h1>User Login</h1>

<form method="post" action="action.php">
<label style="margin-right:10px;">User Name:</label>
<input style="margin-left:20px; width:250px;" type="text" name="username">
<div style="clear:both;">
<label style="margin-right:20px;">Password:</label>
<input style="margin-left:20px; width:250px;" type="password" name="userpassword">
<div style="clear:both">
<input style="margin-top:20px; margin-left:107px;" type="submit"></input>
</form>
</div>
</body>
</html>

Generated Output:


the design section for login page is completed, now it time to write php code for login. For that first of all make a database with name as login under login make a table give name it to users. Put a username and password in users table.

Action.php page code:

<?php
 //db connection start
$dbCon = mysql_connect("localhost","root","") or die ('MySQL connect failed. ' . mysql_error());

mysql_select_db("login") or die('Cannot select database. ' . mysql_error());

 //db connection end

$username   = $_POST['username'];

$password = $_POST['userpassword'];

$query = mysql_query("select * from users where username='".$username."' and password= '".$password."'"); // query to fetch data from db

$result = mysql_num_rows($query);

if($result==1){  //login condition
?> <h1 style="margin-left:200px; margin-top:200px;"><?php echo "Login Success!";?></h1><?php 
}else{
?> <h1 style="margin-left:200px; margin-top:200px;"><?php echo "Login Failed!";?></h1><?php 
}

?>


Description of the Code:

I put username =admin & password=admin in the users table so if the username and password is equal to admin then the output will be.


if the username and password is wrong then the output will be.


Note: validations are not include in this tutorial, different articles about validations will be published soon.
If you like this article then show your interest by commenting or like our facebook page.