控制用户是否使用了$ _GET ['? ? '] URL方法

时间:2018-07-03 09:41:06

标签: php get

我试图控制用户是否键入任何GET URL链接,但是仅当我指定$ _GET请求来重定向该URL时,我才能够。

例如,当我键入以下内容时:https://someurl.com?id=2我可以控制$ _GET,因为我知道调用了“ id”。

if(isset($_GET['id'])) {
  echo "URL DOES NOT EXIST";
  die();
}

因此,基本上,当用户键入其他任何字符而不是“ id”时,该消息将不会显示... 可以这样做吗?

2 个答案:

答案 0 :(得分:2)

您可以遍历$ _GET变量并检查其内容。

foreach ($_GET AS $Key => $Value) {
  echo "Set key: ".$Key;
  echo "Set value: ".$Value;
}

或者也许只是检查是否设置了$ _GET变量:

if(!empty($_GET))
  echo "URL DOES NOT EXIST";

答案 1 :(得分:0)

如果我理解您的问题,according to your comment,这就是您要执行的操作:

<?php
$arr = array(1, 2, 3);
if (isset($_GET['id']) && !empty($_GET['id'])) {
    // If "id" is set and not empty, do:
    $id = $_GET['id'];
    if (in_array($id, $arr)) {
        // If "id" is in array of available ids
        // Page exists
        echo "Hello World";
    } else {
        // Page does not exists
        echo "Page does not exists";
    }
} else {
    // Page does not exists
    echo "Page does not exists";
}

这将导致:

index.php?id=1  // "Hello World"
index.php?id=27 // Page does not exists
index.php?id=   // Page does not exists
index.php       // Page does not exists

我不建议使用数组作为页面ID,而应使用数据库(mysql / mariadb)