douya2007 2016-09-02 21:49
浏览 196
已采纳

$ _GET白页,如果在URL中未定义

I have an example URL http://www.example.com/index.php?pagina=some-cool-document&page=5

When the PHP file exists and the $_GET superglobal is set, it works. For example:

http://www.example.com/index.php?pagina=some-cool-document&page=5 - Will show page 5

http://www.example.com/index.php?pagina=some-cool-document&page=0 and http://www.example.com/index.php?pagina=some-cool-document&page=1 - will show page 1

if there only ten pages and the value of page is 11 or a higher value it will show page 10 as there only 10 pages to show

The above examples and information about my URL are working.

The issue I face is when page is not an array key or when page is not numeric it will show a page with nothing on it; Some example URLs:

http://www.example.com/index.php?pagina=some-cool-document (page is not defined in URL/not an array key) http://www.example.com/index.php?pagina=some-cool-document&page=hello-world (page has no numeric value)

1: I would like to show the content of page 1 instead of a white page when the page is not an array key.

2: I'd like to show a message such as:

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.';

when the page is not numeric, what is the best way to get rid of the white page and to get this to work?

What I've tried:

$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

I also did something with if is_numeric($_GET['page']){ but the result is all pages are page 1 and NO white pages for nonnumeric value in page so I'm still testing other options with is_numeric()

I also tried some options with array_key_exists, for example (Also not working for me):

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

I'm still reading other StackOverflow questions and Google but I didn't find any working method to solve this issue...

What else can I try to get this to work?

EDIT

NOTE: All variables (in message) are defined in other parts of my code and all of them are defined before I used them.

the value of page should be numeric without any other characters, just [0-9] should be used as a value else only show the message..

The code below is the code I use, it doesn't show page 1 when page value is too high and no content from any page.

The content from page 1 is showing up below the message when the value of page contain other characters, I'd like to get rid of the content and only show the message if error is detected

$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'];
}
  • 写回答

3条回答 默认 最新

  • dou4624 2016-09-28 03:40
    关注

    I wrote the following code and everything is working very well. When $_GET['page'] is undefined it will show alternate content When $_GET['page'] is defined with a valid value it will show page content When $_GET['page'] is defined with an invalid value it will show an error message

    $Last_Page is defined before the code and has a value between 1 and the number of the last page $site_URI = 'http://www.example.com'; is defined before...

    The php code:

    <?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...
                }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 串口控制LED灯驱动板设计
  • ¥15 如何在unity场景里做到通过按钮实现模型替换
  • ¥15 uniapp的websocket和c++的websocket服务器通讯 发送的数据和接受的数据表现形式不一样
  • ¥15 pyspark配置中的错误
  • ¥15 html word表格
  • ¥15 kali安装包如何解决,请给明确方案和步骤
  • ¥15 DEA软件进行企业创新效率的计算
  • ¥15 树莓派驱动42步进电机
  • ¥20 bevfusion mit可视化遇到的问题
  • ¥15 如何获得《人工智能工程技术人员》认证?