【问题标题】:PHP Header location with array key and allowed_operations带有数组键和 allowed_operations 的 PHP 标头位置
【发布时间】:2012-03-12 13:58:39
【问题描述】:

在我的 login.php 页面中,我有这个:

$allowed_operations = array('foo', 'lorem');
    if(isset($_GET['p']) 
    && in_array(isset($_GET['p']), $allowed_operations)){
    switch($_GET['p']){
        case 'foo':
              // Code for 'foo' goes here
        break;
        case 'lorem':
             // Code for 'lorem' goes here
        break;
    }
}

如果我调用 url http://example.com/login.php?p=foo,函数 foo 会被调用。

是否可以在我的 html 标记中不添加 href http://example.com?p=foo 的情况下调用此 url?

例如这样的:

<?php

if (array_key_exists("login", $_GET)) {
    $p = $_GET['p'];
    if ($p == 'foo') {
       header("Location: login.php?p=foo");  // This doesn't work
                        // And if I remove the ?p=foo, 
                        // it redirect to the page but
                        // the 'foo' function is not called
        }
    }

    ?>

还有我的html:

<a href="?login&p=foo">Login Foo</a> <br />

【问题讨论】:

  • &lt;a href="?login&amp;p=foo"&gt; 不应该是&lt;a href="login.php?p=foo"&gt; 吗?
  • 我知道这就是我来这里问这个问题的原因。如何在不添加 . 的情况下执行此操作
  • @mishu 不,没关系。 href 会自动将参数附加到当前 uri 中,因此定义两种方式都是正确的。
  • @IbrahimAzharArmar - 如果您已经在 login.php 上,这是真的.. 如果您来自任何其他页面,它只会添加一个名为 login 的空 var.. 但这不是重点问题..我认为..
  • @jQuerybeast 你想使用 $_GET 而不将它添加到 url?另外:in_array(isset($_GET['p']), $allowed_operations)这看起来很破旧

标签: php header case operations


【解决方案1】:

这是因为无限的页面重定向循环。这将由您的代码创建。

$p = $_GET['p'];
    if ($p == 'foo') {
       header("Location: login.php?p=foo");  // This doesn't work
                        // And if I remove the ?p=foo, 
                        // it redirect to the page but
                        // the 'foo' function is not called
        }
    }

每次执行此页面中的代码时,条件都会设置为 true,即$_GET['p'] 将始终保持值 foo,并且它会一次又一次地重定向到同一页面。检测哪个 PHP 将停止执行您的脚本。

即使满足条件,我也无法理解您为什么要再次重定向到同一页面。我的建议是避免它。如果是,只需检查变量是否要重定向到同一页面。如果没有则跳过页面然后重定向到首选目的地。

if (array_key_exists("login", $_GET)) {
    $p = $_GET['p'];
    if ($p == 'foo') {
      //sice the variable foo redirects to the same page skip this path and do nothing
    } else {
        //any other url redirection goes here
        header('Location: index.php?bar');
    }
}

虽然可能有其他方式。上面的代码也应该可以工作,并且会避免进入无限的页面重定向循环。

【讨论】:

【解决方案2】:

我不认为这是正确的:

$allowed_operations = array('foo', 'lorem');
if(isset($_GET['p'])  && in_array(isset($_GET['p']), $allowed_operations)){

应该是

$allowed_operations = array('foo', 'lorem');
if(isset($_GET['p'])  && in_array($_GET['p'], $allowed_operations)){

你应该使用

<a href="login&p=foo">Login Foo</a> <br />

这是一个无限循环

if (array_key_exists("login", $_GET)) {
    $p = $_GET['p'];
    if ($p == 'foo') {
       header("Location: login.php?p=foo");  // This doesn't work

【讨论】:

    【解决方案3】:

    这里有错误:

    <a href="?login&p=foo">Login Foo</a> <br />
    

    正确:

    <a href="login.php?p=foo">Login Foo</a> <br />
    

    而且,循环是无限的。 当您输入 login.php 时,您会一次又一次地要求去... 在第一次之后创建一个“中断”函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多