【问题标题】:$_GET white page if undefined in URL如果在 URL 中未定义 $_GET 白页
【发布时间】:2016-09-02 21:49:10
【问题描述】:

我有一个示例 URL http://www.example.com/index.php?pagina=some-cool-document&page=5

当 PHP 文件存在并且设置了 $_GET 超全局时,它可以工作。例如:

http://www.example.com/index.php?pagina=some-cool-document&page=5 - 将显示第 5 页

http://www.example.com/index.php?pagina=some-cool-document&page=0 和 http://www.example.com/index.php?pagina=some-cool-document&page=1 - 将显示第 1 页

如果只有 10 个页面并且页面的值为 11 或更高的值,它将显示第 10 页,因为只有 10 个页面可以显示

上述示例和有关我的 URL 的信息正在运行。

我面临的问题是当 page 不是数组键或 page 不是数字时,它会显示一个没有任何内容的页面;一些示例网址:

http://www.example.com/index.php?pagina=some-cool-document(页面未在 URL 中定义/不是数组键)http://www.example.com/index.php?pagina=some-cool-document&page=hello-world(页面没有数值)

1:当页面不是数组键时,我想显示第1页的内容而不是白页。

2:我想显示一条消息,例如:

echo 'Sorry, the page ' . $_GET['page'] . 'is not available, please click <a href="http://www.example.com/index.php?pagina=some-cool-document&page=1">here</a> to visit the first page.';

当页面不是数字时,摆脱白页并使其正常工作的最佳方法是什么?

我尝试过的:

$page = isset($_GET['page']) && ($page = intval($_GET['page'])) > 0 ? $page : 1; // I use this option as it does some of the things I want...

$page = (isset($_GET['page']) && trim($_GET['page']) == '1') ? trim($_GET['page']) : '1'; // found here on stackoverflow, 100% is not working for me :-(

$page = isset($_GET['page']) && ($page = !empty($_GET['page']) && is_null($_GET[page])) ? $_GET['page'] : 1; // This one also does some of the things I want to do but is not working that well

我也对 if is_numeric($_GET['page']){ 做了一些事情,但结果是所有页面都是第 1 页,并且页面中没有非数字值的白页,所以我仍在使用 is_numeric() 测试其他选项

我还尝试了一些使用 array_key_exists 的选项,例如(也不适用于我):

if (!array_key_exists('page', $_GET)) {
    $page = $_GET['page'];
} else {
    $_GET['page'] = '';
}

我仍在阅读其他 StackOverflow 问题和 Google,但我没有找到解决此问题的任何工作方法...

我还能尝试什么来让它工作?

编辑

注意:所有变量(在消息中)都在我的代码的其他部分中定义,并且所有变量都是在我使用它们之前定义的。

page 的值应该是数字,没有任何其他字符,只应该使用 [0-9] 作为值,否则只显示消息..

下面的代码是我使用的代码,当页面价值太高并且没有任何页面的内容时,它不会显示第 1 页。

当页面的值包含其他字符时,第 1 页的内容显示在消息下方,我想删除该内容,仅在检测到错误时显示消息

$Last_Page = ceil($totalNodes / $perPage);


$page = isset($_GET['page']) && ($page = intval($_GET['page'])) > 0 ? $page : 1;

if(!is_int($_GET['page']) || $_GET['page'] < 0 || $_GET['page'] > $Last_Page){
    echo 'Sorry, the page ' . $_GET['page'] . ' is not available, please click <a href="' . $site_URI . '/index.php?pagina=' . $_GET['pagina'] . '&page=1">here</a> to visit the first page.';
    }else{
        $page = $_GET['page'];
}

【问题讨论】:

  • 如果您只得到一个白页,请尝试启用错误报告。错误报告(E_ALL); ini_set('display_errors',1);
  • @markt 这个服务器不允许这样做,我编辑了 php.ini 并修复了 1 个错误,一个错字.. 白页已经消失,当$_GET['page'] 未定义时显示第 1 页

标签: php superglobals


【解决方案1】:

首先,您不应该养成修改$_GET 和$_POST 超全局变量的习惯;参考here。但是,为了回答你的问题,我简单地运行了这段代码,它就可以工作了:

$page = isset($_GET['page']) && (intval($_GET['page'])) > 0 && $_GET['page'] <= 1 ? $_GET['page'] : 1; 

如果要发送消息,请使用普通 if 语句而不是三元语句。

【讨论】:

    【解决方案2】:

    试试这个:

    if(!is_int($_GET['page']) || $_GET['page'] < 0 || $_GET['page'] > maxpagesAvailable)
    {
        echo 'Sorry, the page ' . $_GET['page'] . 'is not available, please click <a href="http://www.example.com/index.php?pagina=some-cool-document&page=1">here</a> to visit the first page.';
    }
    else{
        $page = $_GET['page'];
    }
    

    【讨论】:

    • 不应该是 !isset 吗?
    • 是的,我误解了@RGriffiths 的问题,我修正了答案
    • @Jeanclaude 如果页面是一个太高的数字,则显示回显消息,但如果页面包含其他字符,它将显示消息,在消息下方它将显示第 1 页,我不希望文本消息下方的第 1 页内容我将继续尝试删除该内容,并使用相关代码发布对我的问题的编辑
    【解决方案3】:

    我编写了以下代码,一切运行良好。 当$_GET['page'] 未定义时,它将显示替代内容 当$_GET['page'] 定义为有效值时,它将显示页面内容 当$_GET['page'] 定义为无效值时,将显示错误消息

    $Last_Page 在代码之前定义,其值介于 1 和最后一页数之间 $site_URI = 'http://www.example.com'; 之前定义...

    php代码:

    <?php
    // Get rid of undefined index 
    class Page_Pagination {
        function get($page) {
            return isset($_GET['page']) ? $_GET['page'] : null;
            }
    }
    $GET_page = new Page_Pagination;
    $page = $GET_page->get('page'); // look in $_GET['page']
    // Create array with all available page numbers!
    for ($i = 1; $i <= $Last_Page; ++$i) {
        $Available_Pages[] = $i;
        }
        if (in_array($page, $Available_Pages)) { // is the GET value in the array?
            // Do something with $page as the value is valid! $page is defined before this line and has a valid value so it can be used here ;)
            }elseif($page == null){
            // Do something if page variable is NULL 
            echo 'something here to enjoy the visitors...';
            }else{ // value of page variable is NOT a valid value and is not NULL
                echo 'Sorry, the page ' . $_GET['page'] . ' is not available, please click <a href="' . $site_URI . '/index.php?pagina=' . $_GET['pagina'] . '&page=1">here</a> to visit the first page.'; // Include a file or echo something or do something else on here...
                }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      相关资源
      最近更新 更多