I currently use the following script that checks i开发者_开发问答f a page exists in my array and then opens it, else it opens my error.php
<?php
$page = $_GET['page'];
$pages = array('p_1',
'p_2',
'p_3',
'error');
if (!empty($page)) {
if(in_array($page, $pages)) {
$page .= '.php';
require_once('../' . $page . '');
}
}
else {
require_once('../error.php');
}
?>
Accidentally (until now I didn't test it) I added a newpage.php in my site and forgot to add it in my array.
But my script instead of displaying my error.php it opened the newpage.php.
Where did I go wrong?
Thank you
Sorry to say it, but it is not possible. Not in this snippet of code. Search for requiring/including somewhere else
And, error.php shows only in case that $_GET['page']
is empty
<?php
$page = $_GET['page'];
$pages = array('p_1',
'p_2',
'p_3',
'error');
if (!empty($page)) {
if(in_array($page, $pages)) {
$page .= '.php';
require_once('../' . $page . '');
}
else require_once('../error.php');
}
else require_once('../error.php');
?>
is correct one
I don't know why it opened newpage.php without seeing the rest of your code but i can tell you why it didn't display the error.php page
Your code only display error.php if $_GET['page'] "is empty" (ie null, 0, ''). To fix it you should try the following code :
<?php
$page = $_GET['page'];
$pages = array('p_1',
'p_2',
'p_3',
'error');
if(!empty($page) && in_array($page, $pages)) {
$page .= '.php';
require_once('../' . $page . '');
} else {
require_once('../error.php');
}
?>
精彩评论