Jump to content

Help With Index.php?


Kryllster

Recommended Posts

I have some code some of it mine some of it others and someone im sure has already thought of it but I am trying to make an index.php controller page. But what I can figure out is haow to displa a simple "This Page Does Not Exist" error and Im having no luck. The code I will show works except for the above mentioned.

 

==============================================================

<?php
// header
include('pages/main_header.php');
$dir = 'pages';
$files = scandir($dir);
$page = (isset($_GET['page'])) ? 'intro' : $_GET['page'];
if (!in_array($page, $files))
{
 require_once('pages/' . $_GET['page'] . ".php");
}
//footer
include('pages/main_footer.php');
?>

 

Any tips or solutions would be appreciated.

Link to comment
Share on other sites

I tried this but it didnt work and Im not sure about the custom error page

 

========================================================

<?php
// header
include('pages/main_header.php');
$dir = 'pages/';
$files = scandir($dir);
$page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
if (!in_array($page, $files)){
 require_once('pages/' . $_GET['page'] . ".php");
}
else {
 echo "That Page Does Not Exist";
}
//footer
include('pages/main_footer.php');
?>

 

This didnt work!!

Link to comment
Share on other sites

here is the var_dump();

================================

array(37) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(9) "about.php" [3]=> string(11) "airport.php" [4]=> string(13) "amusement.php" [5]=> string(11) "brawler.php" [6]=> string( "chat.php" [7]=> string(10) "combat.php" [8]=> string(12) "congrats.php" [9]=> string(10) "create.php" [10]=> string(10) "defeat.php" [11]=> string(12) "downtown.php" [12]=> string(10) "footer.php" [13]=> string(10) "header.php" [14]=> string(12) "industry.php" [15]=> string(9) "intro.php" [16]=> string(13) "loggedout.php" [17]=> string(12) "magician.php" [18]=> string( "main.php" [19]=> string(15) "main_footer.php" [20]=> string(15) "main_header.php" [21]=> string(12) "maindrag.php" [22]=> string( "news.php" [23]=> string(7) "old.php" [24]=> string(15) "playerstats.php" [25]=> string(12) "recovery.php" [26]=> string(13) "safeplace.php" [27]=> string(10) "sewers.php" [28]=> string(9) "slums.php" [29]=> string(9) "stats.php" [30]=> string(11) "suburbs.php" [31]=> string(10) "tanker.php" [32]=> string(16) "technologist.php" [33]=> string(4) "test" [34]=> string(11) "victory.php" [35]=> string(16) "vilinterface.php" [36]=> string(13) "weaponeer.php" }

 

======================================

I dont know what exactly all that means but its reading the files in the directory.

Link to comment
Share on other sites

Hello again!

 

I have tested your code on my local server and I got several errors, with these errors the problem was quite easy to find.

If you take a look at the code you seem to have made a little mistake ;P

 

$page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
if (!in_array($page, $files)){
require_once('pages/' . $_GET['page'] . ".php");
}

 

The problem is on the 3rd line above, on the first line you set the variable $page to intro if $_GET['page'] is not set else you just use the $_GET variable.

However on the 3rd line you are using the $_GET variable instead of the $page variable.

This should work for you:

 

$page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
if (!in_array($page, $files)){
require_once('pages/' . [color=#ff0000]$page[/color] . ".php");
}

Edited by Mad programmer
Link to comment
Share on other sites

Ok well it looks like I jusmped the gun I only tested default.php?page=test when I had a folder in the main directory called test. After I delelted that folder the same error messages pop up.

 

===========================================================

Warning: require_once(pages/test.php): failed to open stream: No such file or directory in C:\xampp\htdocs\vang\default.php on line 8

 

Fatal error: require_once(): Failed opening required 'pages/test.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\vang\default.php on line 8

=============================================================

back to the drawing board :(

Link to comment
Share on other sites

<?php
// header
include('pages/main_header.php');
$dir = 'pages/';
$files = scandir($dir);
$page = (!isset($_GET['page'])) ? 'intro' : $_GET['page'];
if (!in_array($_GET['page'], $files)){
require_once('pages/' . $page . ".php"); 
}
else {
echo "This page does not exist!!";
}
//footer
include('pages/main_footer.php');
?>

Link to comment
Share on other sites

your logic looks to be back to front, and you should probably make sure that all the URL variables are in the right case as in_array() is case sensitive. Try this:

<?php
// header
include('pages/main_header.php');
$dir = 'pages/';
$files = scandir($dir);
$page = (!isset($_GET['page'])) ? 'intro.php' : $_GET['page'];
if (!in_array($page, $files)){
 echo "This page does not exist!!";
}
else {
 require_once "pages/{$page}";
}
//footer
include('pages/main_footer.php');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.