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'];
}