Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP routing?

  1. #1
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts

    PHP routing?

    Hi guys!

    I'm looking into routing with php, but have no idea where to start!

    I was hoping someone might be able to explain it a little to me?

    The way I imagine it working would be something like www.mydomain.com/myuser

    And that would point to my user page. My question is how would I pull the user data from the db?

    Whenever I extract data on a page I use url strings like userpage.php?id=1, and this would get me user id 1 from my database.

    but the page is called userpage.php. How can I get www.mydomain.com/myuser to work?

    I guess the best example I can think of off the top of my head would be facebook. If you google your name + facebook, you'll get results with www.facebook.com/yourname, and this will take you to your public page.

    This is the same sort of thing I would like to know how to do, but just cant see how it works without going to a specific page like userpage.php?id=1

    I just need to know the logic behind how the url www.mydomain..com/username works. I've done a bit of googling but the tutorials I've found aren't very clear for someone trying to learn the process, or maybe I'm just being dumb!

    Has anyone ever done this sort of thing before and how does it work? It's something I would like to learn how to do, so any good articles or advise form anyone would be very gratefully recieved as always!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,466
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    You need to use Google to find: .htaccess rewrite rules

    You'll use .htaccess to take the portion /username and rewrite it
    to send it as a variable to a PHP script. .htaccess instructs the
    server on how to process any URL's that it receives.

    Example:
    /johndoe will be posted to a PHP script as, index.php?id=johndoe

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not a PHP issue, moving to Apache Configurations forum.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mlseim View Post
    You need to use Google to find: .htaccess rewrite rules

    You'll use .htaccess to take the portion /username and rewrite it
    to send it as a variable to a PHP script. .htaccess instructs the
    server on how to process any URL's that it receives.

    Example:
    /johndoe will be posted to a PHP script as, index.php?id=johndoe
    Ah, I see, I've done some googling and I think I'm getting to grips with it.

    The problem I foresee is if I have two users with the same name, do you know how to handle this?

    I'm imagining my sql would be somethng like select * from user_table where username = '{$_GET['id']}' limit 1

    So if there are two users with the name johndoe, what would happen? I'm assuming it would select the first record?

    Do you hapen to know how to work around this?

    Thanks for the info though, a few more hours Googling and I might be a bit wiser!

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    Not a PHP issue, moving to Apache Configurations forum.
    Thanks for moving, didn't realise it was an Apache related problem, still quite new to php

    Cheers!

  • #6
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by mlseim View Post
    You need to use Google to find: .htaccess rewrite rules

    You'll use .htaccess to take the portion /username and rewrite it
    to send it as a variable to a PHP script. .htaccess instructs the
    server on how to process any URL's that it receives.

    Example:
    /johndoe will be posted to a PHP script as, index.php?id=johndoe
    Still not figuring this one out...

    I understand now that my url of www.example.com/some_page.php?id=joebloggs can be rewritten to www.example.com/joebloggs but I just can't figure out how to do it.

    I found a usefull online tool that rewrote it to www.example.com/joebloggs.html but ideally I'd want to drop the .html, and everything I changed seem to break it. I can't remember the url of the tool I used either.

    Can anyone help with this? Any usefull tuts for a complete beginner in Rewrites?

  • #7
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    closing this as follow up posted here


  •  

    Posting Permissions

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