Fila54 Posted February 23, 2013 Share Posted February 23, 2013 Hi everyone ! I'm new here... So I got a little problem with creating my first website. I want to include files with the content of my subpages to the main index file. At the first I made a simple solution with switch. Where the urls to the files in the menu look like: href="index.php?page=1" than "page=2" and so on and the code in the place of subpages content was: <? switch ($_GET['page']) { case 2: include("/subpages/aboutme.php"); break; case 3: include("/subpages/a=gallery.php"); break; case 4: include("/subpages/movies.php"); break; case 5: include("/subpages/contact.php"); break; case 1: default: include("/subpages/main.php"); } ?> Of course this solution was correct and it was working BUT what about when someone change the url to the ..."index.php?page=10000000" just because being curious and to check what will happen (there isn't any site with the sign "The page you're looking for cannot be found") and the other reason why I want to upgrade my include solution was that I have to add another cases every time when I want to make another subpages. So I decided to change it and my new url code in menu looks like: href="index.php?page=aboutme" or href="index.php?page=gallery" (so after equal sign there is an exactly name of my subpage without ".php") and than the code which includes my content is: if (file_exists('subpages/' . $page . '.php')) { require('subpages/' . $page . '.php'); } else { echo '<div id="main"><span>The page you’re looking for cannot be found.</span></div>'; } where the $page=$_GET['page']; It is working too but only when I click on one of the subpages in my menu but what about default like in the switch solution? I mean the case when I go to my site typing url without selecting any subpage. Now there is just the sign "The page you're looking for cannot be found." My other question is that I want to add a guestbook which is in the other folder in subpages so the url is "subpages/guestbook/gbook.php". What should I change in my code which works only for the subpages folder? I try to do something like this but it doesn't work: if ($page='gbook' && file_exists('subpages/guestbook/' . $page . '.php')) { require('subpages/guestbook/' . $page . '.php'); } elseif ($page!='gbook' && file_exists('subpages/' . $page . '.php')) { require("subpages/' . $page . '.php'); } else { echo '<div id="main"><span>The page you’re looking for cannot be found.</span></div>'; } Sorry for the length of this topic but I want to avoid unnecessary questions and useless solutions and I want apologies for my English but I'm 17 and I'm from Poland so I've been still learning it ! Quote Link to comment Share on other sites More sharing options...
Stuie_b Posted February 23, 2013 Share Posted February 23, 2013 Firstly ensure you make the input for $_GET safe or you'll be opening your site up for attack, "addslashes()" should surfice, $page = addslashes($_GET['page;]) (see addslashes for more info) It is working too but only when I click on one of the subpages in my menu but what about default like in the switch solution? I mean the case when I go to my site typing url without selecting any subpage. Now there is just the sign "The page you're looking for cannot be found." Like with the select loop you need to tell the IF statement what to do if there wasn't a page request supplied, if(!$page){ //or if(!isset($page)) include("/subpages/main.php"); } elseif (file_exists('subpages/' . $page . '.php')) { require('subpages/' . $page . '.php'); }else { echo '<div id="main"><span>The page you’re looking for cannot be found.</span></div>'; } Now the if has a match for page requests that doesn't specify a page and will still maintain a result if the requested page doesn't exist. My other question is that I want to add a guestbook which is in the other folder in subpages so the url is "subpages/guestbook/gbook.php". What should I change in my code which works only for the subpages folder? I try to do something like this but it doesn't work You need to add the guestbook IF statment into your existing one and assuming the gbook.php file exists in the location you have specified it should work as you expect. if(!$page){ //or if(!isser($page)) include("/subpages/main.php"); } elseif (file_exists('subpages/' . $page . '.php')) { require('subpages/' . $page . '.php'); } elseif ($page='gbook' && file_exists('subpages/guestbook/' . $page . '.php')) { require('subpages/guestbook/' . $page . '.php'); }else { echo '<div id="main"><span>The page you’re looking for cannot be found.</span></div>'; } hope it helps Stuie. Quote Link to comment Share on other sites More sharing options...
denno020 Posted February 23, 2013 Share Posted February 23, 2013 Just a quick note, you've got your default case, that will run if nothing else matches, so you could have put your "That page doesn't exist" page there.. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted February 24, 2013 Share Posted February 24, 2013 Firstly ensure you make the input for $_GET safe or you'll be opening your site up for attack, "addslashes()" should surfice, $page = addslashes($_GET['page;]) (see addslashes for more info) I'm afraid you're wrong on this account, addslashes () will not properly protect his script against malicious users. Instead I recommend sanitising the input, to contain only characters that are safe to use in a filename. Something like this: if (isset ($_GET['page'])) { // Ensure that the filename only consists of letters, numbers and underscores. // Also cast it to lower-case, and limit it to a maximum of 20 characters. $page = strtolower (substr (preg_replace ("/[^a-zA-Z0-9_]/", '', $_GET['page'])), 0, 20); } The validation of the filename is done in the check to see if the file exists. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 24, 2013 Share Posted February 24, 2013 Seems to me like the first solution is analogous to the second solution only it requires much less code to implement. Either way you will need to add additional code with each sub-page that you create. If a user types in an id of say 1000000 then it will trigger the default value of the switch statement which should be a generic page like the home page/main page etc. Quote Link to comment Share on other sites More sharing options...
Fila54 Posted February 24, 2013 Author Share Posted February 24, 2013 (edited) Thanks for replies ! now everything is right but when I try to go to my guestbook page there is an error: Warning: require(subpages/guestbook/1.php) [function.require]: failed to open stream: No such file or directory in /index.php on line 75 My code looks like that: <?php if(!$page) { require("/subpages/main.php"); } elseif (file_exists('subpages/' . $page . '.php')) { require('subpages/' . $page . '.php'); } elseif ($page='gbook' && file_exists('subpages/guestbook/' . $page . '.php')) { require('subpages/guestbook/' . $page . '.php'); }else { echo '<div id="main"><span>The page you’re looking for cannot be found.</span></div>'; } ?> and the link to the guestbook in menu: <td><a class="link" href="index.php?page=gbook">Guestbook</a></td> When I tape /subpages/guestbook/gbook.php after my site address everything is fine and there is a guestbook site. Why the warning refers to the "1.php" ? Edited February 24, 2013 by Fila54 Quote Link to comment Share on other sites More sharing options...
DavidAM Posted February 24, 2013 Share Posted February 24, 2013 elseif ($page='gbook' && file_exists('subpages/guestbook/' . $page . '.php')) { "=" is the assignment operator. To compare use "==" (two equal-signs). As it is, you are assigning: 'gbook' && file_exists() to $page, so you changed $page to "true" which evaluates to "1" when you print it. Quote Link to comment 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.