How to get the number that comes after id ?

View link

http://lol.com/page.php/id1

Here it is

 if (isset($_GET['id'])) {$id =$_GET['id']; } else{ exit("Вы зашил на страницу без параметра!");} 

But does not work.

I need immeno

http://lol.com/page.php/id1

not

http://lol.com/page.php/id=1

  • explode break what is there to think then - johniek_comp
  • It is not clear))) More precisely, I know that explode breaks the text into parts, but I do not know how to do it - Sergey Ushakov
  • Check how the web server rewrites the request? What gets into $_GET when you request http://lol.com/page.php/id1 ? Put print_r ($ _GET) at the beginning of the script; exit (); and give the answer here. - Sergiks
  • Writes you have sewn onto a page without a parameter - Sergey Ushakov
  • Not put there. After exit(); nothing should be done. - Sergiks

6 answers 6

 $link = $_SERVER["REQUEST_URI"]; //получаем текущий uri if (isset($link])) { $link_id = substr($link, 13) //кол-во обрезаемых символов $id = $link_id; } else { exit("Вы зашил на страницу без параметра!"); } 
  • what's the point in this line? $ id = $ link_id; and the meaning of the condition? REQUEST_URI will always be that :) - johniek_comp
  • Parse error
  • The author needed the variable id, and the link really will always be (at least "/"), blunted - Demyan112rv
  • more logical condition if (strlen ($ link)> 0) - Demyan112rv pm
  • What do you have on line 9? syntax error, unexpected T_VARIABLE means that the action is performed with an unknown variable - Demyan112rv

Hey. Url needs to be parsed. You can try this way:

 <?php $url = "http://lol.com/page.php/id1"; // парсим url $url_array = explode("/", trim($url, "/")); // узнаем длину строки $len = strlen($url_array[4]); // выбираем все значение строки, без первых двух символов (id) $id = substr($url_array[4], 2, $len); // смотрим результат print $id; ?> 

It is also possible to bring all this into a separate function. I hope to be useful.

  • And how can I get url)))? - Sergey Ushakov
  • change the $ url line to: $ url = $ _SERVER ['REQUEST_URI']; - Lest4t
  • ATP)))))))) - Sergey Ushakov
  • This is good robs - Sergey Ushakov
 $e = explode("/", $_SERVER['REQUEST_URI']); echo $e[4]; 

the easiest way :)

    Well, firstly, GET parameters are passed after the sign?

    In your case http://lol.com/page.php?id=1

    I will propose another variant with regular expressions that will cover such cases as

     id1?wall id1#mail 

    Well, and so on.

     $re= "/\/id([0-9]++)/i"; preg_match_all($re, $url, $result); var_dump($result); 

      Like this:

       $url = "http://lol.com/page.php?id=126"; $parsed_url = parse_url($url); preg_match("/^id=([0-9]+)/", $parsed_url['query'], $matches); var_dump(end($matches));