2011-09-24 46 views
0

少变量我注意到,而不是使用:

http://yoursite.com/index.php?page=4

我可以只使用:

http://yoursite.com/index.php?4

如何在PHP中做到这一点, JavaScript的?

+0

你为什么要这么做? – BRampersad

回答

4

在您发布的示例中,现在4是关键值而不是值。

如果你只通过查询字符串传递一个参数,这是可行的,否则你不会在PHP中知道什么是没有你的参数的关键。

在具体的例子中,你可以得到它像这样在PHP

$id = null 
if(!empty($_GET)){ 
    $keys = array_keys($_GET); 

    // verify that the first key contains only digits, and use it as id 
    // ctype_digit does not work here because the key is now an int so use is_numeric 
    if(is_numeric($keys[0])) 
     $id = $keys[0]; 
} 

我不知道你想在Javascript中做这里。

另外,如果你想生成SEO友好的网址,我建议你看看URL Rewriting

+0

这不是一个建议,不是吗? – hakre

3
$page = $_SERVER['QUERY_STRING'];