Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. <?php
  2. use \application\controllers\MainController;
  3. class LoginController extends MainController{
  4. function __construct(){
  5. $this->setsession();
  6. $this->model('user');
  7. if (isset($_SESSION['login'])){
  8. $this->redirect('index');
  9. }
  10. }
  11. public function index(){
  12. $this->template1sm('login');
  13. }
  14. //Method untuk login
  15. public function log_in(){
  16. $data = array();
  17. $error = array();
  18. if (empty($_POST['username'])) {
  19. array_push($error, "Username cant be empty");
  20. }else{
  21. $username = $this->validate($_POST['username']);
  22. }
  23. if (empty($_POST['password'])) {
  24. array_push($error, "Password cant be empty");
  25. }else{
  26. $password = md5($this->validate($_POST['password']));
  27. }
  28.  
  29. if(count($error)==0){
  30. $query = $this->user->selectWhere(array('username'=>$username, 'password'=>$password));
  31.  
  32. $data = $this->user->getResult($query);
  33. $jml = $this->user->getRows($query);
  34.  
  35. if($jml>0){
  36. $data = $data[0];
  37. if($data['activation']!=NULL){
  38. array_push($error, "Check your email to activate your account, or click Forget Password to resend activation code");
  39. $this->template1sm('login', array('msg'=> $error));
  40. }else{
  41. $_SESSION['login'] = $data['id_user'];
  42. if (isset($_POST['remember'])){
  43. $time = time();
  44. setcookie('login', $data['id_user'], $time + 3600);
  45. }
  46. $this->redirect('index');
  47.  
  48. exit();
  49. }
  50. }else{
  51. array_push($error, "Username or password is wrong!");
  52. $this->template1sm('login', array('msg'=> $error));
  53. }
  54. }else{
  55. $this->template1sm('login', array('msg'=> $error));
  56. }
  57. }
  58.  
  59. //Method untuk registrasi
  60. public function register(){
  61. $data = array();
  62. $error = array();
  63. if (empty($_POST['fname'])) {
  64. array_push($error, "First Name cant be empty");
  65. }else if (preg_match('/^[a-z]{1,20}$/', $_POST['fname'])) {
  66. $data['fname'] = $this->validate($_POST['fname']);
  67. }else{
  68. array_push($error, "First Name must be 1-20 characters a-z (lowercase)");
  69. }
  70. if (empty($_POST['lname'])) {
  71. array_push($error, "Last Name cant be empty");
  72. }else if (preg_match('/^[a-z]{1,20}$/', $_POST['lname'])) {
  73. $data['lname'] = $this->validate($_POST['lname']);
  74. }else{
  75. array_push($error, "Last Name must be 1-20 characters a-z (lowercase)");
  76. }
  77. if (empty($_POST['year'])||empty($_POST['month'])||empty($_POST['date'])) {
  78. array_push($error, "Birthday cant be empty");
  79. }else{
  80. $data['birthday'] = $this->validate($_POST['year'].'-'.$_POST['month'].'-'.$_POST['date']);
  81. }
  82. if (empty($_POST['gender'])) {
  83. array_push($error, "Gender cant be empty");
  84. }else{
  85. $data['gender'] = $this->validate($_POST['gender']);
  86. }
  87. if (empty($_POST['email'])) {
  88. array_push($error, "Email cant be empty");
  89. }else{
  90. $data['email'] = $this->validate($_POST['email']);
  91. }
  92. if (empty($_POST['usernamer'])) {
  93. array_push($error, "Username cant be empty");
  94. }else if (preg_match('/^[A-Za-z]{4,11}[A-Za-z0-9]{0,4}$/', $_POST['usernamer'])) {
  95. $data['username'] = $this->validate($_POST['usernamer']);
  96. $checkemail = $this->isUserExist($data['username'],$data['email']);
  97. if($checkemail) array_push($error, "Username or email already exist");
  98. }else{
  99. array_push($error, "Username must be 4-15 characters A-Z,a-z. You can use max 4 number behind letter");
  100. }
  101. if (empty($_POST['passwordr'])) {
  102. array_push($error, "Password cant be empty");
  103. }else if (preg_match('/^[a-z]{1,20}/i', $_POST['passwordr'])) {
  104. $password = md5($_POST['passwordr']);
  105. $data['password'] = $this->validate($password);
  106. }else{
  107. array_push($error, "Password must be 8-20 characters");
  108. }
  109. $data['regisdate'] = date('Y-m-d');
  110. $data['activation'] = md5(uniqid(rand(), true));
  111.  
  112. if(count($error)==0){
  113. $register = $this->user->insert($data);
  114. $message = "Thankyou for join us \n\n";
  115. $message .= "To activate your account, please click link below :\n\n";
  116. $message .= "".BASE_PATH."/login/activate/".urlencode($data['email'])."/$data[activation]";
  117. mail($data['email'], "WEB Activation Link", $message, "From: cs@crefto.com");
  118. if($register) $this->redirect('login/berhasil');
  119. }else{
  120. $this->template1sm('login', array('msg'=> $error));
  121. }
  122. }
  123. function berhasil(){
  124. $msg = "Thank you for register, check your email to actiated your account";
  125. $this->template1sm('login', array('msg'=> $msg));
  126. }
  127.  
  128. //Method untuk melihat email sudah tersedia atau belum
  129. public function isUserExist($username, $email){
  130. $query = $this->user->selectLike(array('username'=>$username,'email'=>$email));
  131. $jmluser = $this->user->getRows($query);
  132. if($jmluser > 0){
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138.  
  139. //Method untuk aktivasi
  140. public function activate($email,$activation){
  141. $data['activation'] = "Y";
  142. $simpan = $this->user->update($data, array('email'=>$email,'activation'=>$activation));
  143. if($simpan){
  144. $msg = "Your account has been activated you can login now!";
  145. }else{
  146. $msg = "Ooops your account cant activated. please do registration again or contact admin";
  147. }
  148. $this->template1sm('login', array('msg'=> $msg));
  149. }
  150. }
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement