Skip to content

Instantly share code, notes, and snippets.

@tabekg
Created February 28, 2019 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabekg/503d9e52c1b12328c4f8210d0bcf3352 to your computer and use it in GitHub Desktop.
Save tabekg/503d9e52c1b12328c4f8210d0bcf3352 to your computer and use it in GitHub Desktop.
Source code of the lesson https://youtu.be/UW6wm3_zLVA
<?php
session_start();
$db = new mysqli('localhost', 'root', '', 'youtube'); // базаны тууралаңыз!!!
function getSession($name, $delete = true){
$data = $_SESSION[$name];
if ($delete) $_SESSION[$name] = NULL;
return $data;
}
function checkUser(){
global $db;
$query = $db->query('SELECT COUNT(*) AS `c` FROM `users` WHERE `login`="' . $_COOKIE['login'] . '" and `password`="' . $_COOKIE['password'] . '"')->fetch_assoc();
return $query['c'] > 0;
}
<?php
require_once 'config.php';
if (checkUser()){
$login = $_COOKIE['login'];
} else header('Location: login.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Кош келиңиз!</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2>Кош келиңиз, <?=$login?>!</h2>
<div><a href="logout.php">Чыгуу</a></div>
</body>
</html>
<?php
require_once 'config.php';
if (checkUser()) header('Location: index.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$login = $_POST['login'];
$password = $_POST['password'];
if ($login != '' && $password != ''){
$query = $db->query('SELECT COUNT(*) AS `c` FROM `users` WHERE `login`="' . $login . '"')->fetch_assoc();
if ($query['c'] > 0){
$query = $db->query('SELECT `password` FROM `users` WHERE `login`="' . $login . '"')->fetch_assoc();
if (password_verify($password, $query['password'])){
setcookie('login', $login);
setcookie('password', $query['password']);
header('Location: index.php');
} else {
$_SESSION['error'] = 'Пароль туура эмес!';
}
} else {
$_SESSION['error'] = 'Логин туура эмес!';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Кирүү</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2>Кирүү</h2>
<?php
if (getSession('error', false) != '') echo '<div class="error">' . getSession('error') . '</div>';
?>
<form method="post">
<div><input type="text" name="login" placeholder="Логин" required></div>
<div><input type="password" name="password" placeholder="Пароль" required></div>
<div><input type="submit" value="Кирүү" /></div>
<div><a href="register.php">Катталуу</a></div>
</form>
</body>
</html>
<?php
setcookie('login', '');
setcookie('password', '');
header('Location: login.php');
<?php
require_once 'config.php';
if (checkUser()) header('Location: index.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$login = $_POST['login'];
$password = $_POST['password'];
if ($login != '' && $password != ''){
$query = $db->query('SELECT COUNT(*) AS `c` FROM `users` WHERE `login`="' . $login . '"')->fetch_assoc();
if ($query['c'] > 0){
$_SESSION['error'] = 'Мындай логин бош эмес!';
} else {
if ($db->query('INSERT INTO `users` SET `login`="' . $login . '", `password`="' . password_hash($password, PASSWORD_DEFAULT) . '"')){
$_SESSION['success'] = 'Ийгиликтүү катталдыңыз!';
} else {
$_SESSION['error'] = 'Белгисиз катачылык!';
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Катталуу</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h2>Катталуу</h2>
<?php
if (getSession('error', false) != '') echo '<div class="error">' . getSession('error') . '</div>';
if (getSession('success', false) != '') echo '<div class="success">' . getSession('success') . '</div>';
?>
<form method="post">
<div><input type="text" name="login" placeholder="Логин" required></div>
<div><input type="password" name="password" placeholder="Пароль" required></div>
<div><input type="submit" value="Катталуу" /></div>
<div><a href="login.php">Кирүү</a></div>
</form>
</body>
</html>
body {
text-align: center;
}
input {
padding: 10px;
margin: 10px;
}
.error {
background-color: #f00;
color: #fff;
padding: 5px;
font-size: 14px;
}
.success {
background-color: #0f0;
color: #fff;
padding: 5px;
font-size: 14px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment