Olá pessoal estava aqui brincando com o php e resolvi postar, é um sistema bem simples mas que da para o pessoal que estar iniciando em POO ter uma ideia como funciona.
Model/Conexao.interface.php
Código:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Valdir
*/
interface ConexaoInterface{
public function connectar();
public function selecionarDB();
public function executarSQL();
}
/Model/ConexaoMySQL.class.php
Código:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of ConexaoMySQLModel
*
* @author Valdir
*/
include 'Conexao.interface.php';
class ConexaoMySQL implements ConexaoInterface{
//put your code here
private $host = "localhost";
private $usuario = "root";
private $senha = "";
private $db = "";
private $debug = false;
private $sql;
function __construct() {
$this->connectar();
$this->selecionarDB();
}
public function __set($atributo, $valor) {
$this->$atributo = $valor;
}
public function __get($atributo) {
return $this->$atributo;
}
private function connectar(){
$result = mysql_connect($this->host,$this->usuario,$this->senha)
or die (mysql_error());
return $result;
}
private function selecionarDB(){
$result = mysql_select_db($this->db)
or die (mysql_error());
return $result;
}
public function executarSQL(){
$return = mysql_query($this->sql)
or die (mysql_error());
if($this->debug){
echo "[DEBUG] : $this->sql";
}
}
}
Model/Usuario.class.php
Código:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of UsuarioModel
*
* @author Valdir
*/
include 'ConexaoMySQL.class.php';
class Usuario {
//put your code here
private $login;
private $senha;
public function validarLogin($login,$senha){
$conexao = new ConexaoMySQl();
$conexao->sql = "select * from usuario where login = '$login' and senha = '$senha'";
$consulta = $conexao->executar();
$linhas = mysql_num_rows($consulta);
if($linhas > 0){
return true;
}
else{
return false;
}
}
}
View/LoginView.php
Código:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form name="login" method="post" action="../Control/UsuarioControl.php">
<label>Login</label>
<input type="text" name="login" required=""><br>
<label>Senha</label>
<input type="password" name="senha" required=""><br>
<input type="submit" value="Entrar" name="enviar">
<input type="reset" value="Cancelar" name="cancelar">
<input type="hidden" name="control" value="logar">
</form>
</body>
</html>
Control/UsuarioControl.php
Código:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
include '../Model/Usuario.class.php';
if (isset($_POST['control'])) {
$controle = $_POST['control'];
switch ($controle) {
case "logar":
$u = new Usuario();
$result = $u->validarLogin($_POST['login'], $_POST['senha']);
if ($result) {
echo "LOGOU";
} else {
echo "NÃO LOGOU";
}
break;
}
}
É isso ai pessoal como já disse é bem simples fiz em 15 minutos agora no almoço da para melhorar muito como por exemplo uso de session e etc mais estar ai pra quem quiser ^^