Jump to content

include: failed to open stream: No such file or directory (aka "please help")


Recommended Posts

Hi :) basicly i have my index.php and from there i use this code to get the content etc.. you will understand more then me i just know what it dose haha..

<?php

    require_once('page_include/page_config.php');

if ($detect->isMobile()) {
    header('Location: mobile.php');
    exit(0);
}


    if (!empty($_GET['page'])) {
        $action = $_GET['page'];  
        $action = basename($action);
        
        include('pages/$action.php');

    } else {

    include('pages/home.php');

    }


        include('page_include/page_footer.php');

?>
so this works fine for me but if some one tries to call a "non existing" .php file from the pages folder i will get an error

 

here is a link for example https://zonepimps.co.uk/?page=stats .. my stats page is no more but google bots still call it witch is how i found this bug.. here is the error

 

[26-Oct-2016 22:27:55 UTC] PHP Warning:  include(pages/stats.php): failed to open stream: No such file or directory in /home/#######/public_html/index.php on line 15

[26-Oct-2016 22:27:55 UTC] PHP Warning:  include(pages/stats.php): failed to open stream: No such file or directory in /home/#######/public_html/index.php on line 15

[26-Oct-2016 22:27:55 UTC] PHP Warning:  include(): Failed opening 'pages/stats.php' for inclusion (include_path='.:/opt/alt/php56/usr/share/pear:/opt/alt/php56/usr/share/php') in /home/#######/public_html/index.php on line 15

 

if someone could point me in the right direction or help me with this i would be greatfull, i used this code years ago and i added elseif and if a link was called and the file was not in the directory it would display an error page but i lost all that code! iv been spending hours trying to recover hdd partitions and found nothing.. so here i am :)

 

JIMBO.

Edited by requinix

thankyou for fast reply, how can i get that code to deny ALL that is not in my "pages/" folder ??

 

i dont want a array of blacklisted names becouse there are still thousands of other words that could still create this error im getting.. i hope you understand as im not too good at explaining

 

Jimbo

You want a whitelist, not a blacklist. That is, you explicitly list all permitted pages. Anything else should result in a 404 error code:

<?php

const PAGES_BASEDIR = __DIR__.'/pages/';

const PAGES = [
    'home',
    'about',
];



if (isset($_GET['page']) && in_array($_GET['page'], PAGES, true) && is_readable(PAGES_BASEDIR.$_GET['page'].'.php'))
{
    require PAGES_BASEDIR.$_GET['page'].'.php';
}
else
{
    http_response_code(404);
    // display error page
    echo 'Page does not exist';
}

The only thing with line 3 is it is hard coded for linux. You would want to use  DIRECTORY_SEPARATOR to set the proper slashes for Windows or Linux.

const PAGES_BASEDIR = __DIR__. DIRECTORY_SEPARATOR .'pages' . DIRECTORY_SEPARATOR; 

awesome man, thats just what i need, just one thing though i need a default page with it being index.php the default page would be "home" .. but not called as the link just "pages/home.php"

 

thankyou for your help so far!

 

Jimbo

is it possible to add this? so i can have a default then the called link then if link called is wrong or file not there then error page...

 

i would like to update my index with this code if this is possible

 

Jimbo.

hi i have updated my code from what you have posted here and my errors have stopt now and if there is a wrong link being called it just ignores it and gose home.. but i still would like it to go to an error page if the link called is not valid to the contents of my pages/ dir 

 

here is my updated code, not changed alot just cleaned it i think...

    if (!isset($_GET['page'])) {
        $action = $_GET['page'];  
        $action = basename($action);
        
        require('pages/$action.php');

    } else {

        require('pages/home.php');

}

Jimbo.

i imagine something like this...

    if (!isset($_GET['page'])) {
        $action = $_GET['page'];  
        $action = basename($action);
        
        require('pages/$action.php');

    } else {

        require('pages/error.php');

    } else {

        require('pages/home.php');

}

