jimboppin Posted October 26, 2016 Share Posted October 26, 2016 (edited) 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 October 31, 2016 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/ Share on other sites More sharing options...
benanamen Posted October 26, 2016 Share Posted October 26, 2016 See the code here https://forums.phpfreaks.com/topic/302370-router-any-issues-comments/ Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538637 Share on other sites More sharing options...
jimboppin Posted October 26, 2016 Author Share Posted October 26, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538638 Share on other sites More sharing options...
Jacques1 Posted October 26, 2016 Share Posted October 26, 2016 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538639 Share on other sites More sharing options...
benanamen Posted October 27, 2016 Share Posted October 27, 2016 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; Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538641 Share on other sites More sharing options...
Jacques1 Posted October 27, 2016 Share Posted October 27, 2016 Windows can handle forward slashes just fine. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538643 Share on other sites More sharing options...
jimboppin Posted October 27, 2016 Author Share Posted October 27, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538663 Share on other sites More sharing options...
jimboppin Posted October 27, 2016 Author Share Posted October 27, 2016 I think this post would be usefull to some one, if you can think of a better title then "please help" then maybe people will find this answer easyer Jimbo. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538665 Share on other sites More sharing options...
jimboppin Posted October 28, 2016 Author Share Posted October 28, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538705 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538716 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 (edited) 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 October 29, 2016 by jimboppin Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538717 Share on other sites More sharing options...
maxxd Posted October 29, 2016 Share Posted October 29, 2016 (edited) You're assigning $_GET['page'] to $action only if $_GET['page'] isn't set. How is any of this working for you? Edited October 29, 2016 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538720 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538723 Share on other sites More sharing options...
Jacques1 Posted October 29, 2016 Share Posted October 29, 2016 (edited) 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 October 29, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538724 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 // 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538727 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538728 Share on other sites More sharing options...
Jacques1 Posted October 29, 2016 Share Posted October 29, 2016 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 } Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538729 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538732 Share on other sites More sharing options...
Jacques1 Posted October 29, 2016 Share Posted October 29, 2016 Would you be able to apply that to my code please so i can see how it is suposed to be? I have full confidence in your ability to do that yourself. This isn't programming, it's search-and-replace. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538733 Share on other sites More sharing options...
jimboppin Posted October 29, 2016 Author Share Posted October 29, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538748 Share on other sites More sharing options...
Jacques1 Posted October 29, 2016 Share Posted October 29, 2016 (edited) 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 October 29, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538757 Share on other sites More sharing options...
jimboppin Posted October 30, 2016 Author Share Posted October 30, 2016 (edited) 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 October 30, 2016 by jimboppin Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538762 Share on other sites More sharing options...
jimboppin Posted October 30, 2016 Author Share Posted October 30, 2016 is it ok to use require alot too? i used to use includes but they all seem to do the same thing... Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538763 Share on other sites More sharing options...
benanamen Posted October 30, 2016 Share Posted October 30, 2016 (edited) 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 October 30, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538764 Share on other sites More sharing options...
jimboppin Posted October 30, 2016 Author Share Posted October 30, 2016 cool, maybe not use include then... Quote Link to comment https://forums.phpfreaks.com/topic/302412-include-failed-to-open-stream-no-such-file-or-directory-aka-please-help/#findComment-1538772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.