Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use \application\controllers\MainController;
- class LoginController extends MainController{
- function __construct(){
- $this->setsession();
- $this->model('user');
- if (isset($_SESSION['login'])){
- $this->redirect('index');
- }
- }
- public function index(){
- $this->template1sm('login');
- }
- //Method untuk login
- public function log_in(){
- $data = array();
- $error = array();
- if (empty($_POST['username'])) {
- array_push($error, "Username cant be empty");
- }else{
- $username = $this->validate($_POST['username']);
- }
- if (empty($_POST['password'])) {
- array_push($error, "Password cant be empty");
- }else{
- $password = md5($this->validate($_POST['password']));
- }
- if(count($error)==0){
- $query = $this->user->selectWhere(array('username'=>$username, 'password'=>$password));
- $data = $this->user->getResult($query);
- $jml = $this->user->getRows($query);
- if($jml>0){
- $data = $data[0];
- if($data['activation']!=NULL){
- array_push($error, "Check your email to activate your account, or click Forget Password to resend activation code");
- $this->template1sm('login', array('msg'=> $error));
- }else{
- $_SESSION['login'] = $data['id_user'];
- if (isset($_POST['remember'])){
- $time = time();
- setcookie('login', $data['id_user'], $time + 3600);
- }
- $this->redirect('index');
- exit();
- }
- }else{
- array_push($error, "Username or password is wrong!");
- $this->template1sm('login', array('msg'=> $error));
- }
- }else{
- $this->template1sm('login', array('msg'=> $error));
- }
- }
- //Method untuk registrasi
- public function register(){
- $data = array();
- $error = array();
- if (empty($_POST['fname'])) {
- array_push($error, "First Name cant be empty");
- }else if (preg_match('/^[a-z]{1,20}$/', $_POST['fname'])) {
- $data['fname'] = $this->validate($_POST['fname']);
- }else{
- array_push($error, "First Name must be 1-20 characters a-z (lowercase)");
- }
- if (empty($_POST['lname'])) {
- array_push($error, "Last Name cant be empty");
- }else if (preg_match('/^[a-z]{1,20}$/', $_POST['lname'])) {
- $data['lname'] = $this->validate($_POST['lname']);
- }else{
- array_push($error, "Last Name must be 1-20 characters a-z (lowercase)");
- }
- if (empty($_POST['year'])||empty($_POST['month'])||empty($_POST['date'])) {
- array_push($error, "Birthday cant be empty");
- }else{
- $data['birthday'] = $this->validate($_POST['year'].'-'.$_POST['month'].'-'.$_POST['date']);
- }
- if (empty($_POST['gender'])) {
- array_push($error, "Gender cant be empty");
- }else{
- $data['gender'] = $this->validate($_POST['gender']);
- }
- if (empty($_POST['email'])) {
- array_push($error, "Email cant be empty");
- }else{
- $data['email'] = $this->validate($_POST['email']);
- }
- if (empty($_POST['usernamer'])) {
- array_push($error, "Username cant be empty");
- }else if (preg_match('/^[A-Za-z]{4,11}[A-Za-z0-9]{0,4}$/', $_POST['usernamer'])) {
- $data['username'] = $this->validate($_POST['usernamer']);
- $checkemail = $this->isUserExist($data['username'],$data['email']);
- if($checkemail) array_push($error, "Username or email already exist");
- }else{
- array_push($error, "Username must be 4-15 characters A-Z,a-z. You can use max 4 number behind letter");
- }
- if (empty($_POST['passwordr'])) {
- array_push($error, "Password cant be empty");
- }else if (preg_match('/^[a-z]{1,20}/i', $_POST['passwordr'])) {
- $password = md5($_POST['passwordr']);
- $data['password'] = $this->validate($password);
- }else{
- array_push($error, "Password must be 8-20 characters");
- }
- $data['regisdate'] = date('Y-m-d');
- $data['activation'] = md5(uniqid(rand(), true));
- if(count($error)==0){
- $register = $this->user->insert($data);
- $message = "Thankyou for join us \n\n";
- $message .= "To activate your account, please click link below :\n\n";
- $message .= "".BASE_PATH."/login/activate/".urlencode($data['email'])."/$data[activation]";
- mail($data['email'], "WEB Activation Link", $message, "From: cs@crefto.com");
- if($register) $this->redirect('login/berhasil');
- }else{
- $this->template1sm('login', array('msg'=> $error));
- }
- }
- function berhasil(){
- $msg = "Thank you for register, check your email to actiated your account";
- $this->template1sm('login', array('msg'=> $msg));
- }
- //Method untuk melihat email sudah tersedia atau belum
- public function isUserExist($username, $email){
- $query = $this->user->selectLike(array('username'=>$username,'email'=>$email));
- $jmluser = $this->user->getRows($query);
- if($jmluser > 0){
- return true;
- } else {
- return false;
- }
- }
- //Method untuk aktivasi
- public function activate($email,$activation){
- $data['activation'] = "Y";
- $simpan = $this->user->update($data, array('email'=>$email,'activation'=>$activation));
- if($simpan){
- $msg = "Your account has been activated you can login now!";
- }else{
- $msg = "Ooops your account cant activated. please do registration again or contact admin";
- }
- $this->template1sm('login', array('msg'=> $msg));
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement