Как се прави вход форма с PHP


GitHub: Вземи кода
YouTube: PHP Уроци: Как се прави вход система с PHP


login.php

session_start();
define("DB_HOST", "localhost");
define("DB_NAME", "tut_db");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");

include('Database.php');

$db = new Database();

    if(!empty($_POST['nickname']) && !empty($_POST['password'])){

        $nickname = $_POST['nickname'];
        $password = $_POST['password'];

        $md5_password = md5($password);
        
        $userExists = $db->selectOne("SELECT id FROM users WHERE nickname = '$nickname' AND `password` = '$md5_password'");
        if(empty($userExists)){
            echo 'User does not exists';
            return;
        }

        $_SESSION['user_id'] = $userExists['id'];
        echo '<a href="profile.php">Profile</a>';

    }
    <form method="POST">
        <input type="text" name="nickname" placeholder="Nickname"><br>
        <input type="password" name="password" placeholder="Password"><br>
        <input type="submit" name="login" value="Login"><br>
    </form>


register.php

define("DB_HOST", "localhost");
define("DB_NAME", "tut_db");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");

include('Database.php');

$db = new Database();


    if(!empty($_POST['nickname']) && !empty($_POST['password']) && !empty($_POST['re_password']) && !empty($_POST['email'])){
        $nickname = $_POST['nickname'];
        $password = $_POST['password'];
        $re_password = $_POST['re_password'];
        $email = $_POST['email'];

        if($password != $re_password){
            echo 'Password does not match';
            return;
        }
      
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo 'Email is not valid';
            return;   
        }

        $userExists = $db->selectOne("SELECT id FROM users WHERE nickname = '$nickname' OR email = '$email'");
        if(!empty($userExists)){
            echo 'User already exists';
            return;
        }
        
        $md5_password = md5($password);        
        $insertData = array(
            'nickname' => $nickname,
            'password' => $md5_password,
            'email' => $email,
        );
        echo 'Successful!';
        $db->insert('users', $insertData);

    }
    <form method="POST">
        <input type="text" name="nickname" placeholder="Nickname"><br>
        <input type="password" name="password" placeholder="Password"><br>
        <input type="password" name="re_password" placeholder="Re-Password"><br>
        <input type="email" name="email" placeholder="Email"><br>
        <input type="submit" name="register" value="Register"><br>
    </form>


profile.php

session_start();
define("DB_HOST", "localhost");
define("DB_NAME", "tut_db");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "");

include('Database.php');

$db = new Database();

if(empty($_SESSION['user_id'])){
    echo 'You are not logged - <a href="login.php">Login</a>';
    return;
}


$user = $db->selectOne("SELECT nickname, email FROM users WHERE id = '".$_SESSION['user_id']."'");

echo 'Здравей '. $user['nickname'].'<br>';
echo '<a href="logout.php">Logout</a>';


logout.php

session_start();
session_destroy();
header("Location: login.php");


Database.php


    class Database {
        public function __construct() {
            $this->db = new PDO("mysql:host=".DB_HOST."; dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD);
        }

        public function query($sql){
        	$query = $this->db->prepare($sql);
        	$query->execute();

        }

        public function selectOne($sql){
            $query = $this->db->prepare($sql);
            $query->execute();
            return $query->fetch();          
        }

        public function selectAll($sql){
            $query = $this->db->prepare($sql);
            $query->execute();     
            return $query->fetchAll();       
        }

        public function update($table, $data, $where){
            $set_data = '';
            $where_data = '';
            foreach($data as $key => $value) {
                $set_data .= ", {$key} = '{$value}'";
            }
            foreach($where as $key => $value) {
                $where_data .= "AND {$key} = '{$value}'";
            }
            $set_data = ltrim($set_data,',');
            $where_data = ltrim($where_data,'AND') ;
            
            $this->db->query("UPDATE $table SET {$set_data} WHERE {$where_data}");
            return true;
        }

        public function insert($table, $where){
            $set = '';
            $i = 0;
            foreach($where as $key => $value) {
                if($i == 0){
                    $set .= "SET {$key} = '{$value}'";
                }else{
                    $set .= ", {$key} = '{$value}'";
                }
                $i++;
            }
            $set = ltrim($set,',');
            
            $this->db->query("INSERT INTO $table {$set}");
            $id = $this->db->lastInsertId();
            return $id;
        }

        public function delete($table, $data){
            foreach($data as $key => $value) {
                $set_data .= ", {$key} = '{$value}'";
            }

            $set_data = ltrim($set_data,',');

            $this->db->query("DELETE FROM $table WHERE {$set_data}");

        }

    }	


users.sql

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `nickname` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);
  
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

Related Posts

На живо
CURRENTLY OFFLINE