whare Posted June 13, 2010 Share Posted June 13, 2010 Hi all not been playing with php over the past year and still a noob lol i did have some code to include files based on the URL link E.G: http://www.yoursite.com/index.php?page=about then i had some form of string that would access the URL so it would have checked if the url was just index.php or had ?page=somepage after it I can not seem to find any referance on the www or shift the headach i have got trying to remember the code so thought some of you wondaful people would no what i am on about and point me in the rught direction of a tutorial for the information or have the code tohand so i can referance from it thanx all Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/ Share on other sites More sharing options...
premiso Posted June 13, 2010 Share Posted June 13, 2010 Well, what you are doing can be a major security risk if done improperly. Is yoursite.com your actual site that you are trying to do this on? If so the following would probably work: $_GET['page'] = isset($_GET['page'])?$_GET['page']:'index'; // ternary operator acts as a short if / else $validPages = array('about', 'home', 'members', 'index'); // etc etc if (!in_array($_GET['page'], $validPages)) { $_GET['page'] = "index"; } include('index.php'); Should be a secure way of doing it, if you are in-deed working on the site the URL points to. Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071445 Share on other sites More sharing options...
whare Posted June 13, 2010 Author Share Posted June 13, 2010 It is for my own site i want to use the index.php as a main template file and include the other page info into the main body are as requested so on the main page i would have header menu footer and the include for the body thankyou for the reply EDIT: How can i do it without the array ? i would like to just beable to place a file in the folder and URL link it without editing an array first Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071449 Share on other sites More sharing options...
Daniel0 Posted June 13, 2010 Share Posted June 13, 2010 Generate the array based on which files are available. $page = 'pages/' . $_GET['page'] . '.php'; $pages = glob('pages/*.php'); if (!in_array($page, glob('pages/*.php'))) { die('invalid page'); // substitute with proper error handling } include $page; Not tested, but should work. Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071457 Share on other sites More sharing options...
whare Posted June 13, 2010 Author Share Posted June 13, 2010 Thanx for the reply After abit of playing i cam up with this myself after looking at premiso's post <?php if(isset($_GET['page'})) { if(file_exists($_GET['page'].'.php')) { include($_GET['page'].'.php'); } else { include('main.php'); }} ?> now that works sort of if i type in index.php?page=main it will show as it should if i type in index.php?page=nofile again it all works However if i type in index.php i get no include it will not load the main.php so i must be missing something just can not remember what lol Thanx Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071546 Share on other sites More sharing options...
ignace Posted June 13, 2010 Share Posted June 13, 2010 <?php if(isset($_GET['page'})) { if(file_exists($_GET['page'].'.php')) { include($_GET['page'].'.php'); } else { include('main.php'); }} ?> Bad idea, just assume I crafted the URL: index.php?page=../your/secret/config Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071561 Share on other sites More sharing options...
whare Posted June 13, 2010 Author Share Posted June 13, 2010 <?php if(isset($_GET['page'})) { if(file_exists($_GET['page'].'.php')) { include($_GET['page'].'.php'); } else { include('main.php'); }} ?> Bad idea, just assume I crafted the URL: index.php?page=../your/secret/config you would get a 404 error hehe but no i see what you are saying so have you an better code example that i would work with that has ease of use but still the function i require? Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071570 Share on other sites More sharing options...
Daniel0 Posted June 13, 2010 Share Posted June 13, 2010 <?php if(isset($_GET['page'})) { if(file_exists($_GET['page'].'.php')) { include($_GET['page'].'.php'); } else { include('main.php'); }} ?> Bad idea, just assume I crafted the URL: index.php?page=../your/secret/config you would get a 404 error hehe but no i see what you are saying so have you an better code example that i would work with that has ease of use but still the function i require? Here you go: http://www.phpfreaks.com/forums/index.php/topic,300990.msg1424520.html#msg1424520 Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071590 Share on other sites More sharing options...
whare Posted June 13, 2010 Author Share Posted June 13, 2010 Generate the array based on which files are available. $page = 'pages/' . $_GET['page'] . '.php'; $pages = glob('pages/*.php'); if (!in_array($page, glob('pages/*.php'))) { die('invalid page'); // substitute with proper error handling } include $page; Not tested, but should work. Thanx for the reply however i did look over your code and see if (!in_array($page, glob('pages/*.php'))) { used for stopping code from running and see it as a problem however i will try it but sill see it as a pitfall EDIT: Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/a6950394/public_html/index.php on line 150 used the code and that is the error going to have a bit of a play now Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071597 Share on other sites More sharing options...
ignace Posted June 13, 2010 Share Posted June 13, 2010 Why not use Daniel0's solution? Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071600 Share on other sites More sharing options...
whare Posted June 13, 2010 Author Share Posted June 13, 2010 god i forgot how many painkillers was needed for php get the error in my abouve post Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/a6950394/public_html/index.php on line 150 and to be honist being a noob still lol i dont really inderstand the glob command so unsure how best to troble shoot this one Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071602 Share on other sites More sharing options...
Daniel0 Posted June 13, 2010 Share Posted June 13, 2010 glob returns an array of files that matches a pattern. In my example, it would match all files in the pages folder ending on .php. Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1071608 Share on other sites More sharing options...
whare Posted May 12, 2011 Author Share Posted May 12, 2011 Drag up this old topic of mine its is related right still want the same sort of format but looking to use it to pull data from the DB so new pages can be created/edit from site admin area something like what a forum would do but only edited and created by site staff to eliminate the need for FTP access to the server for editing thanx Quote Link to comment https://forums.phpfreaks.com/topic/204645-include-page-based-on-url/#findComment-1214557 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.