jagb jagb - 1 year ago 92
PHP Question

$_GET white page if undefined in 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

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

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?


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.';
$page = $_GET['page'];

Answer Source

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:

// 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...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download