Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | None | 0 0
  1. <?php
  2. // Dao.php
  3. //This script was taken from the class notes and modified.
  4. if(!isset($_SESSION)) session_start();
  5. class Dao
  6. {
  7.    
  8.     private $host = "localhost";
  9.     private $db = "rogden";
  10.     private $user = "rogden";
  11.     private $pass = "BruXaq2j";
  12.    
  13.     public function getConnection()
  14.     {
  15.         return new PDO("mysql:host={$this->host};dbname={$this->db}", $this->user, $this->pass);
  16.     }
  17.    
  18.     public function saveUser($user, $password)
  19.     {
  20.         $connection = $this->getConnection();
  21.         $error = '';
  22.         if (isset($_POST['signup'])) {
  23.             if (empty($_POST['username']) || empty($_POST['password'])) {
  24.                 $error = "Username or Password cannot be blank.";
  25.                 echo $error;
  26.                 header("location: login.php");
  27.             } else {
  28.                 // Define $username and $password
  29.                 $username = $_POST["username"];
  30.                 $password = $_POST["password"];
  31.                 echo $username;
  32.                 echo $password;
  33.  
  34.                 //Check if the entered username is available
  35.                 $check = $connection->prepare('Select * from user where userName=:user');
  36.                 $check->bindParam(":user", $user);
  37.                 $check->execute();
  38.                 $checkResults = $check->fetchAll();
  39.  
  40.                 if(count($checkResults) > 0){
  41.                     $error = "This username already exists. Please try another.";
  42.                     echo $error;
  43.                     $_SESSION['userNameExists'] = $error;
  44.                     die;
  45.                 }
  46.                
  47.                 // Insert new user into the database
  48.                 $conn      = $this->getConnection();
  49.                 $saveQuery = "INSERT INTO user(
  50.                userName,
  51.                password,
  52.                createDate)
  53.                VALUES(
  54.                (:user),
  55.                (:password),
  56.                NOW())";
  57.                
  58.                 $q = $conn->prepare($saveQuery);
  59.                 $q->bindParam(":user", $user);
  60.                 $q->bindParam(":password", $password);
  61.                 $q->execute();
  62.  
  63.                 // Find the newly registered user to push them to the login page
  64.                 $q = $conn->prepare('select * from user where password=:password AND userName=:username');
  65.                 $q->bindParam(':username', $user);
  66.                 $q->bindParam(':password', $password);
  67.                 $q->execute();
  68.                 $results = $q->fetchAll();
  69.                
  70.                 if (!empty($results)) {
  71.                     $_SESSION['login_user'] = $username; // Initializing Session
  72.                     $_SESSION['loggedIn'] = true;
  73.                     header("location: dashboard.php"); // Redirecting To Dashboard
  74.                 } else {
  75.                     $error = "Unspecified error. Please try again.";
  76.                     echo $error;
  77.                     header("location: login.php");
  78.                 }
  79.             }
  80.         }
  81.     }
  82.    
  83.     public function login($username, $password)
  84.     {
  85.         $connection = $this->getConnection();
  86.         $error = '';
  87.         if (isset($_POST['login'])) {
  88.             if (empty($_POST['username']) || empty($_POST['password'])) {
  89.                 $error = "Username or Password is invalid";
  90.                 echo $error;
  91.                 header("location: login.php");
  92.             } else {
  93.                 // Define $username and $password
  94.                 $username = $_POST["username"];
  95.                 $password = $_POST["password"];
  96.                 echo $username;
  97.                 echo $password;
  98.                
  99.                 // SQL query to fetch information of registered users and finds user match.
  100.                 $q = $connection->prepare('select * from user where password=:password AND userName=:username');
  101.                 $q->bindParam(':username', $username);
  102.                 $q->bindParam(':password', $password);
  103.                 $q->execute();
  104.                 $results = $q->fetchAll();
  105.                 print_r($results);
  106.                
  107.                 if (!empty($results)) {
  108.                     $_SESSION['login_user'] = $username; // Initializing Session
  109.                     $_SESSION['loggedIn'] = true;
  110.                     header("location: dashboard.php"); // Redirecting To Dashboard
  111.                 } else {
  112.                     $error = "Username or Password is invalid";
  113.                     echo $error;
  114.                     header("location: logi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement