I am attempting to publish a question which I know has many answers floating around Stack Overflow. However I, for some reason, just cannot seem to get the information in them to click. I will post my sources at the end of the question.
The question being, how can I pull a user_id
from a URL & mod_rewrite it show the username.
Instead of this:
domain/user/index.php?user_id=1
I get this:
domain/username
I had customized other SO answers to my needs on my .htaccess file to this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteOptions MaxRedirects=1
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^user/([^/]+)$ index.php?user_id=$1
</IfModule>
The if
statement at the top of my user page looks like this:
if (isset($_GET['user_id']) || isset($_GET['username']) && queryUserId($user_id)) {
// Render Page Content
} else {
header('Location: sign_up.php');
}
And my queryUsername
functions looks like this:
function queryUsername($username) {
$conn = dbConnect('read');
$sql = "SELECT * FROM users WHERE user_id = '".$username."'";
$result = $conn->query($sql) or die(mysqli_error($conn));
$row = $result->fetch_assoc();
return $row['username'];
$username = $row['username'];
}
I have successfully implemented unique URL's for users, and am accessing Account Profiles just fine when appending different user_id's to my URL, so what am I missing to get the username written to the URL instead of variable strings and folder structure?
Cheers!
Other SO Questions:
Directly adding username to URL PHP
Using mod rewrite to change URL with username variable
AddedBytes Article