Skip to content

Commit 0c8115b

Browse files
Code Files Added
1 parent 62d84fd commit 0c8115b

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

PHPLearning.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.7.7
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Host: localhost
6+
-- Generation Time: May 25, 2018 at 12:09 PM
7+
-- Server version: 10.1.30-MariaDB
8+
-- PHP Version: 7.2.1
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET AUTOCOMMIT = 0;
12+
START TRANSACTION;
13+
SET time_zone = "+00:00";
14+
15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
21+
--
22+
-- Database: `PHPLearning`
23+
--
24+
25+
-- --------------------------------------------------------
26+
27+
--
28+
-- Table structure for table `users`
29+
--
30+
31+
CREATE TABLE `users` (
32+
`id` int(11) NOT NULL,
33+
`username` varchar(255) NOT NULL,
34+
`password` varchar(255) NOT NULL,
35+
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
36+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
37+
38+
--
39+
-- Dumping data for table `users`
40+
--
41+
42+
INSERT INTO `users` (`id`, `username`, `password`, `created`) VALUES
43+
(1, 'shehryar', 'MTIzNDU=', '2018-05-25 06:17:14');
44+
45+
--
46+
-- Indexes for dumped tables
47+
--
48+
49+
--
50+
-- Indexes for table `users`
51+
--
52+
ALTER TABLE `users`
53+
ADD PRIMARY KEY (`id`);
54+
55+
--
56+
-- AUTO_INCREMENT for dumped tables
57+
--
58+
59+
--
60+
-- AUTO_INCREMENT for table `users`
61+
--
62+
ALTER TABLE `users`
63+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
64+
COMMIT;
65+
66+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
67+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
68+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

api/User/login.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
// include database and object files
3+
include_once '../config/database.php';
4+
include_once '../objects/user.php';
5+
6+
// get database connection
7+
$database = new Database();
8+
$db = $database->getConnection();
9+
10+
// prepare user object
11+
$user = new User($db);
12+
// set ID property of user to be edited
13+
$user->username = isset($_GET['username']) ? $_GET['username'] : die();
14+
$user->password = base64_encode(isset($_GET['password']) ? $_GET['password'] : die());
15+
// read the details of user to be edited
16+
$stmt = $user->login();
17+
if($stmt->rowCount() > 0){
18+
// get retrieved row
19+
$row = $stmt->fetch(PDO::FETCH_ASSOC);
20+
// create array
21+
$user_arr=array(
22+
"status" => true,
23+
"message" => "Successfully Login!",
24+
"id" => $row['id'],
25+
"username" => $row['username']
26+
);
27+
}
28+
else{
29+
$user_arr=array(
30+
"status" => false,
31+
"message" => "Invalid Username or Password!",
32+
);
33+
}
34+
// make it json format
35+
print_r(json_encode($user_arr));
36+
?>

api/User/signup.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
// get database connection
4+
include_once '../config/database.php';
5+
6+
// instantiate user object
7+
include_once '../objects/user.php';
8+
9+
$database = new Database();
10+
$db = $database->getConnection();
11+
12+
$user = new User($db);
13+
14+
// set user property values
15+
$user->username = $_POST['username'];
16+
$user->password = base64_encode($_POST['password']);
17+
$user->created = date('Y-m-d H:i:s');
18+
19+
// create the user
20+
if($user->signup()){
21+
$user_arr=array(
22+
"status" => true,
23+
"message" => "Successfully Signup!",
24+
"id" => $user->id,
25+
"username" => $user->username
26+
);
27+
}
28+
else{
29+
$user_arr=array(
30+
"status" => false,
31+
"message" => "Username already exists!"
32+
);
33+
}
34+
print_r(json_encode($user_arr));
35+
?>

api/config/database.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
class Database{
3+
4+
// specify your own database credentials
5+
private $host = "localhost";
6+
private $db_name = "PHPLearning";
7+
private $username = "root";
8+
private $password = "";
9+
public $conn;
10+
11+
// get the database connection
12+
public function getConnection(){
13+
14+
$this->conn = null;
15+
16+
try{
17+
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
18+
$this->conn->exec("set names utf8");
19+
}catch(PDOException $exception){
20+
echo "Connection error: " . $exception->getMessage();
21+
}
22+
23+
return $this->conn;
24+
}
25+
}
26+
?>

api/objects/user.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
class User{
3+
4+
// database connection and table name
5+
private $conn;
6+
private $table_name = "users";
7+
8+
// object properties
9+
public $id;
10+
public $username;
11+
public $password;
12+
public $created;
13+
14+
// constructor with $db as database connection
15+
public function __construct($db){
16+
$this->conn = $db;
17+
}
18+
// signup user
19+
function signup(){
20+
21+
if($this->isAlreadyExist()){
22+
return false;
23+
}
24+
// query to insert record
25+
$query = "INSERT INTO
26+
" . $this->table_name . "
27+
SET
28+
username=:username, password=:password, created=:created";
29+
30+
// prepare query
31+
$stmt = $this->conn->prepare($query);
32+
33+
// sanitize
34+
$this->username=htmlspecialchars(strip_tags($this->username));
35+
$this->password=htmlspecialchars(strip_tags($this->password));
36+
$this->created=htmlspecialchars(strip_tags($this->created));
37+
38+
// bind values
39+
$stmt->bindParam(":username", $this->username);
40+
$stmt->bindParam(":password", $this->password);
41+
$stmt->bindParam(":created", $this->created);
42+
43+
// execute query
44+
if($stmt->execute()){
45+
$this->id = $this->conn->lastInsertId();
46+
return true;
47+
}
48+
49+
return false;
50+
51+
}
52+
// login user
53+
function login(){
54+
// select all query
55+
$query = "SELECT
56+
`id`, `username`, `password`, `created`
57+
FROM
58+
" . $this->table_name . "
59+
WHERE
60+
username='".$this->username."' AND password='".$this->password."'";
61+
// prepare query statement
62+
$stmt = $this->conn->prepare($query);
63+
// execute query
64+
$stmt->execute();
65+
return $stmt;
66+
}
67+
function isAlreadyExist(){
68+
$query = "SELECT *
69+
FROM
70+
" . $this->table_name . "
71+
WHERE
72+
username='".$this->username."'";
73+
// prepare query statement
74+
$stmt = $this->conn->prepare($query);
75+
// execute query
76+
$stmt->execute();
77+
if($stmt->rowCount() > 0){
78+
return true;
79+
}
80+
else{
81+
return false;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)