SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: User ID in URL

  1. #1
    SitePoint Enthusiast BMorganVA's Avatar
    Join Date
    Mar 2010
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    User ID in URL

    I want people to be able to share there public pages with other people, but I have no skill how to do this. I'll explain this in parts.

    First, I want people friends to be able to access the user public page by going "http://domainname.com/USERNAME" or by going "http://domainname.com/user.php?id=12345". I'd preferably use the first option, but I know that may be too complicated.

    Second, I have no clue how to make this code.

    Could someone please help me or guide me. I know that isn't what the forums are for, but I'd greatly appreciate it.

    To sum this up, could someone please help me make a public page for a user with either a "http://domainname.com/USERNAME" URL (first choice) or a "http://domainname.com/user.php?id=12345" URL? Thanks for the help!

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In order to understand the first choice you need to consider the third choice:

    http://domainname.com/user.php?username=someusername

    So it is like second choice only instead if id you use username as the identifier. You then select your data from database using WHERE username='$_GET['username']

    username is usually just as unique as the id, so it will work.

    Then you can make the first choice http://domainname.com/USERNAME and then
    just use rewrite rule to redirect to the third choice
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use mod_rewrite

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This came up from google feeling lucky, should help you get the urls sorted.
    http://www.roscripts.com/Pretty_URLs...iting-168.html

    To sum this up, could someone please help me make a public page for a user with either a "http://domainname.com/USERNAME" URL (first choice) or a "http://domainname.com/user.php?id=12345" URL? Thanks for the help!
    So the url bit is optional, what do you want help with here? If you have absolutely no idea what to do then google for some basic turorials. Otherwise, have a go, and come back here with some code when you get stuck.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2010
    Posts
    153
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've been trying to do this exact thing as well, and it's surprisingly hard to google for.

    I want to have a user page that displays different data depending on the username.

    I thought I had it figured out with this code at the top of my main.php page.

    PHP Code:
    <?php
    session_start
    ();
    if (isset(
    $_SESSION['username'])) {
    $user $_SESSION['username'];
    }
    else
    $user '';
    ?>
    and then for the link going to my user.php page I did this:

    PHP Code:
    <?php echo '<a href="user.php?user=' $user '">' ?>User Page</a>
    which works great for someone who is logged in, but 1) I think it's just the incorrect way to go about it and 2) it is completely useless for someone who ISN'T logged in trying to view someones user page.

    maybe we can both get help with this

  6. #6
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can write URLs like

    somesite/user.php/some_identifier

    without using URL rewriting. The $_SERVER["PATH_INFO"] contains the stuff that comes after "user.php" (your script), in this case it contains "/some_identifier" and you can parse that and do what you want with it, just like you would with a $_GET variable.

    Now, in some really fancy apps, you might have

    somesite/controller.php/user/{user_id}

    in this case, "controller.php" is a "superscript" that renders all (or many) of the pages on your site, and it's smart enough to see that the PATH_INFO starts with /user/, and pass the job to some component that reads the {user_id} and then draws the right page. There all kinds of architectural advantages of this, and its the way that modern 'MVC' webapps work.

    Now, if you don't like the ".php" there are a bunch of ways to make that go away.

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @oknow,

    $_SESSION variables create nightmares for people who don't really understand what they do. You'd do much better as a PHP beginner if you pretended that they don't exist, trust me.

    In your case, just use good 'old $_GET instead of $_SESSION. $_GET captures any variables that are defined in the URL and does the job nice and easy.

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ontology2 View Post
    @oknow,

    $_SESSION variables create nightmares for people who don't really understand what they do. You'd do much better as a PHP beginner if you pretended that they don't exist, trust me.
    Well said

  9. #9
    SitePoint Enthusiast BMorganVA's Avatar
    Join Date
    Mar 2010
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm probably going to try the second option before the first one. Thanks for all your help!

  10. #10
    SitePoint Enthusiast BMorganVA's Avatar
    Join Date
    Mar 2010
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the first redirect code.

    Code:
    RewriteEngine  on
    RewriteRule ^/social/([A-Za-z]+,[0-9]+)$ /user.php?id=$1
    See my Regex Help thread for more on that.

    Can someone help me with getting the information from the database? The not logged in user will enter http://domainname.com/USERNAME, then they will be redirected to http://domainname.com/user.php?user=USERNAME. How do I get the information for USERNAME? I don't understand how I would do that.

    Thanks for the help!


  11. #11
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BMorganVA View Post
    I have the first redirect code.

    Code:
    RewriteEngine  on
    RewriteRule ^/social/([A-Za-z]+,[0-9]+)$ /user.php?id=$1
    See my Regex Help thread for more on that.

    Can someone help me with getting the information from the database? The not logged in user will enter http://domainname.com/USERNAME, then they will be redirected to http://domainname.com/user.php?user=USERNAME. How do I get the information for USERNAME? I don't understand how I would do that.

    Thanks for the help!

    I replied to the regex thread.

    It's important that you realize users are not redirected but just 'rewritten' as if they had requested a different URL.

    To get the data from the database, check out the manual on mysql_query() and related functions.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  12. #12
    SitePoint Enthusiast BMorganVA's Avatar
    Join Date
    Mar 2010
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I posted the reg. code in the Regex Help thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •