Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Banco.class.php ------------------------
- <?php
- abstract class Banco{
- protected $conexao;
- protected $user='root';
- protected $pass='xxxxx';
- protected $dsn='mysql:host=localhost;dbname=loja';
- function __construct(){
- $this->conexao = new PDO($this->dsn,$this->user,$this->pass);
- }
- function inserir($sql){
- return $this->conexao->exec($sql);
- }
- function listar($sql){
- return $this->conexao->query($sql);
- }
- }
- ?>
- //----------------------------------------
- // Usuario.class.php ---------------------
- <?php
- class Usuario extends Banco{
- private $nome;
- private $telefone;
- private $email;
- private $senha;
- private $id;
- public function exibir($campos='*',$where=null){
- $sql = "SELECT $campos FROM usuario $where";
- return $this->listar($sql);
- }
- public function login($usuario='',$senha=''){
- $where = "WHERE email='$usuario' AND senha='$senha'";
- $validar = true;
- foreach($this->exibir('*',$where) as $usuario){
- $_SESSION['usuario'] = $usuario;
- $validar = false;
- header('location: listar_livro.php');
- exit;
- }
- if($validar){
- $msg = 'Usuário e/ou senha errado(s)';
- header('location: login.php?msg='.$msg);
- }
- }
- }
- ?>
- //----------------------------------------
- // login.php ----------------------------
- <form action='logar.php' method='post'>
- e-mail: <input type='text' name='email'/>
- <br/>
- Senha: <input type='password' name='senha'/>
- <input type='submit' value='login'/>
- </form>
- <?php if($_GET['msg']){ echo $_GET['msg'];}?>
- //---------------------------------------
- // logar.php ----------------------------
- <?php
- require_once 'Banco.class.php';
- require_once 'Usuario.class.php';
- session_start();
- $u = new Usuario();
- $u->login($_POST['email'],$_POST['senha']);
- ?>
- //---------------------------------------
- //listar_livro.php ----------------------
- <?php
- require_once 'Banco.class.php';
- require_once 'Usuario.class.php';
- echo "<h1> Ola ".$_SESSION['usuario']."vc esta LOGADO!!!!</h1>";
- echo "<table align='center'>";
- $sql='SELECT * FROM loja.livro';
- $c = Usuario();
- foreach($c->listar($sql) as $livro){
- echo '<tr><td>Titulo: '.$livro['titulo'].'</td><td>Autor: '.$livro['autor'].'</td><td>Preco: '.$livro['valor'].'</td></tr>';
- }
- echo "</table>";
- ?>
- //--------------------------------------
- // meu banco de dados: -----------------
- select * from loja.usuario;
- id - email - senha
- 1 - x@x.x - 123
- //--------------------------------------
- ---------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement