infolink

Friday 30 March 2012

Simple MVC base Login Form in PHP

This tutorial is about MVC in PHP Login form. MVC is a Model View Controller pattern. This pattern is very commonly used today in the programming.
Model: Model manage the data, it stores and retrieve the data usually from the data base.
View: View represent the data in the required format.
Controller: it is handle the Model and View layer. Controller take the request from the client send to Model for  operation and then send the required output to View.
Now let start to create the simple login form in php using MVC pattern.
------------------------------------------------------

Step 1:

Make a new folder. In this new folder make 3 another new folders with the name controller, model, view respectively and also create a PHP page with a name index.php.



  • new folder
                  -  controller
                  -  model

                  -  view
                  -  index.php
------------------------------------------------------

Step 2:

In the controller folder create a PHP page with the name Controller. and write the following code:

<?php
include_once("model/Model.php");

class Controller {
public $model;
public function __construct()  
    {  
        $this->model = new Model();

    } 
public function invoke()
{
$reslt = $this->model->getlogin();     // it call the getlogin() function of model class and store the return value of this function into the reslt variable.
if($reslt == 'login')
{
include 'view/Afterlogin.php';
}
else
{
include 'view/login.php';
}
}
}

?>
------------------------------------------------------

Step 3:

In the model folder create a PHP page with the name Model. and write the following code:

<?php

include_once("model/Book.php");

class Model {

public function getlogin()
{
// here goes some hardcoded values to simulate the database
if(isset($_REQUEST['username']) && isset($_REQUEST['password'])){
if($_REQUEST['username']=='admin' && $_REQUEST['password']=='admin'){
return 'login';
}
                        else{
return 'invalid user';
}
}
}
}

?>
------------------------------------------------------

Step 4:

In the view folder create two(2) PHP page with the name login.php, Afterlogin.php respectively. and write the following code:
  • login.php
<html>
<head></head>

<body>
<?php 
echo $reslt;

?>
<form action="" method="POST">
<p>
<label>Username</label>
<input id="username" value="" name="username" type="text" required="required" /><br>
</p>

<p>
<label>Password</label>
<input id="password" name="password" type="password" required="required" />
</p>
 <br />
<p>

  <button type="submit" class="green big" name="submit"><span>Login</span></button> <button type="reset" class="grey big"><span>Cancle</span></button>
</p>
</form>

</body>
</html>

  • Afterlogin.php

<html>
<head></head>

<body>

<?php 

echo $reslt;

?>

</body>
</html>

------------------------------------------------------

Step 5:

Open the index.php file and write the following code:

<?php 
include_once("controller/Controller.php");

$controller = new Controller();
$controller->invoke();

?>
------------------------------------------------------
Now try your self. It is too simple.

49 comments:

  1. thank you very muuch...this is what i want...simple and easy to understand!

    ReplyDelete
    Replies
    1. I can easily understand this, too much help from thanks Khawar.

      Delete
  2. Hi!

    what is the use of " include_once("model/Book.php"); " in Model.php
    as i am getting Warning message for this line.

    Onkar

    ReplyDelete
    Replies
    1. Which will included once

      Delete
    2. hi ahmad this example which your looking for taken from another example and copy pasted so it's giving error.. now tell me in whole program is there any book.php page exists??????????? answer is NO so in model folder there should be another file calld (model/...... .php) thanks

      Delete
  3. Thanks for nice code my question also same like Onkar Kubal what is use of " include_once("model/Book.php"); " i am not understand i have comment that and using.... but plz can u explain...

    ReplyDelete
    Replies
    1. hi manoj this example which your looking for taken from another example and copy pasted so it's giving error.. now tell me in whole program is there any book.php page exists??????????? answer is NO so in model folder there should be another file calld (model/...... .php) thanks

      Delete
    2. what will b script for:- (model/..... .php)

      Delete
    3. what will b script for:- (model/..... .php)

      Delete
  4. hey I was looking this code. Thanks for share it.

    Hire PHP Programmer

    ReplyDelete
  5. Hi Malik. I'm starting to program in PHP using MVC architecture.
    Which is your twitter?

    Your tutorial is very good. Thank you.

    @codigogm

    ReplyDelete
  6. really helpful for beginners
    thanks a lot :)

    ReplyDelete
  7. It was very useful for me. Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.
    Web development Company

    ReplyDelete
  8. Too much thanks ... it is really very good tutorial for the beginners.....

    ReplyDelete
  9. Malik i've a doubt ,how to make use of database in login form

    ReplyDelete
  10. thank,but i do not understand "include_once("model/Book.php")".
    i do not find file Book.php,

    ReplyDelete
    Replies
    1. hi Phong Bui Tu this example which your looking for taken from another example and copy pasted so it's giving error.. now tell me in whole program is there any book.php page exists??????????? answer is NO so in model folder there should be another file calld (model/...... .php) thanks

      Delete
  11. Could you implement further with session,cache or so called handling? I have followed your concept in my website,in IE and FF i have no issue after logout as I click back button and it redirect me to login page (i have destroy the session the moment I logout). In Chrome,after logout,it will show me the secure page again when I click back button

    ReplyDelete
  12. Will you please implement a registration form with validation. Thanks in advance

    ReplyDelete
  13. thnak you good post for me

    www.saturnvpn.com

    ReplyDelete
  14. really use full tutorial to begin with mvc .thanks.

    remove code include 'view/Book.php';

    ReplyDelete
    Replies
    1. hi viyukam this example which your looking for taken from another example and copy pasted so it's giving error.. now tell me in whole program is there any book.php page exists??????????? answer is NO so in model folder there should be another file calld (model/...... .php) thanks

      Delete
  15. hello sir ur code is good but if i remove the getlogin() functio or model class from model then also is shows the same result u jst check it n let me konw asap....

    ReplyDelete
  16. Me too show the same error in Book.php..But i gave Model.php instead of Book.php thn it works fine...

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Useful example. Thank you.
    I wonder if you can give an advanced example about MVC with database (MySQL), please!
    Thank you again.

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. If you're having problems concerning the "model/Book.php" then change it to "model/Model.php" or simply comment this line. I don't see the logic of changing it to Model.php.. Also I've noticed an error getlogin() function is called from controller whereas in model function login() is created. Solution either change getlogin() to login() or vice versa.

    ReplyDelete
  21. Hai,
    It was good. but what is the use of " include_once("model/Book.php"); " in Model.php
    as i am getting Warning message for this line.

    ReplyDelete
  22. Thanks for sharing .

    I am completely new for mvc and found lots of tutorials on web but i was confused in bootstrap , router , .htaccess template etc. finally i am clear how it works . Thanks !!!

    ReplyDelete
  23. Thank u sir,
    i want one more example for user registration for MVC

    ReplyDelete
  24. Jeezus H. - you Indians need to clean up your English and the code author NEEDS to double check his code here.

    The line that includes "Book.php" should be deleted. It is an error on authors part since he did NOT check his simplistic code example.

    ReplyDelete
    Replies
    1. u haave to improve ur english. Indian rocks U socks. F**k **f.

      Delete
  25. Nice example - however it does not have anything to do with MVC. In MVC, Controller will change the state of Model and Model only. View will then read the model and decide how to render the model for user.

    In your example, controller works as a mediator between the model and the view; You store a result of model method and then let it go to view through controller and that is not MVC. It's MVP (model/view/presenter)

    Also another note, your Model could/should be enviroment agnostic - thus controller should give the auth details (username and password) using the login() method, in controller:
    $model->login($_POST['username'],$_POST['password']) etc (and clean every single user input, but that's another thing)

    In a nutshell: Controller reads the request and checks what user wants to do by calling proper methods on Model layer. View will then read the Model (they share the model, therefore Controller will not also create models using new, you should pass the model both to Controller and View Classes (View != just a template, View USES templates) and render stuff.

    ReplyDelete
  26. This is a amazing inspiring article. now i am pretty much pleased Making use of your good work. anyone put this season very helpful information. Keep The idea up. Keep blogging. Looking for you to reading your subsequently post. Outbound Calling App

    ReplyDelete
  27. I truly liked looking over this submit, I ended up being just wanting to know would you trade included blogs. Thanks pertaining to expressing your website with other folks. You really share precious information.Business Live Updates

    ReplyDelete
  28. simple and clear .. very helpful for starters ..
    Thank You (y)

    ReplyDelete
  29. thank you .simple and clear explanation

    ReplyDelete
  30. Very useful Codes been imparted by you. I like your strategy and the way you organize things in an informative way. Thanks for putting your time to explain the things to us.Tutor services are also provided by TheTuitionTeacher in Lucklnow.
    Home Tutors in Lucknow | Home Tuition Service

    ReplyDelete
  31. This article turned out to be genuinely beneficial for me and I am certain that it might be beneficial for numerous other visitors as well. I admire you for the share. Continue blogging.
    Website Design Agency | Website design company

    ReplyDelete
  32. nice article .thank you for sharing useful post.
    web programming tutorial
    welookups

    ReplyDelete
  33. Thanks for sharing this valuable information and we collected some information from this post.

    Corporate training in Machine learning

    ReplyDelete
  34. I am so happy after reading your blog. It’s very useful blog for us.

    Corporate training in Machine learning

    ReplyDelete
  35. Nice Post, Keep posting in the future as well, Now i am going to tell you about Training icon which is a leading It training institute in Delhi NCR so if you are looking for any type of training ,you can visit.
    Trainingicon.in-Learn HTML,CSS and Jquery

    Trainingicon.in- PHP training

    Trainingicon.in- Data Analytics Course

    Trainingicon.in- R Programming Course

    Trainingicon.in- MIS Programming Course

    Trainingicon.in- WordPress Programming Course

    Trainingicon.in- SAS Programming Course

    ReplyDelete