This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
129 lines (115 loc) · 4.58 KB
/
profile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
session_start();
require_once 'includes/db.php';
require_once 'includes/functions.php';
if (!isset($_SESSION['user_id'])) {
header("Location: index.php");
exit();
}
$user_id = $_SESSION['user_id'];
// Fetch user information
$sql = "SELECT username, email, profile_picture FROM users WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$profile_picture = $user['profile_picture'] ?: 'uploads/default_profile.png';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
$email = $_POST['email'];
$password = !empty($_POST['password']) ? password_hash($_POST['password'], PASSWORD_BCRYPT) : null;
$sql = "UPDATE users SET username = ?, email = ?" . ($password ? ", password = ?" : "") . " WHERE id = ?";
$stmt = $conn->prepare($sql);
if ($password) {
$stmt->bind_param("sssi", $username, $email, $password, $user_id);
} else {
$stmt->bind_param("ssi", $username, $email, $user_id);
}
if ($stmt->execute()) {
echo "User information updated successfully.";
} else {
echo "Error updating user information: " . $stmt->error;
}
if (!empty($_FILES['profile_picture']['name'])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["profile_picture"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["profile_picture"]["tmp_name"]);
if ($check === false) {
echo "File is not an image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["profile_picture"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["profile_picture"]["tmp_name"], $target_file)) {
$sql = "UPDATE users SET profile_picture = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target_file, $user_id);
if ($stmt->execute()) {
echo "Profile picture updated successfully.";
} else {
echo "Error updating profile picture: " . $stmt->error;
}
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
// Reload the page to reflect the changes
header("Location: profile.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile - Memory Card Game</title>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/profile.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body style="display: unset !important;">
<?php include 'includes/navbar.php'; ?>
<div class="container">
<h2>Profile</h2>
<form action="profile.php" method="post" enctype="multipart/form-data">
<div>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="<?php echo $user['username'] ?? ''; ?>" required>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?php echo $user['email'] ?? ''; ?>" required>
</div>
<div>
<label for="password">Password (leave blank to keep current):</label>
<input type="password" id="password" name="password">
</div>
<div>
<label for="profile_picture">Profile Picture:</label>
<input type="file" id="profile_picture" name="profile_picture">
<img src="<?php echo htmlspecialchars($profile_picture); ?>" alt="Profile Picture" width="100">
</div>
<button type="submit">Update Profile</button>
</form>
</div>
</body>
</html>