php include file not found error - php

I have a question. I have this script
<?php
$GetPage= "index";
if((isset($_GET["page"])==true) && ($_GET["page"] != "")){
$GetPage = $_GET["page"];
}
?>
But I search on stackoverflow and google. But I can't find it. I want to include a error page when php can't find the file. How can I do that? I'm jut a starter with php.
Ow almost forgoten. I use this to include a part of my site:
<?php include ("include/$GetPage.php"); ?>
Thanks for reading !

<?php
//file_exists will eliminate the need for any of your other checks.
if(file_exists($_GET["page"])){
//Set the page to be loaded if it is found on the server
$GetPage = $_GET["page"];
}else{
//Show the user a 404 error message
header("HTTP/1.0 404 Not Found");
//OR
//Set the page to be loaded as your custom error page
$GetPage = "my_error_page.php";
}
//Include the page
include $GetPage;
?>
Are you looking for a 404 redirect? Or just load a custom error page into the document? Select the above based on what you wish to do.

$GetPage = $_GET["page"]; // validate string!!
if (!file_exists('include/' . $GetPage . '.php')) {
$GetPage = 'errorPage';
}

first check file is in folder (for injection) , ($file is full path of file.)
$path = "include";//your include folder path
if ( substr(realpath($file) , 0,strlen($path)) != $path || is_dir($file))
//error file not found
second check file is exist or not
if (!file_exists($file))
//error

Related

Checking if a page exists in php. GET and include error catching

I am using the following code to include the correct page into the shell page using $_GET from the previous page. index.php?page= . I have fixed the error for it being blank but still get errors when the user changes what comes after the equals. How would i prevent these errors below from coming up and what could i do to prevent blind sql injection. I am using mysqli_real_escape_string but is there anything else apart from prepared statements. I have tried curl but i couldn't get it to work in this situation.
Thanks in advance.
failed to open stream: no such file or directory
failed opening pages for inclusion
if(isset($_GET["page"])){
if ($_GET["page"] == ""){
header("Location:index.php");
}else{
$page = $_GET["page"];
include("pages/$page.php");
}
}else{
include("pages/home.php");
}
I think the code can be cleaned up a bit. For instance like this:
$page = 'home';
if (isset($_GET["page"]) &&
file_exists('pages/'.$_GET["page"].'.php')) $page = $_GET["page"];
include("pages/$page.php");
There's no need to reload to 'index.php' since you're already there.
The file_exists() function checks whether or not a file or directory exists.
This function returns TRUE if the file or directory exists, otherwise it returns FALSE.
if(isset($_GET["page"]))
{
if ($_GET["page"] == "")
{
header("Location:index.php");
}
else if( file_exists('pages/'.$_GET["page"].'.php') )
{
$page = $_GET["page"];
include("pages/$page.php");
}
}
else
{
include("pages/home.php");
}
}

php header location not redirected from 404

