开发者

How to use less variables with $_GET

开发者 https://www.devze.com 2023-04-06 23:26 出处:网络
I\'ve noticed that instead of using: http://yoursite.com/index.php?page=4开发者_高级运维 I could just use:

I've noticed that instead of using:

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

开发者_高级运维

I could just use:

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

How to do this in php and JavaScript?


In the example you have posted, now 4 is the key instead of value.

If you are passing ONLY one parameter via query string this is doable,otherwise you wont know in PHP what is what without a key for your parameter.

In the particular example you can get it like this in 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];
}

I do not know what do you want to do in Javascript here.

Also if you are trying to generate SEO friendly URLs I would suggest that you look into URL Rewriting.


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

精彩评论

暂无评论...
验证码 换一张
取 消