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 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入