RossIsDfunkt Posted January 6, 2011 Share Posted January 6, 2011 OK I'm 98% new to PHP. I'm trying to do my own website and my idea will work something like this: index.php ____________________ Header + Nav {PHP INCLUDE} Footer ____________________ The content will reside in another *.php page If you look at www.avlscotland.com/test 'temporary' It loads default.php when you go to the page which is perfect. I have it working in basic form although only the hyperlinks HOME - ABOUT AVL - DEAD LINK are active at this moment. So it works this way with everything in the same directory, but I want it to load the content from a subdirectory /test/pages/content.php This is the PHP code I am using <?php // Check if page has been requested if (!isset($_GET['content'])) { // Page has not been requested, show default page $content = 'default.php'; } else { // Page has been requested, validate page exists, show page $content = $_GET['content'].'.php'; } // End if page has been requested // Check to see if page exists if (file_exists($content)) { // Page exists // Show page include("$content"); } else { // Page doesn't exist echo '<br>'; echo '<br>'; echo '<br>'; echo 'Sorry, the page that you are trying to access does not exist.<br>'; echo 'Please return to <a href="http://www.avlscotland.com/test/index.php">AVL Scotland</a><br>'; echo '<br>'; echo 'Thank you.<br>'; } // End if page exists ?> I'm accessing the pages using links like <a class="menuactive" href="test/index.php?content=avl">Home</a> What I'm struggling to do is make the PHP look for the content files in a sub-directory. Look forward to any help you can offer. Thanks, Ross Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/ Share on other sites More sharing options...
denno020 Posted January 6, 2011 Share Posted January 6, 2011 So you have a form set up that requests a page to view?? Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1155559 Share on other sites More sharing options...
denno020 Posted January 6, 2011 Share Posted January 6, 2011 Sorry my bad with the above post.. Looking at it further you can do this: $content = "/pages/"; if (!isset($_GET['content'])) { // Page has not been requested, show default page $content .= 'default.php'; } else { // Page has been requested, validate page exists, show page $content .= $_GET['content'].'.php'; } I've then apprended the filename to the content variable. So for the default link, $content will be equal to '/pages/default.php' Does that help? Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1155562 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 @denno Thanks for your input, though that didn't seem to help. I have started again with a very basic page to test the script, which is working better now. The default page is loading fine from sub-dir. The problem I'm having now is prefixing my variable with the sub-dir... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>PHP Include Test</title> </head> <body> <table width="700" border="1" align="center"> <tr> <td><div align="center"> <a href="index.php?content=default">Home</a> <a href="index.php?content=one">One</a> <a href="index.php?content=two">Two</a> </div></td> </tr> <tr> <td><div align="center"> <?php // Check if page has been requested if (!isset($_GET['content'])) { // Page has not been requested, show default page $content .= 'pages/default.php'; } else { // Page has been requested, validate page exists, show page $content .= $_GET['$content'].'.php'; } // End if page has been requested // Check to see if page exists if (file_exists($content)) { // Page exists // Show page include("$content"); } else { // Page doesn't exist echo '<br>'; echo '<br>'; echo '<br>'; echo 'Sorry, the page that you are trying to access does not exist.<br>'; echo 'Please return to <a href="http://www.avlscotland.com/phptest/">AVL Scotland</a><br>'; echo '<br>'; echo 'Thank you.<br>'; } // End if page exists ?> </div></td> </tr> <tr> <td><div align="center">FOOTER</div></td> </tr> </table> </body> </html> At this line $content .= $_GET['$content'].'.php'; it loads fine in the same directory. I'm trying to get something like $content .= $_GET['pages/$content'].'.php'; or $content .= ['.pages/']$_GET['$content'].'.php'; So close, but seems so far. Looking forward to some help. Ross Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156054 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Try putting double quotes in here: $content .= $_GET["pages/$content"].'.php'; That might help... Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156056 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 I'd already tried $content .= $_GET['pages/$content'].'.php'; When I saw your "" I got all excited, but still no joy Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156068 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Wait sorry, I've just realized what's happening there. I probably should have tried to comprehend the code before I suggested anything lol. Try this: $content .= "/pages/"; $content .= $_GET['$content']; $content .= ".php"; That should hopefully work a bit better . You can put them all on the same line if you want, but it's easier to put them on the 3 lines, at least for me . Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156070 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 Still a no go. Does it help if you see how it's behaving now? http://www.avlscotland.com/phptest using your amendment. Thanks, Ross Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156076 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Ok can you give me a short brief as to what you want to achieve here? Do you want the index page to link to another php page that is in the sub folder pages? Or is the content pulled from a php page and display in the index page? Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156077 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 The page avlscotland.com/phptest/index.php contains the Header, Navigation, Space for Include, Footer When you load the page, the include is avlscotland.com/phptest/pages/default.php All other content will reside in avlscotland.com/phptest/pages When I click the links in the Navigation of avlscotland.com/phptest/index.php They put a variable in the URL which I'm trying to use the PHP to load the /pages/ with the corresponding name Does that all make sense? Thank you again for your continued help. Ross Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156082 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Alright, so it will depend on where you put the line of code that says include (blah blah).. If you put that line in the php file that is in /phptest then you will need to specify the path to the file which is /pages/page.php. If the include code line is in the default.php file, then you don't say /pages/page.php, as this will look for another folder called pages. You just say page.php (so $_GET["content"] etc etc) I think it's only a path issue that you're having.. I'll continue to try and help . Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156084 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Just to make sure, you have named the files in the pages folder as one.php and two.php correct? Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156085 Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Assuming the other scripts are in the pages/ directory as well . . . } else { // Page has been requested, validate page exists, show page if( ctype_alnum($_GET['content']) ) { // do some basic validation to prevent non-alphanumeric chars (like ../..) $content = "pages/{$_GET['content']}.php"; } } Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156086 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 @denno Yes that is how the files are named. default.php is in there also. There is some good news though... Pikachu2000 has just solved the problem. @Pikachu2000 - Thank you I always thought it was a syntax thing in "pages/{$_GET['content']}.php"; Over the past 2 days 12-15 developers have tried to tell me it was all wrong and try other ways. Thank you both, a lot. Ross Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156090 Share on other sites More sharing options...
RossIsDfunkt Posted January 7, 2011 Author Share Posted January 7, 2011 Just a quick note though. When I include if( ctype_alnum($_GET['content']) ) { // do some basic validation to prevent non-alphanumeric chars (like ../..) It throws an error. Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156091 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Pikachu2000 can you please explain why this didn't work then? $content .= "/pages/"; $content .= $_GET['$content']; $content .= ".php"; Is it just because of the lack of { } curly brackets? Denno Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156092 Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 You're welcome. It could use some further refinement, but at the moment, I'm going to sleep . . . Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156093 Share on other sites More sharing options...
denno020 Posted January 7, 2011 Share Posted January 7, 2011 Oh wait, maybe it's because I never took the $ sign out. Damn it lol. Quote Link to comment https://forums.phpfreaks.com/topic/223546-includes-changed-by-_variable/#findComment-1156094 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.