Let's break it down. Consider the following folder structure:
/projeto (raiz)
Modelo
Conexao.php
Usuario.php
UsuarioCRUD.php
Visao
Login.php
Controle
Controle.php
UsuarioControle.php
You will split your code between the files in the hierarchy above. Then it stays:
Template
Template \ Connection.php
<?php
class Conexao{
private $usuario = 'root';
private $senha = '';
private $host = 'localhost';
private $nomeBanco = 'meudb';
private $conexao = null;
public function __construct(){
$this->conexao = mysqli_connect($this->host, $this->usuario,$this->senha);
$db = mysqli_select_db($connect, $this->nomeBanco);
}
public function getConexao(){
return $this->conexao;
}
}
?>
Template \ User.php
<?php
class Usuario{
private nome = null;
private $senha = null;
private $email = null;
public function __construct($nome = null, $senha = null, $email = null){
$this->nome = nome;
$this->senha = senha;
$this->email = email;
}
public function getNome(){
return $this->nome;
}
public function getSenha(){
return $this->senha;
}
public function getEmail(){
return $this->email;
}
}
?>
Template \ UserCRUD.php
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Usuario.php';
class UsuarioCRUD{
private conexao = null;
public function __construct($conexao = null){
$this->conexao = conexao;
}
/*
@return boolean | Usuario
*/
public function usuarioExiste(Usuario $usuario){
$verifica = mysqli_query($this->conexao,
"SELECT idcliente, nome, email, senha FROM clientes WHERE email = '"
. $usuario->getEmail() ."' AND senha = '". $usuario->getSenha() . "'") or die("erro ao selecionar");
$row = mysqli_fetch_assoc($verifica);
if (mysqli_num_rows($verifica)<=0){
return false;
}
return new Usuario($row['nome'], $row['senha'], $row['email']);
}
}
?>
Vision \ Login.php
<html>
<head>
<head>
<body>
<form action="/Controle.php" method="post">
<input name="email" type="text">
<input name="senha" type="password">
<input name="entrar" type="submit">
<form>
<body>
</html>
Control \ Control.php
<?php
session_start();
require_once __DIR__ . DIRECTORY_SEPARATOR . 'UsuarioControle.php';
if(isset($_POST['entrar'])){
(new UsuarioControle())->logar($_POST);
}else if(isset($_GET['login'])){
(new UsuarioControle())->login();
}
?>
Control \ User Control.php
<?php
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Modelo' . DIRECTORY_SEPARATOR . 'UsuarioCRUD.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Modelo' . DIRECTORY_SEPARATOR . 'Usuario.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Modelo' . DIRECTORY_SEPARATOR . 'Conexao.php';
class UsuarioControle{
private $usuarioCRUD = null;
public function __construct(){
$usuarioCRUD = new UsuarioCRUD((new Conexao())->getConexao());
}
//ao submeter o formulario
public function _logar($dados){
$usuario = $usuarioCRUD->usuarioExiste(new Usuario($dados['nome'], $dados['senha'], $dados['email']));
if($status === false){
echo 'Usuario inexistente';
}else{
if (!session_id())
$_SESSION['logon'] = true;
$_SESSION['user'] = $usuario->getNome();
setcookie('usuario',$usuario->getNome(),0,"/");
header("Location:../View/adm.php");
}
}
die();
}
//ao chamar o formulario, algo como http://localhost/?login
public function login(){
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Visao' . DIRECTORY_SEPARATOR . 'Login.php';
}
}
?>
This should help.