but i dont know how to make it work :(

Edited by jimboppin

hi iv done it finaly worked it out.. for anyone wanting to have a links system within your index page and still have your index as index this works fine for me

 

and it shows error page if any wrong links are called.

const PAGES_BASEDIR = __DIR__.'/pages/';
const PAGES = [
    'home',
    ];

if (isset($_GET['page']) && in_array($_GET['page'], PAGES, true) && is_readable(PAGES_BASEDIR.$_GET['page'].'.php')) {
    require PAGES_BASEDIR.$_GET['page'].'.php';    
} else {
if (isset($_GET['page'])) {
    http_response_code(404);
    // display error page
    require('pages/error.php');
} else {
    require('pages/home.php');
    }
}

 Thankyou for your time.

 

Jimbo.

You should rephrase the code for clarity:

whitelist = ...

if no page parameter:
  show home page
else if page is in whitelist and page exists ...:
  show page
else:
  show error
Edited by Jacques1
    // Directory Of Pages
const PAGES_BASEDIR = __DIR__.'/pages/';
const PAGES = [
    // Array Of Active Pages
    'home',
    ];

if (isset($_GET['page']) && in_array($_GET['page'], PAGES, true) && is_readable(PAGES_BASEDIR.$_GET['page'].'.php')) {
    // Displays The Called Link i.e index.php/?page=home
    require PAGES_BASEDIR.$_GET['page'].'.php';    
} else {
    // Check To Display Error Or Home
if (isset($_GET['page'])) {
    http_response_code(404);
    // Display Error404 Page
    require('pages/Error404.php');
} else {
    // Default Index Page i.e "home"
    require('pages/home.php');
    }
}

do you mean like this... im not a coder just learn fast i did alot in my teens but forgot alot of it.

 

JImbo.

here is my site i feel its working well so far..

 

index https://www.zonepimps.co.uk

wrong link https://www.zonepimps.co.uk/?page=wronglink

then just my homepage but being called thru the links http://www.zonepimps.co.uk/?page=home

 

i think iv managed realy well with a little help from yourself :)

 

Jimbo.

Isn't that the exact same code you had before?

 

I'm talking about first checking if the page parameter isn't set

if (!isset($_GET['page']))
{
    // default page
}
else if (in_array(...) ...)
{
    // selected page
}
else
{
    // error
}

ah i see what you mean now.. as i said im not a coder this is just a hobby for me i learn alot from it.

 

Would you be able to apply that to my code please so i can see how it is suposed to be?

 

Would be appreciated :)

Jimbo.

:idea: well i did it! thankyou again!

 

if you could just check it for me

    // Directory Of Pages
const PAGES_BASEDIR = __DIR__.'/pages/';
const PAGES = [
    // Array Of Active Pages
    'home',
    ];

if (!isset($_GET['page']))
{
    // Default Index Page i.e "home"
    require('pages/home.php');
}
else if (in_array($_GET['page'], PAGES, true) && is_readable(PAGES_BASEDIR.$_GET['page'].'.php'))
{
    // Displays The Called Link i.e index.php/?page=home
    require PAGES_BASEDIR.$_GET['page'].'.php';    
}
else
{
    http_response_code(404);
    // Display Error404 Page
    require('pages/Error404.php');
}

Jimbo.

The home page and error page path (first and third case) are lacking the PAGES_BASEDIR prefix. You're relying on relative paths instead, which can go wrong.

 

Other than that, it's OK.

Edited by Jacques1

hows this...

<?php

    // Directory Of Page_Include
const INCLUDE_BASEDIR = __DIR__.'/page_include/';
    // Directory Of Pages
const PAGES_BASEDIR = __DIR__.'/pages/';

    require_once INCLUDE_BASEDIR.'page_config.php';

if ($detect->isMobile()) {
header('Location: mobile.php');
exit(0);
}

const PAGES = [
    // Array Of Active Pages
    'home',
    ];

if (!isset($_GET['page']))
{
    // Default Index Page i.e "home"
    require PAGES_BASEDIR.'home.php';
}
else if (in_array($_GET['page'], PAGES, true) && is_readable(PAGES_BASEDIR.$_GET['page'].'.php'))
{
    // Displays The Called Link i.e index.php/?page=home
    require PAGES_BASEDIR.$_GET['page'].'.php';    
}
else
{
    http_response_code(404);
    // Display Error404 Page
    require PAGES_BASEDIR.'Error404.php';
}

    require INCLUDE_BASEDIR.'page_footer.php';

?>

iv been working on this none stop haha its a little addictive

 

Jimbo.

Edited by jimboppin
they all seem to do the same thing

 

The difference is in error handling. Per the manual:

 

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERRORlevel error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

Edited by benanamen
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.