1

在我的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 />
4

3 回答 3

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');
    }
}

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

于 2012-03-12T14:05:08.230 回答
1

我不认为这是正确的:

$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
于 2012-03-12T14:08:50.360 回答
0

这里有一个错误:

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

正确的:

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

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

于 2012-03-12T14:06:48.643 回答