how to use username in url

Discussion in 'PHP' started by balaji153, Jan 22, 2010.

  1. #1
    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?
     
    balaji153, Jan 22, 2010 IP
  2. Djkanna

    Djkanna Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    you could use mod-rewrite for the pretty url.

    http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

    This may help you :)

    Thanks

    Djkanna :)
     
    Djkanna, Jan 22, 2010 IP
  3. balaji153

    balaji153 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if i use mod_rewite

    to convert mydomin.com/profile.php?id=12345

    mydomin.com/profile/balaji

    how can i use get method to fetch user information from database
     
    balaji153, Jan 22, 2010 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    RewriteRule ^profile/([a-z]+)$ profile.php?name=$1
    Code (markup):
    $clean_name = preg_replace('/[^a-z]/', '', trim($_GET['name']));
    $sql = "select * from users where name='{$clean_name}'";
    PHP:
     
    SmallPotatoes, Jan 22, 2010 IP
  5. balaji153

    balaji153 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Get method work with only if domain look like this?

    mydomain.com?name=balaji

    $get['name']

    if we don't use name variable in domain how get method will work?
     
    balaji153, Jan 22, 2010 IP
  6. RDJLabs

    RDJLabs Peon

    Messages:
    170
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Instead of using username in url its better to use session to pass the variables.

    
    $_SESSION["uname"]="yourusername";
    
    PHP:
     
    RDJLabs, Jan 22, 2010 IP
  7. balaji153

    balaji153 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    if i use session ,then only logged in user can view their profile!!!!
     
    balaji153, Jan 22, 2010 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It works because the RewriteRule that I posted takes the username off the end of the URL and inserts it into the GET arguments list. Why not try it?
     
    SmallPotatoes, Jan 24, 2010 IP
  9. balaji153

    balaji153 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thanks @ SmallPotatoes:

    i tried its working but its working for all the file inside the folder!!!!

    please help me to fix this
     
    balaji153, Jan 25, 2010 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I think you are setting yourself up for namespace conflicts.

    If you want http://mydomain.com/profile/abcdef to open someone's profile, then don't put anything else in http://mydomain.com/profile/ because sooner or later you will have someone whose user name is the same as something you put in there.

    Either move everything else out of /profile or make the profiles display under /user_profile or something like that.
     
    SmallPotatoes, Jan 25, 2010 IP