evanscnce Posted August 26, 2007 Share Posted August 26, 2007 I am trying to make a website that lets me click a menu option and it just loads an html file into a window. I have one right now, but there is a problem. When the page loads, the initial html file loads (main.html) But when I go to click a link, it keeps main.html instead of going where asked. However, it does show the correct address in the address bar. here is what I have so far <?php if (empty($page)) { $page = "main.html"; } if (file_exists("$page")) { include("$page"); } else { echo "<b>404</b> File not found."; } ?> Then the links are like this index.php?page=main.html Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/ Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 You have to set $page such as $page = $_GET['page']; However this method could leave to people acessing files you don't want them to simply by saying ?page=admin.php for example, instead use a switch Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334752 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 i believe " panget yan" thats a wrong practice it better to use something like this switch($_GET['pagez']){ case 'delete': include 'delete.php' break; default: any page here or put the index page break; } in this case you dont have to test it if that page exist Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334753 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 so your url should look something like this page.php?page=delete Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334754 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 I used cooldude832's method as I don't have anything valuble on that site Now everything works fine. teng84 I am a noob to php so I don't understand what you are saying. to be honest Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334757 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 I said the same thing teng said its best to use a switch because even if u don't think it could be any file... any file you could have someone say your container file as the file and then create this infinite loop deal that would crash your server. i.e www.mydomain.com/index.php?page=index.php?page=index.php?page=index.php?index.php?page=index.php?index.php?page=index.php get the point Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334761 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 oh I see. I just don't quite understand where to inject his blurb of code into mine. Or what I would need to replace for that matter. Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334762 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 like what dude was saying people will know your page so they can run directly on it and besides its ugly when you see something like page.php?page=page.html this is better page.php?page = edit this is clear switch($_GET['pagez']){ case 'delete': include 'delete.php' break; default: any page here or put the index page break; } but maybe the prob is that your not sure using switch ?? Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334763 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 Yea, I don't understand the switch thing. So I would put this correct?: <?php switch($_GET['pagez']){ case 'delete': include 'delete.html' break; default: main.html break; } Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334764 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 that or what you could try is naming acceptable pages i.e <?php $pages = array("edit.html","delete.html","home.php"); if(in_array($_GET['page'],$pages)){ include($_GET['page']); } else{ include("default.html"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334766 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 or to be much simple do this if ($_GET['page'] == edit){ $page= 'edit.php'; } elseif ($_GET['page'] == delete){ $page= 'delete.php'; } else{ $page = 'wrong.php'; } include $page ; Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334769 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 if u have only a few pages, else if you have like 20 pages your switch/else if will be long. The idea here is classic needle in haystack logic which is the in_array() function by definition. Switch by design is for a filtration in which a logical choice needs to be made based on some sort of input such as a percentage base drawing. Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334773 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 I got teng's method to work now Just one thing. You said its ugly to have index.php?page=main.html How do I make it prettier? (lol) Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334777 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 dont put the name of the page instead use the process pertaining that page in my sample i use page=delete which mean you will include page for delete Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334781 Share on other sites More sharing options...
tibberous Posted August 26, 2007 Share Posted August 26, 2007 You can use mod rewrite in Apache - I wouldn't worry about it. Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334782 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 I'll give you a bit of an explanation of GET vs POST and you will begin to understand why its ugly. Get is by design a way of processing variables from Page A to Page B while keeping the variable visitable to the user via the url. This method has some advantages because the user can then bookmark this page and view it later, or actually edit the url manually. Secondly it allows the page to be indexed by search engines so that it can be viewed as a unique page. This is very important because when you are programing you don't want to make a million pages when each on is just different content pulled off a database, instead you create a viewer page and then say view.php?id=1 for example. It acts like a unique page, but you only store the database entry/single view.php page on your server. The disadvantage to GET is it shows the data so any type of personal data processing will be visable to anyone who uses that computer and could be stolen. Now to post Post uses a method of passign data hidden from user (not encrypted technically) This methods best advantage is that you can hide data like Personal information and password so that they CAN'T be indexed. Also Post hides the data in a sorta package to be opened on page 2 for you to handle via an array called $_POST. This is also useful if you haev a lot of data so that the url isn't a big messy string that is porabbly only used once. The disadvantage to psot is also clear that data can't be indexed for bookmarking/indexing. Overall conclusion: Use Post always unless you see a clear and defined reason for GET. In this problem we have a clear use for GET to index all your pages. Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334784 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 Awsome. Thanks guys Got it up and working now. *bookmarks this site* Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334786 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 One more question. How do I make that code show an error if a page doesn't exist and also start at main? Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334805 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 i believe i have put this in my sample\ else { $page='wrong.php'; } its the default that if your condition is not satisfied then they fall in your wrong page Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334807 Share on other sites More sharing options...
evanscnce Posted August 26, 2007 Author Share Posted August 26, 2007 OK, yea that works. But how do I make the script start at a page right off the bat? Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334809 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 OK, yea that works. But how do I make the script start at a page right off the bat? what? Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334811 Share on other sites More sharing options...
cooldude832 Posted August 26, 2007 Share Posted August 26, 2007 If it can't find the one it wiil load what ever is default so if it isn't defined it will load that also Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334812 Share on other sites More sharing options...
evanscnce Posted August 27, 2007 Author Share Posted August 27, 2007 How do I make one a default? Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-334997 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 if youre using the if else then else in the last part all get value that dont match your condition will fall in your default Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-335002 Share on other sites More sharing options...
evanscnce Posted August 27, 2007 Author Share Posted August 27, 2007 I use this <?php $pages = array("edit.html","delete.html","home.php"); if(in_array($_GET['page'],$pages)){ include($_GET['page']); } else{ include("default.html"); } ?> But I want a default, and an error page. Quote Link to comment https://forums.phpfreaks.com/topic/66802-need-help-with-come-code/#findComment-335005 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.