Skip to content

Commit aabd5ef

Browse files
committed
Any profile page now always viewable
Add feature for any profile page to be viewable by any user, including a user who is not signed in. If the user viewing a profile page is not the owner of that profile page, then the ability for editing the user profile is disabled. User can only edit their own profile.
1 parent e9857d5 commit aabd5ef

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

fetchProfile.php

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ function fetchProfile($email){
1414
$_SESSION["about"] = $userProfile["about"];
1515
mysqli_free_result($resultProfile);
1616
}
17+
function getProfileByID($ID){
18+
include "server.php";
19+
$profileQuery = "SELECT * FROM users where userID = '$ID'"; //Fetches user info from database.
20+
21+
$resultProfile = mysqli_query($db, $profileQuery);
22+
$userProfile = mysqli_fetch_assoc($resultProfile); //retrieves all user data stored in the result from SQL query
23+
mysqli_free_result($resultProfile);
24+
25+
return $userProfile; //returns entire user profile (without image)
26+
}
1727

1828
function fetchProfilePic($username) {
1929
include "server.php";

index.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<?php
173173
if (isset($_SESSION["userID"])){
174174
echo("<a href=\"logout.php\" class=\"right\">Logout</a>");
175-
echo("<a href=\"#\" class=\"right\">". $_SESSION["username"] ."</a>");
175+
echo("<a href=\"profile.php?userID=". $_SESSION["userID"] ."\" class=\"right\">". $_SESSION["username"] ."</a>");
176176
}
177177
else{
178178
echo("<a href=\"registration.php\" class=\"right\" >Register</a>");
@@ -214,8 +214,8 @@
214214
echo("
215215
<div class=\"col-sm-3 well\">
216216
<div class=\"well\">
217-
<p><a href=\"#\">". $_SESSION["username"] ."</a></p>
218-
<img class=\"img-circle\" src=\"data:image/jpeg;base64, " . fetchProfilePic($_SESSION["username"]) . "\" alt=\"Profile Picture\" height=\"65\" width=\"65\">
217+
<p><a href=\"profile.php?userID=". $_SESSION["userID"] ."\">". $_SESSION["username"] ."</a></p>
218+
<a href=\"profile.php?userID=". $_SESSION["userID"] ."\"><img class=\"img-circle\" src=\"data:image/jpeg;base64, " . fetchProfilePic($_SESSION["username"]) . "\" alt=\"Profile Picture\" height=\"65\" width=\"65\"></a>
219219
220220
</div>
221221
<div class=\"well\">

printQandA.phtml

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ $result_numAnswers = mysqli_query($db, $numAnswersQuery);
77
$ans = mysqli_fetch_assoc($result_numAnswers);
88
$row_answers = mysqli_num_rows($result_numAnswers);
99

10-
$asker_query = "SELECT username FROM users where userID = '$currentQ[asker]'";
10+
$asker_query = "SELECT username, userID FROM users where userID = '$currentQ[asker]'";
1111
$asker_result = mysqli_query($db, $asker_query);
1212
$askerName=mysqli_fetch_assoc($asker_result);
1313

@@ -21,8 +21,9 @@ $askerName=mysqli_fetch_assoc($asker_result);
2121
<!-- </div>-->
2222
<div class="col-sm-2" style="margin-left: 30px">
2323
<div class="well">
24-
<p><?php echo($askerName['username']); ?></p>
25-
<img class="img-circle" src="data:image/jpeg;base64, <?php echo fetchProfilePic($askerName['username']) ?>" alt="Profile Picture" height="55" width="55">
24+
<p><?php echo("<a href=\"profile.php?userID=". $askerName["userID"] ."\">". $askerName["username"] ."</a>"); ?></p>
25+
<a href="profile.php?userID=<?php echo $askerName["userID"]?>"><img class="img-circle" src="data:image/jpeg;base64, <?php echo fetchProfilePic($askerName['username']) ?>" alt="Profile Picture" height="55" width="55"></a>
26+
<p style="margin-top: 10px;">Asked:</p>
2627
</div>
2728
</div>
2829
<div class="col-sm-9">

profile.php

+35-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
<?php
2-
include_once "server.php";
3-
session_start(); //start a new session if not already started
4-
if (!isset($_SESSION["username"])){
2+
include_once "server.php";
3+
session_start(); //start a new session if not already started
4+
$editable = false;
5+
if (isset($_GET['userID'])){
6+
if ($_GET['userID'] != "") {
7+
if (isset($_SESSION["username"])) {
8+
if ($_SESSION["userID"] === $_GET['userID']) {//Check if the current signed-in user is the same as the user of the profile page
9+
$editable = true; //if the current signed-in user is the same as the user of the profile page, profile will be editable
10+
}
11+
}
12+
$profileID = $_GET['userID'];
13+
}
14+
else{//Else no userID in URL for profile page, redirect to homepage
15+
header('location: index.php');
16+
}
17+
}
18+
19+
else{//Else no userID in URL for profile page, redirect to homepage
520
header('location: index.php');
6-
}
7-
include 'fetchProfile.php';
8-
include 'updateProfile.php';
9-
fetchProfile($_SESSION['email']);
10-
$memberSince = strtotime($_SESSION["time_created"]);
21+
}
22+
23+
24+
25+
include 'fetchProfile.php';
26+
include 'updateProfile.php';
27+
$profile = getProfilebyID($profileID);
28+
29+
$memberSince = strtotime($profile["time_created"]);
1130
?>
1231

1332

@@ -198,7 +217,7 @@
198217
<div class="panel panel-default">
199218

200219
<div class="panel-heading">
201-
<img class="card-img-top" id="profilePic" src="data:image/jpeg;base64, <?php echo fetchProfilePic($_SESSION["username"]); ?>" alt="Profile Picture" style="width:75%; max-width: 300px; max-height: 300px;">
220+
<img class="card-img-top" id="profilePic" src="data:image/jpeg;base64, <?php echo fetchProfilePic($profile["username"]); ?>" alt="Profile Picture" style="width:75%; max-width: 300px; max-height: 300px;">
202221
<div id="editImage" hidden>
203222
<div class="row justify-content-center" style="margin-top: 5px">
204223
<div class="col-sm-3"></div>
@@ -214,7 +233,7 @@
214233
</div>
215234
</div>
216235
<div id="username">
217-
<h2><?php echo $_SESSION["username"]; ?></h2>
236+
<h2><?php echo $profile["username"]; ?></h2>
218237
</div>
219238
<div style="display: none" id="usernameText" class="row justify-content-center">
220239
<div class="col-sm-4">
@@ -223,27 +242,27 @@
223242
<div class="col-sm-8">
224243

225244
<textarea style="font-size: 18px; margin-top: 10px" id="newUsername" name="newUsername" class="form-control" rows="1"
226-
><?php echo $_SESSION["username"]; ?></textarea>
245+
><?php echo $profile["username"]; ?></textarea>
227246
</div>
228247
</div>
229248

230249
</div>
231250
<div class="panel-body">
232251
<h4>About me:</h4>
233-
<p id="aboutMe"> <?php echo $_SESSION["about"]; ?> </p>
234-
<textarea style="display: none; width: 80%" class="form-control" id="aboutText" name="aboutText" rows="3"><?php echo $_SESSION["about"]; ?></textarea>
252+
<p id="aboutMe"> <?php echo $profile["about"]; ?> </p>
253+
<textarea style="display: none; width: 80%" class="form-control" id="aboutText" name="aboutText" rows="3"><?php echo $profile["about"]; ?></textarea>
235254
<div class="modal-footer"></div>
236255
<h4>Email:</h4>
237-
<p> <?php echo $_SESSION["email"]; ?> </p>
256+
<p> <?php echo $profile["email"]; ?> </p>
238257
<div class="modal-footer"></div>
239258
<h4>User ID:</h4>
240-
<p> <?php echo $_SESSION["userID"]; ?> </p>
259+
<p> <?php echo $profile["userID"]; ?> </p>
241260
<div class="modal-footer"></div>
242261
<h4 class="title">Registered Tech Hut User <span style="color: rgb(15, 184, 23)">&#10003;</span></h4>
243262
<h4>Member Since:</h4>
244263
<p> <?php echo date('m/d/Y', $memberSince); ?> </p>
245264
</div>
246-
<div class="panel-footer" >
265+
<div class="panel-footer" <?php if(!$editable) {echo "style=\"display:none;\"";} ?> >
247266
<div class="row justify-content-center">
248267
<div class="col-sm-3"></div>
249268
<div class="col-sm-6">

updateProfile.php

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
$updateNoPic = "UPDATE users SET username = '$newUsername', about = '$newAbout' WHERE users.userID = $userID";
3131
mysqli_query($db,$updateNoPic);
3232
}
33+
//Update the data stored in the session
34+
$_SESSION['username'] = $newUsername;
35+
3336
}
3437
}

0 commit comments

Comments
 (0)