how to use username in url

4 replies
Hi all i am creating small webapp with php

where mydomain.com/profile.php?id=12345

this was users profile page ,and then with get method ,i fetch user data from database as below code

$userid=$_GET['id'];
$qry="select * from `users` where userid ='$userid'";
echo $qry;
$result=mysql_query($qry);
$userinfo = mysql_fetch_array($result);
$u_name=$userinfo['name'];
$u_userid=$userinfo['userid'];
$u_gender=$userinfo['gender'];


but i want my url like this mydomain.com/username

without .php or anything can any one help me to fix this

get method doesn't work here any other option?
#url #username
  • Profile picture of the author aaron_nimocks
    If you want your domain exactly like that you need to use .htaccess and MOD REWRITE. I am no pro with that stuff but thats the direction you need to go if you want to google it.
    Signature

    My free PSD logs can be downloaded at PSD Bum. Enjoy!

    {{ DiscussionBoard.errors[1692677].message }}
  • Profile picture of the author Aaron Sustar
    This should be fairly simple to achieve with some .htaccess magic.

    Step 1: Create a ".htaccess" file and place it in the"public_html" folder on your server. Yes, that's a file without a name, it only has the "htaccess" extension. And it's a text file.

    Step 2: Put the following lines into your .htaccess file:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^([0-9]+)/user/[0-9a-zA-Z_\-]+/$ profile.php?id=$1
    </IfModule>

    That's it.

    Now you can use "mydomain.com/12345/user/aaron_nimocks" instead of "mydomain.com/profile.php?id=12345".

    Hope that helps.
    {{ DiscussionBoard.errors[1692883].message }}
  • Profile picture of the author theIMgeek
    With Aaron's instructions above you will be able to get the pretty url of mydomain.com/12345/user/username but it takes a couple extra steps to get the mydomain.com/username address you were originally hoping for.

    Based on what Aaron outlined, change the htaccess rules like this...

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^([0-9a-zA-Z_\-])+/$ profile.php?username=$1
    </IfModule>

    Now, this requires a modification to your profile.php script, since the original requires the user ID variable, but with this the ID is not known.

    $username=$_GET[username'];
    $qry="select * from `users` where username ='$username'";

    That will pull the record based on a username match. NOTE: I am assuming your database table has a column named username. That may need to be changed. Also, it assumes that the username is unique. (if it's not unique, then this whole plan falls apart)

    Speaking of "whole plan falling apart", with this setup mydomain.com/anything is taken as an attempt to pull up the username. That's probably why Aaron added in the /user/ part. If it was mydomain.com/user/username that would prevent conflicts with the rest of your site directories. In that case change the rewrite rule like this...

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^user/([0-9a-zA-Z_\-])+/$ profile.php?username=$1
    </IfModule>

    Hope you can get this working as you desire.

    -Ryan
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[1694095].message }}
  • Profile picture of the author m4rx
    I acctually did something like this for one of my projects.

    I used:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^profile/([0-9]+)/?$ ./index.php?action=profile&id=$1 [L,QSA]
    RewriteRule ^profile/(.*)/?$ ./index.php?action=profile&member=$1 [L,QSA]
    </IfModule>

    Then I would leave it up to the php script to handles the rest.
    I used:
    if(isset($_GET['member'))
    $member = $db->myEscape($_GET['member']);
    $use = 'member';
    elseif(isset($_GET['id']))
    $member = $db->myEscape($_GET['id']);
    $use = 'id';
    else
    $member = $db->myEscape($_GET['member']);
    $use = 'member';
    Then I would have the information that I need, and I could just plug that into a mysql statement.
    $sql = "SELECT * FROM `users` WHERE '".$use."' = '".$member."'";

    Probably not the best way to do things, but it worked for what I needed it for.

    You could also just use PHP all the way.
    if(is_int($_GET['member'])) // it's an ID
    $sql = "SELECT * FROM users WHERE id =".$_GET['member'];
    else // Its a username
    $sql = "SELECT * FROM users WHERE name ='".$_GET['member']."'";

    If you need anymore help, feel free to PM me.

    --m4rx
    Signature
    We are what we repeatedly do. Excellence, then, is not an act, but a HABIT. ~Aristotle
    Bored. Learn everything you need to know about Organic Gardening.
    {{ DiscussionBoard.errors[1720912].message }}

Trending Topics