Hi friends i have one problem with my redirect page php section.
This is my php redirect section:
<?php
session_start();
if (!(isset($_SESSION['uid']) && $_SESSION['uid'] != '')) {
header('Location: '.$base_url.'index.php');
exit;
}
include_once 'includes.php' ;
if($_GET['user_name']){
$user_name=$_GET['user_name'];
include_once 'public.php';
}
if(empty($_GET['user_name'])) {
header(location:$url404);
}?>
$url404" is in includes.php
$url404=$base_url.'404.php';
The problem is page not redirect if username empty
You forgot to include ", so it's not seeing the header as a string.
Change header(location:$url404); to header("Location:" . $url404);
Use this code:
if(!isset($_GET['user_name']) or $_GET['user_name'] == "") {
header("location:".$url404);
Hopefully this will help you.

How to correctly check if page was included? [duplicate]

This question already has answers here:
PHP: Check if a file is loaded directly instead of including?
(15 answers)
Closed 8 years ago.
I want to make sure that my pages are being included working index page. I would like to know what would be correct way of assuring that my page is being included instead of rendered by itself?
Right now I'm checking if there are at least 2 included files, but I'm not sure I'd it's behavior.
include('config/config.inc.php');
$cms = new cms();
if(($_SERVER['REQUEST_METHOD'] === 'GET' || $_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_GET['page'])) {
include($cms->GetTheme() . "/head.php");
$cms->IncludeModule($_GET['page']); <- actual page being included
include($cms->GetTheme() . "/foot.php");
} // end (GET || POST) && GET
else { // just index.php
include($cms->GetTheme() . "/head.php");
foreach($cms->GetModuleList() as $module) {
echo " $module <br />";
}
include($cms->GetTheme() . "/foot.php");
} // end ELSE
Included page and how I check is it's included
<?php
$module_name = 'Log out user';
$module_directory = 'admin';
$this->SetTitle($module_name); // setting page title
if(count(get_required_files()) < 2) {
header('Location: index.php');
}
else {
if(isset($_SESSION['user'])) {
$this->DestroyUser();
echo "You have been logged out! Please navigate to the Login Page.";
}
else {
header('Location: index.php?page=login');
}
}
?>
I'm not sure if you're talking about this:
include 'test.php';
If yes, then do a simple test like this:
test.php
$testVar = '1';
index.php
include 'test.php';
echo $testVar;
I have no idea what library you're using, so i hope this simple example will allow you to understand.

Creating links to embed page

I have created an index.php that serves as a template with a content box. I also have home.php, about.php, and contact.php which only contain the content to fill that content box. This is the code I use to embed pages into that content box:
<?php
if(!$_GET[page]){
include "home.php"; // Page to goto if nothing picked
} else {
include $_GET[page]."php"; // test.php?page=links would read links.php
}
?>
The home page works fine but I am not sure what code to use in the main menu to link to the other pages. I am having a very hard time getting an answer, so I think I may be searching with the wrong terms, which is why I am asking here.
On the main menu for the website, what code do I use in the links so that they get home.php, about.php, or contact.php?
I made the following test:
$page = "test.php?page=links";
$link = explode("=", $page);
echo $link[1].".php"; //gets links.php
So, your code should looks like:
<?php
if(isset($_GET[page])){
$page = $_GET[page];
$link = explode("=", $page);
include $link[1].".php"; // test.php?page=links would read links.php
} else {
include "home.php"; // Page to goto if nothing picked
}
?>
Saludos.
if(!$_GET[page]){
include "home.php"; // Page to goto if nothing picked
} else {
include $_GET[page].".php"; // test.php?page=links would read links.php
}
It was just missing the '.' before the 'php'.
You should use Quotes for arrays though to avoid a Notice (Undefined constant)
Be careful though, you should verify that $_GET['page'] only contains sites you want to make accessible. Otherwise an attacker could just read any file on your server.
if(array_key_exists('page', $_GET)) {
$page = preg_replace('~[^a-z]~', '', $_GET['page']);
include __DIR__ . '/' . $page . '.php';
} else {
include __DIR__ . '/home.php';
}
Better solution (but you have to manually add all the pages):
$page = (array_key_exists('page', $_GET) ? $_GET['page'] : 'home');
switch($page) {
case 'about':
case 'links':
case 'whatever':
include __DIR__ . '/' . $page . '.php';
break;
default:
include __DIR__ . '/home.php';
break;
}
About
?<key>=<value> in the url.
You look up a value in the $_GET-array by using the key.

Page navigation with php

I am trying to create a dynamic website, the index.php includes the following code in the content area of the website:
<?PHP
// if undefined then define
if(!$od || $od == ""){
$od = "news";
}
// check if exists prep
$link = 'incs'."/".$od.$ext;
flush();
$fp = fopen($link, "r");
// check if inclusion file not exists then return error
if (!$fp) {
echo "An error has ocurred while processing your request. The file linked as ?od=".$od." does not appear to exist.";
}
// if exists then return parse
else {
fclose($fp);
include 'incs'."/".$od.$ext;
}
echo '</body>'."\n";
echo '</html>'."\n";
?>
I also have various links throughout the site to pages like register, login, etc.
Those links point to pages like ?od=register, ?od=login, etc.
The website will pull the default file for me, news, and display that in my content section of my website, but when I click register, the url in the address bar DOES change to /?od=register, but the default news remains in the content section, is there an error in the code above? Or am I just missing something?
P.S. $ext is defined in my config file as inc.php, which is included at the top of the index page.
Besides the fact that this is very insecure - you are making a GET request, access the variables through the $_GET array ie $od = $_GET['od']
I believe you need to define $od with a $_GET['od'] or $_REQUEST['od']
$od = $_GET['od'];
// if undefined then define
if(!$od || $od == ""){
$od = "news";

Categories