Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Dao.php
- //This script was taken from the class notes and modified.
- if(!isset($_SESSION)) session_start();
- class Dao
- {
- private $host = "localhost";
- private $db = "rogden";
- private $user = "rogden";
- private $pass = "BruXaq2j";
- public function getConnection()
- {
- return new PDO("mysql:host={$this->host};dbname={$this->db}", $this->user, $this->pass);
- }
- public function saveUser($user, $password)
- {
- $connection = $this->getConnection();
- $error = '';
- if (isset($_POST['signup'])) {
- if (empty($_POST['username']) || empty($_POST['password'])) {
- $error = "Username or Password cannot be blank.";
- echo $error;
- header("location: login.php");
- } else {
- // Define $username and $password
- $username = $_POST["username"];
- $password = $_POST["password"];
- echo $username;
- echo $password;
- //Check if the entered username is available
- $check = $connection->prepare('Select * from user where userName=:user');
- $check->bindParam(":user", $user);
- $check->execute();
- $checkResults = $check->fetchAll();
- if(count($checkResults) > 0){
- $error = "This username already exists. Please try another.";
- echo $error;
- $_SESSION['userNameExists'] = $error;
- die;
- }
- // Insert new user into the database
- $conn = $this->getConnection();
- $saveQuery = "INSERT INTO user(
- userName,
- password,
- createDate)
- VALUES(
- (:user),
- (:password),
- NOW())";
- $q = $conn->prepare($saveQuery);
- $q->bindParam(":user", $user);
- $q->bindParam(":password", $password);
- $q->execute();
- // Find the newly registered user to push them to the login page
- $q = $conn->prepare('select * from user where password=:password AND userName=:username');
- $q->bindParam(':username', $user);
- $q->bindParam(':password', $password);
- $q->execute();
- $results = $q->fetchAll();
- if (!empty($results)) {
- $_SESSION['login_user'] = $username; // Initializing Session
- $_SESSION['loggedIn'] = true;
- header("location: dashboard.php"); // Redirecting To Dashboard
- } else {
- $error = "Unspecified error. Please try again.";
- echo $error;
- header("location: login.php");
- }
- }
- }
- }
- public function login($username, $password)
- {
- $connection = $this->getConnection();
- $error = '';
- if (isset($_POST['login'])) {
- if (empty($_POST['username']) || empty($_POST['password'])) {
- $error = "Username or Password is invalid";
- echo $error;
- header("location: login.php");
- } else {
- // Define $username and $password
- $username = $_POST["username"];
- $password = $_POST["password"];
- echo $username;
- echo $password;
- // SQL query to fetch information of registered users and finds user match.
- $q = $connection->prepare('select * from user where password=:password AND userName=:username');
- $q->bindParam(':username', $username);
- $q->bindParam(':password', $password);
- $q->execute();
- $results = $q->fetchAll();
- print_r($results);
- if (!empty($results)) {
- $_SESSION['login_user'] = $username; // Initializing Session
- $_SESSION['loggedIn'] = true;
- header("location: dashboard.php"); // Redirecting To Dashboard
- } else {
- $error = "Username or Password is invalid";
- echo $error;
- header("location: logi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement