php url queston

Discussion in 'PHP' started by izlik, Mar 21, 2010.

  1. #1
    the code below sets $number to anyting that is after the/ of of my domain. i wonder how i can make it so that if there is nothing after the / $number automaticlly is set to 1.

    $number = basename('http://'.$_SERVER['HTTP_HOST'].($_SERVER['REQUEST_URI'] == '/' ? 1 : $_SERVER['REQUEST_URI']));
     
    izlik, Mar 21, 2010 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    Try this:
    $number = basename($_SERVER['REQUEST_URI'] == '/' ? 1 : $_SERVER['REQUEST_URI']);
    PHP:
     
    Lordo, Mar 21, 2010 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    well, that works kinda. what i want is so that if my url is for example http://mydomain.com/2 $number becomes 2 and so on, however if there is nothing behind the / $number becomes 1.
     
    izlik, Mar 22, 2010 IP
  4. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It'll try to load a directory if it's a /1 or /2 etc..

    You will need some .htaccess to redirect it to a php file to handle it. :)
     
    Wrighty, Mar 23, 2010 IP
  5. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #5
    Yes that is by using rewrite rules in htaccess
     
    Lordo, Mar 23, 2010 IP
  6. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #6
    how would i be using rewrite rules in htaccess to achive this ? i havent used .htaccess very much :/
     
    izlik, Mar 23, 2010 IP
  7. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #7
    anyone that could help me please ? :/
     
    izlik, Mar 24, 2010 IP
  8. foxbsd

    foxbsd Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you can used with regular expression , it's simple
     
    foxbsd, Mar 25, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    $number = (is_numeric(basename($_SERVER['REQUEST_URI']))) ? basename($_SERVER['REQUEST_URI']) : 1;
    PHP:
     
    danx10, Mar 25, 2010 IP
  10. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #10
    It goes in your .htaccess file like this:
    RewriteEngine ON
    RewriteRule ^([0-9]+)$ index.php?number=$1 [L]
     
    Lordo, Mar 25, 2010 IP