get someting from url.

Discussion in 'PHP' started by izlik, Jan 27, 2010.

  1. #1
    Hello

    I was wondering how i can getsometing from the url into a variable ? let's say i have this url http://domain.com/1726 now i want 1726 into $number. How can this be done ?? it should be for anyting that comes after the /
     
    izlik, Jan 27, 2010 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    You can do it with simple function.

    $number = basename("http://domain.com/1726");
    PHP:
     
    Kaizoku, Jan 27, 2010 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    ok, but if the number is someting else, it varies? 1726 for now, then next time the page is loaded it's 1912 etc.
     
    izlik, Jan 27, 2010 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    basename will get anything after last trailing slash.

    If you want to work on the Current url, then

    
    $number = basename('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    
    PHP:
     
    Kaizoku, Jan 27, 2010 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    got it to work, thanks a lot! =)
     
    Last edited: Jan 27, 2010
    izlik, Jan 27, 2010 IP
  6. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #6
    i found another question now i need to ask.

    this is my current code.

    <body bgcolor="Black">
    <? include 'head.php'; ?>
    
    <?
    $number = basename('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    
    
    ?>
    <center>
    <div id="flashloop">
    		<object type="application/x-shockwave-flash" data="<? echo "/flash/loops/".basename($number).""; ?>.swf" width="640" height="480">
    
    			<param name="autostart" value="true" />
    			<param name="src" value="<? echo "/flash/loops/".basename($number).""; ?>.swf" />
    		</object>
    	</div>
    </center>
    <center>   
    Code (markup):
    now, if i link mydomain.com/index.php/765 $number ofcourse becomes 765. BUT, if link mydomain.com/index.php/ can we make it put someting after the / by default if there is nothing there ?
     
    izlik, Jan 27, 2010 IP
  7. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #7
    what "something" you want to put?
     
    Kaizoku, Jan 27, 2010 IP
  8. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #8
    let's say if there is nothing there after the / i want it to default to to 1. so it becomes mydomain.com/index.php/1
     
    izlik, Jan 27, 2010 IP
  9. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #9
    
    $number = basename('http://'.$_SERVER['HTTP_HOST'].($_SERVER['REQUEST_URI'] == '/' ? 1 : $_SERVER['REQUEST_URI']));
    
    PHP:
     
    Kaizoku, Jan 27, 2010 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    JAY6390, Jan 27, 2010 IP