main
This commit is contained in:
parent
3c25b0a6af
commit
df71b56319
104
add_pet.php
Normal file
104
add_pet.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
|
||||
header("location: login.php");
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once 'db/config.php';
|
||||
$error = '';
|
||||
$success = '';
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$name = trim($_POST['name']);
|
||||
$age = trim($_POST['age']);
|
||||
$breed = trim($_POST['breed']);
|
||||
$photo_url = trim($_POST['photo_url']);
|
||||
$user_id = $_SESSION['id'];
|
||||
|
||||
if (empty($photo_url)) {
|
||||
$photo_url = 'https://picsum.photos/500/500?random=' . mt_rand();
|
||||
}
|
||||
|
||||
if (empty($name) || empty($age) || empty($breed)) {
|
||||
$error = 'Please fill out all name, age, and breed fields.';
|
||||
} else {
|
||||
try {
|
||||
$conn = db();
|
||||
$sql = "INSERT INTO pets (user_id, name, age, breed, photo_url) VALUES (:user_id, :name, :age, :breed, :photo_url)";
|
||||
$stmt = $conn->prepare($sql);
|
||||
|
||||
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
|
||||
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
|
||||
$stmt->bindParam(':age', $age, PDO::PARAM_INT);
|
||||
$stmt->bindParam(':breed', $breed, PDO::PARAM_STR);
|
||||
$stmt->bindParam(':photo_url', $photo_url, PDO::PARAM_STR);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$success = "Pet added successfully!";
|
||||
} else {
|
||||
$error = "Something went wrong. Please try again later.";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = "Error: " . $e->getMessage();
|
||||
}
|
||||
$conn = null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Add Pet - Pet Tinder</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="index.php">Pet Tinder</a>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="logout.php">Logout</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
<h1 class="text-center mt-5">Add a New Pet</h1>
|
||||
<?php if(!empty($error)): ?>
|
||||
<div class="alert alert-danger"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
<?php if(!empty($success)): ?>
|
||||
<div class="alert alert-success"><?php echo $success; ?></div>
|
||||
<?php endif; ?>
|
||||
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Pet's Name</label>
|
||||
<input type="text" name="name" id="name" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="age" class="form-label">Age</label>
|
||||
<input type="number" name="age" id="age" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="breed" class="form-label">Breed</label>
|
||||
<input type="text" name="breed" id="breed" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="photo_url" class="form-label">Photo URL</label>
|
||||
<input type="text" name="photo_url" id="photo_url" class="form-control">
|
||||
</div>
|
||||
<div class="d-grid">
|
||||
<button type="submit" class="btn btn-primary">Add Pet</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
32
css/style.css
Normal file
32
css/style.css
Normal file
@ -0,0 +1,32 @@
|
||||
body {
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
#pet-card {
|
||||
border-radius: 15px;
|
||||
overflow: hidden;
|
||||
height: 70vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.card-img-top {
|
||||
width: 100%;
|
||||
height: 75%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.card-body {
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
#actions .btn {
|
||||
width: 100px;
|
||||
height: 100px;
|
||||
border-radius: 50%;
|
||||
margin: 0 20px;
|
||||
font-size: 1.5rem;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
17
db/config.php
Normal file
17
db/config.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
// Generated by setup_mariadb_project.sh — edit as needed.
|
||||
define('DB_HOST', '127.0.0.1');
|
||||
define('DB_NAME', 'app_30785');
|
||||
define('DB_USER', 'app_30785');
|
||||
define('DB_PASS', '73300eb2-b4f4-4ce5-a835-8ec1d05b239c');
|
||||
|
||||
function db() {
|
||||
static $pdo;
|
||||
if (!$pdo) {
|
||||
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
}
|
||||
return $pdo;
|
||||
}
|
||||
174
index.php
174
index.php
@ -1,115 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
@ini_set('display_errors', '1');
|
||||
@error_reporting(E_ALL);
|
||||
@date_default_timezone_set('UTC');
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
// Fetch pets from the database
|
||||
$pets = [];
|
||||
try {
|
||||
$conn = db();
|
||||
$sql = "SELECT name, age, breed, photo_url FROM pets ORDER BY RAND()"; // Get pets in random order
|
||||
$stmt = $conn->query($sql);
|
||||
$pets = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
// Don't display error to user, just log it or handle it
|
||||
}
|
||||
|
||||
$phpVersion = PHP_VERSION;
|
||||
$now = date('Y-m-d H:i:s');
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>New Style</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
:root {
|
||||
--bg-color-start: #ffecd2;
|
||||
--bg-color-end: #fcb69f;
|
||||
--text-color: #333333;
|
||||
--card-bg-color: rgba(255, 255, 255, 0.5);
|
||||
--card-border-color: rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: 'Inter', sans-serif;
|
||||
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
|
||||
color: var(--text-color);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
body::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(0,0,0,0.05)"/></svg>');
|
||||
animation: bg-pan 20s linear infinite;
|
||||
z-index: -1;
|
||||
}
|
||||
@keyframes bg-pan {
|
||||
0% { background-position: 0% 0%; }
|
||||
100% { background-position: 100% 100%; }
|
||||
}
|
||||
main {
|
||||
padding: 2rem;
|
||||
}
|
||||
.card {
|
||||
background: var(--card-bg-color);
|
||||
border: 1px solid var(--card-border-color);
|
||||
border-radius: 16px;
|
||||
padding: 2rem;
|
||||
backdrop-filter: blur(20px);
|
||||
-webkit-backdrop-filter: blur(20px);
|
||||
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
|
||||
animation: float 6s ease-in-out infinite, fadeIn 1s ease-out forwards;
|
||||
}
|
||||
h1 {
|
||||
font-size: 3rem;
|
||||
font-weight: 700;
|
||||
margin: 0 0 1rem;
|
||||
letter-spacing: -1px;
|
||||
animation: fadeIn 1.5s ease-out forwards;
|
||||
}
|
||||
p {
|
||||
margin: 0.5rem 0;
|
||||
font-size: 1.1rem;
|
||||
animation: fadeIn 2s ease-out forwards;
|
||||
}
|
||||
@keyframes float {
|
||||
0% { transform: translateY(0px); }
|
||||
50% { transform: translateY(-10px); }
|
||||
100% { transform: translateY(0px); }
|
||||
}
|
||||
@keyframes fadeIn {
|
||||
from { opacity: 0; transform: translateY(10px); }
|
||||
to { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
code {
|
||||
background: rgba(0,0,0,0.2);
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
||||
}
|
||||
footer {
|
||||
position: absolute;
|
||||
bottom: 1rem;
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.7;
|
||||
}
|
||||
</style>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="referrerpolicy" content="no-referrer">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Tinder for Pets</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div class="card">
|
||||
<h1>Welcome!</h1>
|
||||
<p>Your project is ready to conquer the peaks.</p>
|
||||
<p>PHP version: <code><?= htmlspecialchars($phpVersion) ?></code></p>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="index.php">Pet Tinder</a>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<?php if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="my_pets.php">My Pets</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="add_pet.php">Add Pet</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="logout.php">Logout</a>
|
||||
</li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="login.php">Login</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="register.php">Register</a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-6">
|
||||
<div id="pet-card" class="card shadow-lg">
|
||||
<!-- Pet profile will be injected here by JavaScript -->
|
||||
</div>
|
||||
<div id="actions" class="text-center mt-4">
|
||||
<button id="dislike-btn" class="btn btn-danger btn-lg">Пропустить</button>
|
||||
<button id="like-btn" class="btn btn-success btn-lg">Лайк</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
<footer>
|
||||
Page updated: <?= htmlspecialchars($now) ?> (UTC)
|
||||
</footer>
|
||||
|
||||
<script>
|
||||
// Pass pets data from PHP to JavaScript
|
||||
const pets = <?php echo json_encode($pets); ?>;
|
||||
</script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="js/script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
38
js/script.js
Normal file
38
js/script.js
Normal file
@ -0,0 +1,38 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const petCard = document.getElementById('pet-card');
|
||||
const likeBtn = document.getElementById('like-btn');
|
||||
const dislikeBtn = document.getElementById('dislike-btn');
|
||||
|
||||
|
||||
|
||||
let currentPetIndex = 0;
|
||||
|
||||
function renderPet() {
|
||||
if (currentPetIndex >= pets.length) {
|
||||
petCard.innerHTML = `<div class="card-body text-center"><h5 class="card-title">Больше питомцев нет!</h5></div>`;
|
||||
likeBtn.disabled = true;
|
||||
dislikeBtn.disabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const pet = pets[currentPetIndex];
|
||||
petCard.innerHTML = `
|
||||
<img src="${pet.photo_url}" class="card-img-top" alt="${pet.name}">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">${pet.name}</h5>
|
||||
<p class="card-text"><strong>Возраст:</strong> ${pet.age}</p>
|
||||
<p class="card-text"><strong>Порода:</strong> ${pet.breed}</p>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function showNextPet() {
|
||||
currentPetIndex++;
|
||||
renderPet();
|
||||
}
|
||||
|
||||
likeBtn.addEventListener('click', showNextPet);
|
||||
dislikeBtn.addEventListener('click', showNextPet);
|
||||
|
||||
renderPet();
|
||||
});
|
||||
85
login.php
Normal file
85
login.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) {
|
||||
header("location: index.php");
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once 'db/config.php';
|
||||
|
||||
$error = '';
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$username = trim($_POST['username']);
|
||||
$password = trim($_POST['password']);
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
$error = 'Please enter username and password.';
|
||||
} else {
|
||||
try {
|
||||
$conn = db();
|
||||
$sql = "SELECT id, username, password FROM users WHERE username = :username";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
if ($stmt->rowCount() == 1) {
|
||||
$row = $stmt->fetch();
|
||||
$id = $row['id'];
|
||||
$hashed_password = $row['password'];
|
||||
if (password_verify($password, $hashed_password)) {
|
||||
session_start();
|
||||
$_SESSION["loggedin"] = true;
|
||||
$_SESSION["id"] = $id;
|
||||
$_SESSION["username"] = $username;
|
||||
header("location: index.php");
|
||||
} else {
|
||||
$error = 'The password you entered was not valid.';
|
||||
}
|
||||
} else {
|
||||
$error = 'No account found with that username.';
|
||||
}
|
||||
} else {
|
||||
$error = "Oops! Something went wrong. Please try again later.";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = "Error: " . $e->getMessage();
|
||||
}
|
||||
$conn = null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Login - Pet Tinder</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 class="text-center mt-5">Login</h1>
|
||||
<?php if(!empty($error)): ?>
|
||||
<div class="alert alert-danger"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4">
|
||||
<div class="mb-3">
|
||||
<label for="username" class="form-label">Username</label>
|
||||
<input type="text" name="username" id="username" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="password" name="password" id="password" class="form-control">
|
||||
</div>
|
||||
<div class="d-grid">
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
</div>
|
||||
<p class="mt-3 text-center">Don't have an account? <a href="register.php">Sign up now</a>.</p>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
10
logout.php
Normal file
10
logout.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
$_SESSION = array();
|
||||
|
||||
session_destroy();
|
||||
|
||||
header("location: login.php");
|
||||
exit;
|
||||
?>
|
||||
88
my_pets.php
Normal file
88
my_pets.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
|
||||
header("location: login.php");
|
||||
exit;
|
||||
}
|
||||
|
||||
$user_id = $_SESSION['id'];
|
||||
$db = db();
|
||||
$stmt = $db->prepare("SELECT * FROM pets WHERE user_id = ? ORDER BY created_at DESC");
|
||||
$stmt->execute([$user_id]);
|
||||
$pets = $stmt->fetchAll();
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="referrerpolicy" content="no-referrer">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>My Pets</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="index.php">Tinder for Pets</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<?php if (isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="my_pets.php">My Pets</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="add_pet.php">Add Pet</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="logout.php">Logout</a>
|
||||
</li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="login.php">Login</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="register.php">Register</a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1 class="text-center mb-4">My Pets</h1>
|
||||
<div class="row">
|
||||
<?php if (empty($pets)): ?>
|
||||
<div class="col">
|
||||
<p class="text-center">You haven't added any pets yet. <a href="add_pet.php">Add one now!</a></p>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<?php foreach ($pets as $pet): ?>
|
||||
<div class="col-md-4 mb-4">
|
||||
<div class="card">
|
||||
<img src="<?= htmlspecialchars($pet['photo_url']) ?>" class="card-img-top" alt="<?= htmlspecialchars($pet['name']) ?>">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title"><?= htmlspecialchars($pet['name']) ?></h5>
|
||||
<p class="card-text">
|
||||
<strong>Breed:</strong> <?= htmlspecialchars($pet['breed']) ?><br>
|
||||
<strong>Age:</strong> <?= htmlspecialchars($pet['age']) ?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
71
register.php
Normal file
71
register.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
$error = '';
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$username = trim($_POST['username']);
|
||||
$password = trim($_POST['password']);
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
$error = 'Please enter username and password.';
|
||||
} else {
|
||||
try {
|
||||
$conn = db();
|
||||
$sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
|
||||
$stmt = $conn->prepare($sql);
|
||||
|
||||
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
$stmt->bindParam(':username', $username);
|
||||
$stmt->bindParam(':password', $hashed_password);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
header("location: login.php");
|
||||
} else {
|
||||
$error = "Something went wrong. Please try again later.";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
if ($e->errorInfo[1] == 1062) { // Duplicate entry
|
||||
$error = "This username is already taken.";
|
||||
} else {
|
||||
$error = "Error: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
$conn = null;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Register - Pet Tinder</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 class="text-center mt-5">Register</h1>
|
||||
<?php if(!empty($error)): ?>
|
||||
<div class="alert alert-danger"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="mt-4">
|
||||
<div class="mb-3">
|
||||
<label for="username" class="form-label">Username</label>
|
||||
<input type="text" name="username" id="username" class="form-control">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="password" name="password" id="password" class="form-control">
|
||||
</div>
|
||||
<div class="d-grid">
|
||||
<button type="submit" class="btn btn-primary">Register</button>
|
||||
</div>
|
||||
<p class="mt-3 text-center">Already have an account? <a href="login.php">Login here</a>.</p>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
34
setup.php
Normal file
34
setup.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
require_once 'db/config.php';
|
||||
|
||||
try {
|
||||
$conn = db();
|
||||
|
||||
$sqlUsers = "CREATE TABLE IF NOT EXISTS users (
|
||||
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(30) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
)";
|
||||
$conn->exec($sqlUsers);
|
||||
echo "Table 'users' created successfully or already exists.<br>";
|
||||
|
||||
$sqlPets = "CREATE TABLE IF NOT EXISTS pets (
|
||||
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT(6) UNSIGNED NOT NULL,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
age INT(3) NOT NULL,
|
||||
breed VARCHAR(50) NOT NULL,
|
||||
photo_url VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
)";
|
||||
$conn->exec($sqlPets);
|
||||
echo "Table 'pets' created successfully or already exists.<br>";
|
||||
|
||||
} catch(PDOException $e) {
|
||||
echo "Error: " . $e->getMessage();
|
||||
}
|
||||
|
||||
$conn = null;
|
||||
?>
|
||||
Loading…
x
Reference in New Issue
Block a user