$ _GET多个选项创建链接

时间:2012-11-06 13:46:28

标签: php get

我使用$_GET[variable from link] - 方式传递用户在我的网页上选择的选项,例如每页帖子或后奥德。 我正在考虑写一个简单的变量,它会给我用户选择的选项,这样我就可以在我的header中使用它 - 重定向:

if ((isset($_GET['id'])) AND($_GET['id'] != 0 )) {  
    $topic_id = (int)$_GET['id'];  
    $header = 'Location: topic.php?id='.$topic_id;  
}  
        else {  
        header('Location: error.php');  
        die();  
        }  
if ((!isset ($_GET['page'])) OR ((int)$_GET['page'] == NULL)) {  
header('Location: topic.php?id='.$topic_id.'&page=1');  
die();  
}  
else {  
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;  
$header = $header.'&page='.$page;  
}  
    if(isset($_GET['order']) AND $_GET['order'] != NULL)  {  
    $order = $_GET['order'];  
    $header = $header.'&order='.$order;  
     }  

等等。

所以我现在得到了用户选择的选项,但如果我想更改其中一个用于特殊重定向,我需要编写一个string-replace - 函数。我的问题是:有没有更好的方法来处理许多$_GET选项和/或缺少选项?有什么好的做法吗?对不起,如果这是一个简单的问题。

1 个答案:

答案 0 :(得分:1)

使用您的示例代码,您通过$_GET访问的每个查询字符串参数,例如$_GET['page'],您可以直接附加到$header变量,例如$header = '&page=' . $_GET['page'];

由于每个密钥看起来都有“规则”,例如id不能为0,因此循环访问密钥列表是可行的,但是,您也可以需要保留一个要评估的规则列表,这是不值得的(除非你有一个很多的密钥,然后我就能看到一个很好的好处)。

相反,您可以重新排序和清理代码以构建完整的查询字符串,然后担心实际重定向:

if (!isset($_GET['id']) || ((int)$_GET['id'] == 0)) {
    // invalid id; no need to process further.
    header('Location: error.php');
    die();
}

// set the ID
$queryString = 'id=' . (int)$_GET['id'];

// process the current page
$queryString .= '&page=' . ((empty($_GET['page']) || ((int)$_GET['page'] <= 0)) ? 1 : $_GET['page']);

// set the order, if available
if (!empty($_GET['order'])) {
    $queryString .= '&order=' . $_GET['order'];
}

// redirect
header('Location: topic.php?' . $queryString);
die();

虽然这仍然需要每个键的手动工作,但它更容易管理(在我看来),实际的URL /重定向只在一个地方处理。这也是我将用于较小项目的方法,这些方法预计不会发生变化(很多)或者具有动态/不断增长的部件列表。

如果上述方法过于繁琐(当您有太多查询字符串参数/规则时很容易就会出现这种情况),创建密钥列表及其规则将成为首选方法。以下是此方法的快速抛出(未经测试)示例:

// define the list of "keys" and their "rules"
$keys = array(
    'id'    => array('required' => true, 'min' => 1),
    'page'  => array('required' => false, 'min' => 1, 'default' => 1),
    'order' => array('required' => false)
);

$query = '';
foreach ($keys as $key => $rules) {
    // get the value of the key from the query-string, if set
    $value = (empty($_GET[$key]) ? null : $_GET[$key]);

    // determine if the value is valid or not
    $valid = (empty($value) || (isset($rules['min']) && ((int)$value < $rules['min']))) ? false : true;

    if ($rules['required'] && !$valid) {
        // required key but invalid value =[
        header('Location: error.php');
        die();
    } else if (!$rules['required'] && !$valid) {
        if (!empty($rules['default'])) {
            // the key is not set but has a default value
            $value = $rules['default'];
        } else {
            // the key is not required, is not set, and has no default value; skip it
            continue;
        }
    }

    // append the key/value to the query
    $query .= (($query != '') ? '&' : '') . $key . '=' . $value;
}

header('Location: topic.php?' . $query);
die();

以上是基于密钥/规则的系统的框架。规则非常小,并定义是否需要密钥以及它的最小值是什么(如果设置了min,则假定它是一个整数并通过(int)强制转换它。如果密钥是必需的而未设置,或者不是有效值,则会立即失败并重定向到error.php。如果密钥且值,则设置default值,它将使用该值;否则它将跳过该键。完成所有的值处理后,它会将键/值附加到查询中并继续进行。

我说它是一个“骨架”系统,因为它只定义了两个规则,minrequired。您可以在此基础上添加其他规则,例如max来设置最高值,或in_list来定义有效值列表(例如order,您可以'in_list' => array('asc', 'desc') in_array($value, $rules['in_list']) 1}}然后用{{1}}检查它。它有潜力,但仅限于您的需求!