Advertisement
Guest User

class.user.php

a guest
Mar 29th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'config/dbconfig.php';
  4.  
  5. class USER
  6. {  
  7.  
  8.     private $conn;
  9.    
  10.     public function __construct()
  11.     {
  12.         $database = new Database();
  13.         $db = $database->dbConnection();
  14.         $this->conn = $db;
  15.     }
  16.    
  17.     public function runQuery($sql)
  18.     {
  19.         $stmt = $this->conn->prepare($sql);
  20.         return $stmt;
  21.     }
  22.    
  23.     public function lasdID()
  24.     {
  25.         $stmt = $this->conn->lastInsertId();
  26.         return $stmt;
  27.     }
  28.    
  29.     public function register($remail,$runame,$rupass,$code)
  30.     {
  31.         try
  32.         {                          
  33.             $password = md5($rupass);
  34.             $stmt = $this->conn->prepare("INSERT INTO users(email,username,password, passkey)
  35.                                                          VALUES(:user_mail, :user_name, :user_pass, :passkey)");
  36.             $stmt->bindparam(":user_mail",$remail);
  37.             $stmt->bindparam(":user_name",$runame);
  38.             $stmt->bindparam(":user_pass",$password);
  39.             $stmt->bindparam(":passkey",$code);
  40.  
  41.             $stmt->execute();  
  42.             return $stmt;
  43.         }
  44.         catch(PDOException $ex)
  45.         {
  46.             echo $ex->getMessage();
  47.         }
  48.     }
  49.    
  50.     public function edit($nama, $id)
  51.     {
  52.         try
  53.         {                          
  54.            
  55.             $stmt = $this->conn->prepare("UPDATE users SET username=:edit_nama WHERE id=:uid");
  56.             $stmt->bindparam(":edit_nama",$nama);
  57.             $stmt->bindparam(":uid",$id);
  58.  
  59.             $stmt->execute();  
  60.             return $stmt;
  61.         }
  62.         catch(PDOException $ex)
  63.         {
  64.             echo $ex->getMessage();
  65.         }
  66.     }
  67.  
  68.     public function login($email,$upass)
  69.     {
  70.         try
  71.         {
  72.             $stmt = $this->conn->prepare("SELECT * FROM users WHERE email=:email_id");
  73.             $stmt->execute(array(":email_id"=>$email));
  74.             $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
  75.            
  76.             if($stmt->rowCount() == 1)
  77.             {
  78.                 if($userRow['status_member']=="sudah")
  79.                 {
  80.                     if($userRow['password']==md5($upass))
  81.                     {
  82.                         $_SESSION['userSession'] = $userRow['id'];
  83.                         return true;
  84.                     }
  85.                     else
  86.                     {
  87.                         header("Location: login.php?error");
  88.                         exit;
  89.                     }
  90.                 }
  91.                 else
  92.                 {
  93.                     header("Location: login.php?inactive");
  94.                     exit;
  95.                 }  
  96.             }
  97.             else
  98.             {
  99.                 header("Location: login.php?error");
  100.                 exit;
  101.             }      
  102.         }
  103.         catch(PDOException $ex)
  104.         {
  105.             echo $ex->getMessage();
  106.         }
  107.     }
  108.    
  109.    
  110.     public function is_logged_in()
  111.     {
  112.         if(isset($_SESSION['userSession']))
  113.         {
  114.             return true;
  115.         }
  116.     }
  117.    
  118.     public function redirect($url)
  119.     {
  120.         header("Location: $url");
  121.     }
  122.    
  123.     public function logout()
  124.     {
  125.         session_destroy();
  126.         $_SESSION['userSession'] = false;
  127.     }
  128.    
  129.     function send_mail($email,$message,$subject)
  130.     {                      
  131.         require_once('mailer/class.phpmailer.php');
  132.         $mail = new PHPMailer();
  133.         $mail->IsSMTP();
  134.         $mail->SMTPDebug  = 1;                    
  135.         $mail->SMTPAuth   = true;                  
  136.         $mail->SMTPSecure = "ssl";                
  137.         $mail->Host       = "smtp.gmail.com";      
  138.         $mail->Port       = 465;            
  139.         $mail->AddAddress($email);
  140.         $mail->Username="bernandmalmass@gmail.com";  
  141.         $mail->Password="B3rnando";            
  142.         $mail->SetFrom('bernandmalmass@gmail.com','Coding Cage');
  143.         $mail->AddReplyTo("bernandmalmass@gmail.com","Coding Cage");
  144.         $mail->Subject    = $subject;
  145.         $mail->MsgHTML($message);
  146.         $mail->Send();
  147.     }  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement