├── .vscode └── settings.json ├── add.php ├── database.php ├── delete.php ├── edit.php ├── home.php ├── index.php ├── login.php ├── logout.php ├── partials ├── footer.php ├── header.php └── navbar.php ├── register.php ├── sql └── setup.sql └── static ├── css └── index.css ├── img ├── background.jpg └── logo.png └── js └── welcome.js /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[php]": { 3 | "editor.tabSize": 2 4 | }, 5 | "[html]": { 6 | "editor.tabSize": 2 7 | }, 8 | "[javascript]": { 9 | "editor.tabSize": 2 10 | }, 11 | "files.insertFinalNewline": true, 12 | "editor.fontSize": 24, 13 | "editor.lineHeight": 26, 14 | "terminal.integrated.fontSize": 24, 15 | "window.zoomLevel": 2 16 | } 17 | -------------------------------------------------------------------------------- /add.php: -------------------------------------------------------------------------------- 1 | prepare("INSERT INTO contacts (user_id, name, phone_number) VALUES ({$_SESSION['user']['id']}, :name, :phone_number)"); 24 | $statement->bindParam(":name", $_POST["name"]); 25 | $statement->bindParam(":phone_number", $_POST["phone_number"]); 26 | $statement->execute(); 27 | 28 | $_SESSION["flash"] = ["message" => "Contact {$_POST['name']} added."]; 29 | 30 | header("Location: home.php"); 31 | return; 32 | } 33 | } 34 | ?> 35 | 36 | 37 | 38 |
39 |
40 |
41 |
42 |
Add New Contact
43 |
44 | 45 |

46 | 47 |

48 | 49 |
50 |
51 | 52 | 53 |
54 | 55 |
56 |
57 | 58 |
59 | 60 | 61 |
62 | 63 |
64 |
65 | 66 |
67 |
68 | 69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /database.php: -------------------------------------------------------------------------------- 1 | query("SHOW DATABASES") as $row) { 11 | // print_r($row); 12 | // } 13 | // die(); 14 | } catch (PDOException $e) { 15 | die("PDO Connection Error: " . $e->getMessage()); 16 | } 17 | -------------------------------------------------------------------------------- /delete.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM contacts WHERE id = :id LIMIT 1"); 15 | $statement->execute([":id" => $id]); 16 | 17 | if ($statement->rowCount() == 0) { 18 | http_response_code(404); 19 | echo("HTTP 404 NOT FOUND"); 20 | return; 21 | } 22 | 23 | $contact = $statement->fetch(PDO::FETCH_ASSOC); 24 | 25 | if ($contact["user_id"] !== $_SESSION["user"]["id"]) { 26 | http_response_code(403); 27 | echo("HTTP 403 UNAUTHORIZED"); 28 | return; 29 | } 30 | 31 | $conn->prepare("DELETE FROM contacts WHERE id = :id")->execute([":id" => $id]); 32 | 33 | $_SESSION["flash"] = ["message" => "Contact {$contact['name']} deleted."]; 34 | 35 | header("Location: home.php"); 36 | -------------------------------------------------------------------------------- /edit.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM contacts WHERE id = :id LIMIT 1"); 15 | $statement->execute([":id" => $id]); 16 | 17 | if ($statement->rowCount() == 0) { 18 | http_response_code(404); 19 | echo("HTTP 404 NOT FOUND"); 20 | return; 21 | } 22 | 23 | $contact = $statement->fetch(PDO::FETCH_ASSOC); 24 | 25 | if ($contact["user_id"] !== $_SESSION["user"]["id"]) { 26 | http_response_code(403); 27 | echo("HTTP 403 UNAUTHORIZED"); 28 | return; 29 | } 30 | 31 | $error = null; 32 | 33 | if ($_SERVER["REQUEST_METHOD"] == "POST") { 34 | if (empty($_POST["name"]) || empty($_POST["phone_number"])) { 35 | $error = "Please fill all the fields."; 36 | } else if (strlen($_POST["phone_number"]) < 9) { 37 | $error = "Phone number must be at least 9 characters."; 38 | } else { 39 | $name = $_POST["name"]; 40 | $phoneNumber = $_POST["phone_number"]; 41 | 42 | $statement = $conn->prepare("UPDATE contacts SET name = :name, phone_number = :phone_number WHERE id = :id"); 43 | $statement->execute([ 44 | ":id" => $id, 45 | ":name" => $_POST["name"], 46 | ":phone_number" => $_POST["phone_number"], 47 | ]); 48 | 49 | $_SESSION["flash"] = ["message" => "Contact {$_POST['name']} updated."]; 50 | 51 | header("Location: home.php"); 52 | return; 53 | } 54 | } 55 | ?> 56 | 57 | 58 | 59 |
60 |
61 |
62 |
63 |
Add New Contact
64 |
65 | 66 |

67 | 68 |

69 | 70 |
71 |
72 | 73 | 74 |
75 | 76 |
77 |
78 | 79 |
80 | 81 | 82 |
83 | 84 |
85 |
86 | 87 |
88 |
89 | 90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /home.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM contacts WHERE user_id = {$_SESSION['user']['id']}"); 13 | 14 | ?> 15 | 16 | 17 | 18 |
19 |
20 | 21 | rowCount() == 0): ?> 22 |
23 |
24 |

No contacts saved yet

25 | Add One! 26 |
27 |
28 | 29 | 30 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
5 |

Store Your Contacts Now

6 | Get Started 7 |
8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /login.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM users WHERE email = :email LIMIT 1"); 14 | $statement->bindParam(":email", $_POST["email"]); 15 | $statement->execute(); 16 | 17 | if ($statement->rowCount() == 0) { 18 | $error = "Invalid credentials."; 19 | } else { 20 | $user = $statement->fetch(PDO::FETCH_ASSOC); 21 | 22 | if (!password_verify($_POST["password"], $user["password"])) { 23 | $error = "Invalid credentials."; 24 | } else { 25 | session_start(); 26 | 27 | unset($user["password"]); 28 | 29 | $_SESSION["user"] = $user; 30 | 31 | header("Location: home.php"); 32 | } 33 | } 34 | } 35 | } 36 | ?> 37 | 38 | 39 | 40 |
41 |
42 |
43 |
44 |
Login
45 |
46 | 47 |

48 | 49 |

50 | 51 |
52 |
53 | 54 | 55 |
56 | 57 |
58 |
59 | 60 |
61 | 62 | 63 |
64 | 65 |
66 |
67 | 68 |
69 |
70 | 71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /logout.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /partials/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | Contacts App 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 |
44 | 50 |
51 | 52 | 53 | 54 | 55 |
56 | 57 | -------------------------------------------------------------------------------- /partials/navbar.php: -------------------------------------------------------------------------------- 1 | 49 | -------------------------------------------------------------------------------- /register.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM users WHERE email = :email"); 14 | $statement->bindParam(":email", $_POST["email"]); 15 | $statement->execute(); 16 | 17 | if ($statement->rowCount() > 0) { 18 | $error = "This email is taken."; 19 | } else { 20 | $conn 21 | ->prepare("INSERT INTO users (name, email, password) VALUES (:name, :email, :password)") 22 | ->execute([ 23 | ":name" => $_POST["name"], 24 | ":email" => $_POST["email"], 25 | ":password" => password_hash($_POST["password"], PASSWORD_BCRYPT), 26 | ]); 27 | 28 | $statement = $conn->prepare("SELECT * FROM users WHERE email = :email LIMIT 1"); 29 | $statement->bindParam(":email", $_POST["email"]); 30 | $statement->execute(); 31 | $user = $statement->fetch(PDO::FETCH_ASSOC); 32 | 33 | session_start(); 34 | $_SESSION["user"] = $user; 35 | 36 | header("Location: home.php"); 37 | } 38 | } 39 | } 40 | ?> 41 | 42 | 43 | 44 |
45 |
46 |
47 |
48 |
Register
49 |
50 | 51 |

52 | 53 |

54 | 55 |
56 |
57 | 58 | 59 |
60 | 61 |
62 |
63 | 64 |
65 | 66 | 67 |
68 | 69 |
70 |
71 | 72 |
73 | 74 | 75 |
76 | 77 |
78 |
79 | 80 |
81 |
82 | 83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /sql/setup.sql: -------------------------------------------------------------------------------- 1 | DROP DATABASE IF EXISTS contacts_app; 2 | 3 | CREATE DATABASE contacts_app; 4 | 5 | USE contacts_app; 6 | 7 | CREATE TABLE users ( 8 | id INT AUTO_INCREMENT PRIMARY KEY, 9 | name VARCHAR(255), 10 | email VARCHAR(255) UNIQUE, 11 | password VARCHAR(255) 12 | ); 13 | 14 | CREATE TABLE contacts ( 15 | id INT AUTO_INCREMENT PRIMARY KEY, 16 | name VARCHAR(255), 17 | user_id INT NOT NULL, 18 | phone_number VARCHAR(255), 19 | 20 | FOREIGN KEY (user_id) REFERENCES users(id) 21 | ); 22 | -------------------------------------------------------------------------------- /static/css/index.css: -------------------------------------------------------------------------------- 1 | .navbar img { 2 | width: 1.2rem; 3 | } 4 | 5 | .welcome { 6 | background: url('/contacts-app/static/img/background.jpg'); 7 | background-position: center center; 8 | background-size: cover; 9 | } 10 | -------------------------------------------------------------------------------- /static/img/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mastermindac/curso-php/ca39aed8034f9dfa33c4afc88047614659868210/static/img/background.jpg -------------------------------------------------------------------------------- /static/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mastermindac/curso-php/ca39aed8034f9dfa33c4afc88047614659868210/static/img/logo.png -------------------------------------------------------------------------------- /static/js/welcome.js: -------------------------------------------------------------------------------- 1 | const navbar = document.querySelector(".navbar"); 2 | const welcome = document.querySelector(".welcome"); 3 | const navbarToggle = document.querySelector("#navbarNav"); 4 | 5 | const resizeBakgroundImg = () => { 6 | const height = window.innerHeight - navbar.clientHeight; 7 | welcome.style.height = `${height}px`; 8 | }; 9 | 10 | 11 | navbarToggle.ontransitionend = resizeBakgroundImg; 12 | navbarToggle.ontransitionstart = resizeBakgroundImg; 13 | window.onresize = resizeBakgroundImg; 14 | window.onload = resizeBakgroundImg; 15 | --------------------------------------------------------------------------------