Reputation:
hello this is my first project and after alot of searching i cant set cookie please help me there are my codes what should i have to add into it?
it is create user table
php :
"CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`email` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`password` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`permission` enum('user','admin') COLLATE utf8_persian_ci NOT NULL DEFAULT 'user',
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;",
class auth contains function login logout , checkadmin and register
php:
<?php
namespace AdminDashboard;
require_once(realpath(dirname(__FILE__) . "/DataBase.php"));
use DataBase\DataBase;
class Auth
{
function __construct()
{
if(session_status() == PHP_SESSION_NONE){
session_start();
}
}
public function login()
{
require_once(realpath(dirname(__FILE__) . "/../template/auth/login.php"));
}
public function checkLogin($request)
{
if (empty($request['email']) || empty($request['password'])) {
$this->redirectBack();
} else {
$db = new DataBase();
$user = $db->select("SELECT * FROM `users` WHERE (`email` = ?); ", [$request['email']])->fetch();
if ($user != null) {
if (password_verify($request['password'], $user['password'])) {
$_SESSION['user'] = $user['id'];
$this->redirect('admin');
} else {
$this->redirectBack();
}
} else {
$this->redirectBack();
}
}
}
public function register()
{
require_once(realpath(dirname(__FILE__) . "/../template/auth/register.php"));
}
public function registerStore($request)
{
if (empty($request['email']) || empty($request['password'])) {
$this->redirectBack();
} else if (strlen($request['password'] < 8)) {
$this->redirectBack();
} else if (!filter_var($request['email'], FILTER_VALIDATE_EMAIL)) {
$this->redirectBack();
} else {
$db = new DataBase();
$user = $db->select("SELECT * FROM `users` WHERE (`email` = ?); ", [$request['email']])->fetch();
if ($user != null) {
$this->redirectBack();
} else {
$request['password'] = $this->hash($request['password']);
$db->insert('users', array_keys($request), $request);
$this->redirect('login');
}
}
}
public function logout()
{
if (isset($_SESSION['user'])) {
unset($_SESSION['user']);
session_destroy();
}
$this->redirectBack();
}
public function checkAdmin()
{
if (isset($_SESSION['user'])) {
$db = new DataBase();
$user = $db->select("SELECT * FROM `users` WHERE `id` = ? ; ", [$_SESSION['user']])->fetch();
if ($user != null) {
if ($user['permission'] != 'admin') {
$this->redirect('home');
}
} else {
$this->redirect('home');
}
} else {
$this->redirect('home');
}
}
protected function redirect($url)
{
$prtocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === true ? 'https://' : 'http://';
header("Location: " . $prtocol . $_SERVER['HTTP_HOST'] . "/admin-panel/" . $url);
}
protected function redirectBack()
{
header("Location: " . $_SERVER['HTTP_REFERER']);
}
public function hash($string){
$hashString= password_hash($string,PASSWORD_DEFAULT);
return $hashString;
}
}
user.class
<?php
namespace AdminDashboard;
require_once ("Admin.class.php");
require_once (realpath(dirname(__FILE__) . "/DataBase.php"));
use DataBase\DataBase;
class User extends Admin{
public function index(){
$db= new DataBase();
$users=$db->select('SELECT * FROM `users` ORDER BY `id` DESC ;');
require_once (realpath(dirname(__FILE__). "/../template/admin/users/index.php"));
}
public function permission($id){
$db= new DataBase();
$user = $db->select("SELECT * FROM `users` WHERE (`id` = ?); ",[$id])->fetch();
if($user['permission'] == 'admin'){
$db->update('users',$id,['permission'],['user']);
}
else{
$db->update('users',$id,['permission'],['admin']);
}
$this->redirectBack();
}
public function edit($id){
$db= new DataBase();
$user = $db->select("SELECT * FROM `users` WHERE `id` = ? ;", [$id])->fetch();
require_once (realpath(dirname(__FILE__). "/../template/admin/users/edit.php"));
}
public function update($request,$id){
$db= new DataBase();
$db->update('users',$id,array_keys($request),$request);
$this->redirect('user');
}
public function delete($id){
$db= new DataBase();
$db->delete('users',$id);
$this->redirectBack();
}
}
html: it is login form
<body>
<form method="post" class="" action="http://localhost/admin-panel/check-login">
<?php
$httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
if($httpReferer == 'http://localhost/admin-panel/login'){?>
<div> <small class="form-text text-danger"> user is wrong</small> </div><?php
}?>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email ...">
<!-- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>-->
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password ...">
<!-- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>-->
</div>
<button type="submit" class="btn btn-sm btn-primary">login</button>
</form>
</body>
</html>
please help me what should i do? ?????????????????????????????????????????????????????????????????????????
Upvotes: 1
Views: 52
Reputation: 11
You need to use setcookie()
function in the login function of your Auth class or in the login file that you have included.
Upvotes: 1