Jump to content

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


jimboppin

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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';
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

    // 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.

Link to comment
Share on other sites

: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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
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.