How to $_GET something in an friendly URL

Discussion in 'PHP' started by TheDarkAssassin, Oct 28, 2009.

  1. #1
    Hello everybody, I am needing help...

    ...Lets say I have this url:

    www.exampledomain.com/pages/3

    As you can see, there is /pages/3

    I want to get the 3 and store it in a variable...


    Obviously $_GET['page'] won't work..What is best solution (I know of .htacess)
     
    TheDarkAssassin, Oct 28, 2009 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The variable used is defined in your .htaccess file. Check there or post it.
     
    nico_swd, Oct 28, 2009 IP
  3. TheDarkAssassin

    TheDarkAssassin Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey, thanks for your reply but I don't want to use .htaccess....Is there any other way APART from .htacess, I don't know .htacess very well.. :(
     
    TheDarkAssassin, Oct 28, 2009 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Well you need .htaccess to rewrite your URLs like this...

    Your question sounded like you already have those URLs and just problems getting the data out of it.
     
    nico_swd, Oct 28, 2009 IP
  5. TheDarkAssassin

    TheDarkAssassin Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am actually using a php framework for those types of urls :D

    yeha, I need to get the little 3 out of that url and store it in an $variable

    :)
     
    TheDarkAssassin, Oct 28, 2009 IP
  6. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #6
    Well, if not use .htaccess, then you can use frameworks such as symfony, zend framework or some others, they take care of extracting parameters from url. BUT if .htacess is hard for you, framework will surely be harder.

    Second possible solution - parse $_SERVER['REQUEST_URI']. Something like:
    
    $tokens = explode("/",$_SERVER['REQUEST_URI']);
    
    PHP:
    But still you'll have to use .htaccess to redirect all requests to your index.php or any other file where above code will be situated.
    Just like wordpress does:
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    
    Code (markup):
     
    AsHinE, Oct 28, 2009 IP
  7. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #7
    Looks like I'm late with my advice.
    If you are using a framework, I guess there are already some functions to extract these params from url.
     
    AsHinE, Oct 28, 2009 IP
  8. tonythetiger

    tonythetiger Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i agree if you are using a php framework it should have something built in. I use Zend framework occasionally and it gives me that information through $this->_request->getParam('PARAM NAME HERE') inside your "controller". obviously the framework would know that the "3" is supposed to be a "id" of some sort. so It might represent pageId, so you would say $this->_request->getParam('pageId') to get your value.
     
    tonythetiger, Oct 28, 2009 IP
  9. TheDarkAssassin

    TheDarkAssassin Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Okay, thanks for the help everybody..I'll use Tonythetiger's advice!
     
    TheDarkAssassin, Oct 29, 2009 IP
  10. w47w47

    w47w47 Peon

    Messages:
    255
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yep... .htaccess is the easiest way i think.
     
    w47w47, Oct 29, 2009 IP
  11. m30

    m30 Guest

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just use htaccess, google htaccess generator or something
     
    m30, Oct 29, 2009 IP