使用MVC的PHP登录/寄存器系统



我正在制作MVC PHP登录/注册系统,最初是我的基本登录:

index.php

  <?php
  session_start();
  if (isset($_POST['submit'])) { 
  include("config.php");
  $myusername = strip_tags($_POST['username']);
  $mypassword = strip_tags($_POST['password']); 
  $sql = "SELECT id, email, password FROM users WHERE email = '$myusername'          
  and password = '$mypassword'";
  $query = mysqli_query($db,$sql);
  if ($query) {
     $row = mysqli_fetch_row($query);
     $dbUsername = $row[1];
     $dbPassword = $row[2];
  }
  if ($myusername == $dbUsername && $mypassword == $dbPassword){
     $_SESSION['email'] = $username;
     header('Location: logged.php');
  } 
  else {
     header('Location: incorrect.html');
  } 
  if ($myusername == '' && $mypassword == ''){
     header('Location: incorrect.html');
}
}
if (isset($_POST['submit1'])) {
    include("config.php"); 
    $name = strip_tags($_POST['name']);
    $username = strip_tags($_POST['username1']);
    $password = strip_tags($_POST['password1']);
$sql = "INSERT INTO users (name, email, password) VALUES ('$name',  
'$username', '$password')";
 if ($name == '' || $username == '' || $password == ''){
     header('Location: invalid.html');
   }
else if (mysqli_query($db, $sql)) 
   mysqli_close($db);
}
?>
<form id="form1" method = "post" action = "index.php">
<input type="text" id="email" name="username" placeholder="Email">
<input type="password" id="password" name="password" placeholder="Password">
<button id="login" name="submit"  >LOGIN</button>
<button id="login1" name="submit1" style='display: none;' >Sign Up</button>
<a href="http://www.webmd.com/brain/memory-loss#1" id="forgot">Forgot?</a>
</form>

config.php:

<?php
define('DB_SERVER', 'localhost:3306');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'k');

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
?>

将该系统变成MVC结构的最佳方法是什么?我需要制作全新的代码还是可以使用其中的一些?预先感谢。

首先,您必须经过并了解PHP MVC框架,例如CodeIgniter,yii等,文档/教程。

此CodeIgniter Documentaion可以帮助您知道如何创建控制器,操作和访问它。http://www.codeigniter.com/user_guide/general/controllers.html

最新更新