使用MVC的PHP登录/注册系统

时间:2016-12-03 23:03:12

标签: php model-view-controller login

我正在制作一个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结构的最佳方法是什么?我是否需要制作全新的代码或者我可以使用其中的一些代码?提前谢谢。

0 个答案:

没有答案