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

$ _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...
                }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douwang9650 2016-09-02 22:14

    try this:

    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'];
    }
    
    评论
    解决 无用
    打赏 举报
  • dqiatvbi61502 2016-09-02 22:25

    Firstly, you shouldn't make it habit to modify your $_GET and $_POST superglobals; Reference here. But, to answer your question, I simply ran this code and it works:

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

    If you want to have a message sent, use a normal if statement instead of a ternary statement.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题