Jump to content

A question about URLs


sazzle

Recommended Posts

Hello,

 

I'm new to this forum and reasonably new to PHP as well!  Please forgive me if my question is a simple one.

 

Briefly, here is what I am trying to achieve: a website where a user signs up, logs in, completes a profile, and can choose whether or not they want to make their profile public (visible to anyone who has the correct URL).

 

I would like a user's public profile to have a URL of this format: domain.com/username.  Then, that page should recognise which user's profile the visitor is looking for, and, if that user's profile is public, display the profile or, if the user's profile is not public, display an error message.

 

I don't know how to get from domain.com/username to being able to run a query using that username.  Perhaps there will be some .htaccess stuff involved to allow visitors to type domain.com/username instead of domain.com/profile?username=username ??  I'm not sure.  Any help or pointing in the right direction would be very much appreciated!

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/240193-a-question-about-urls/
Share on other sites

You need to use mod_rewrite to internally rewrite the URL. While the user sees "domain.com/username", it's actually pointing to "domain.com/profile.php?username=username". This is actually quite a common request, and the very basic rewrite rule would be:

 

RewriteEngine on
RewriteRule ^[a-z0-9]+$ profile.php?username=$1 [L]

 

Be sure to modify the regex to allow whatever characters/format your usernames are made up of.

Thank you for that code - I've now tried it out and it works perfectly.  I have one other question.

 

I need to get the username from the URL (which is now displayed as domain.com/username instead of domain.com/profile.php?username=username) to use in a mysql query to display the correct information in the profile.  I tried setting $username = $_GET['username'] but that doesn't work when viewing the page as domain.com/username.  Is there another way in which I can achieve this?

 

Thank you and again I apologise if my question is a simple one.

When I view the profile page as domain.com/profile.php?username=sarah and do <?php print_r($_GET); ?> the following is displayed:

 

Array ( [username] => sarah )

 

When I view the profile page as domain.com/sarah and do <?php print_r($_GET); ?> the following is displayed:

 

Array ( [username] => )

 

Thank you for helping me!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.