SOCMED/index.php

61 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2020-01-02 21:48:25 +07:00
<?php
include('application/config/config.php');
include('application/model/user.php');
session_start();
//route
if( isset($_SESSION['username'])){ //jika sudah login
header('Location: home.php');
}
else{ //jika belum login
if (isset($_POST['action'])){ //jika form disubmit
unset($_SESSION['notif-alert']);
switch($_POST['action']) {
case 'login':
login();
break;
case 'register':
register();
break;
}
}else{ //jika form belum disubmit
include(view_file.'login.php');
}
}
//function
function login(){
$username = $_POST['user'];
$pass = $_POST['pass'];
$user = new User();
if($user->login($username,$pass)){ //jika valid
$_SESSION['username']=$username;
header('Location: '.base_url.'home.php');
}else{ //jika tidak valid
$_SESSION['notif-alert']='Username dan Password Salah';
header('Location: '.base_url.'index.php');
}
}
function register(){
$data = array(
"user" => $_POST['rUser'],
"pass" => $_POST['rPass'],
"email" => $_POST['rEmail']
);
$user = new User();
if($user->register($data)){ //jika valid
$_SESSION['username']=$data['user'];
mkdir(user_file . $_SESSION['username'], 0700);
header('Location: '.base_url.'home.php');
}else{ //jika tidak valid
$_SESSION['notif-alert']='Periksa kembali username dan email';
header('Location: '.base_url.'index.php');
}
}
?>