robabby
robabby

Reputation: 2200

Add 'username' to URL from 'user_id' with PHP for Dummies

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

Get username from URL in PHP

Using mod rewrite to change URL with username variable

AddedBytes Article

URL Rewriting for Beginners

Upvotes: 2

Views: 1699

Answers (1)

jeroen
jeroen

Reputation: 91734

This line is wrong according to your information:

RewriteRule ^user/([^/]+)$ index.php?user_id=$1

It should at least be:

RewriteRule ^user/([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/user/username

or using your description:

RewriteRule ^([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/username

Also, you are not setting any variable with the name of username in the code you have shown, so the check for $_GET['username'] is unnecessary.

Your check in php should look something like:

if ( isset($_GET['user_id']) && queryUserId($_GET['user_id']) ) {

Apart from that you should not use the deprecated mysql_* functions and use prepared statements as you have an sql injection problem now.

Also note that using two return statements after each other only returns the first value.

Edit: There seems to be some confusion between the user ID and the username. If the value in the url is a username, you'd better call it username in both the .htaccess file and php to avoid confusion with the user ID:

RewriteRule ^([^/]+)$ /user/index.php?username=$1    // for a url like domain/username}

and

if ( isset($_GET['username']) && queryUserName($_GET['username']) ) {

in the function (using the deprecated functions just to illustrate...):

function queryUserName($username) {        
    $conn = dbConnect('read');
    $sql = "SELECT * FROM users WHERE username = '".$username."'";
    ...
}

Upvotes: 3

Related Questions