<?php
require "db.php";
$data = $_POST;
if(isset($data['signup'])){
$error = array();
if(trim($data['firstname']) == ''){
$error[] = 'Enter Firstname';
}
if(trim($data['lastname']) == ''){
$error[] = 'Enter Lastname';
}
if(trim($data['login']) == ''){
$error[] = 'Enter Login';
}
if(trim($data['password']) == ''){
$error[] = 'Enter password';
}
if(trim($data['password_2']) == ''){
$error[] = 'Confirm password';
}
if(R::count('users', 'login = ?', array($data['login'])) > 0){
$error[] = 'Пользователь зарегистрирован';
}
if(trim($data['password']) != trim($data['password_2'])) {
$error[] = 'Wrong password';
}
if(empty($error)){
$user = R::dispense('users');
$user->firstname = $data['firstname'];
$user->lastname = $data['lastname'];
$user->login = $data['login'];
$user->password = password_hash($data['password'], PASSWORD_DEFAULT);
$user->ip = $_SERVER['REMOTE_ADDR'];
$user->d_date_reg = date("d");
$user->m_date_reg = date("m");
$user->y_date_reg = date("Y");
$user->h_time_reg = date("H");
$user->m_time_reg = date("i");
R::store($user);
}else{
echo "<div>".array_shift($error)."</div>";
}
}
//Авторизация
if(isset($data['signin'])){
$user = R::findOne('users', 'login = ?', array($data['login']));
if($user){
if(password_verify($data['password'], $user->password)){
$_SESSION['logged_user'] = $user;
}
}
}
?>
<?php if(isset($_SESSION['logged_user'])) : ?>
<meta http-equiv="refresh" content="0; URL='/user.php'" />
<?php else : ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>r</title>
</head>
<body>
<form action="/" method="POST">
<input type="test" name="login" placeholder="Login">
<input type="password" name="password" placeholder="Password">
<button type="submit" name="signup"> Sign in</button>
</form>
<br>
<form action="/" method="POST">
<input type="text" name="firstname" placeholder="Firstname">
<input type="text" name="lastname" placeholder="Lastname">
<input type="text" name="login" placeholder="Login">
<input type="password" name="password" placeholder="Password">
<input type="password" name="password_2" placeholder="Confirm password">
<button type="submit" name="signup">Sign up</button>
</form>
</body>
</html>
<?php endif; ?>