Topic: Code works for passing variables in URL, but maybe you have suggestions? (Read 972 times)
My Name is Enigo Montoya
Posts: 33
188 credits Members referred : 0
« on: Dec 02, 2006, 08:36:14 PM »
Hi,
Thank you for the nice scripts. Atleast I am now registerd so I can ask a question in the future if I need to. I worked a little bit more with the following code I had, and believe I found a solution. Someone may know a slightly different way to do it a little better. I wanted something to be able to pass the variable in the URL like snippets.php?id=12 The example below would work for text updates, for instance where you have the snippets listed each id is text with links. I wanted to see if there was a way to be able to include a file because changing the content in the below code could get messy with tables, graphics...etc. in between the php tags I just did <?php include("mypage.php"); ?> so I can keep a possibly complicated structure in a seperate file wihout having to search through the following code to find out what page is which. I also tested this without the HTML tags and included the php into a certain portion of a php page so only that portion changes.
My Name is Enigo Montoya
Posts: 33
188 credits Members referred : 0
« Reply #6 on: Dec 03, 2006, 09:56:56 PM »
Thank you both for your reply! I will look into the "switch" function.
My Name is Enigo Montoya
Posts: 33
188 credits Members referred : 0
« Reply #7 on: Dec 06, 2006, 05:56:22 AM »
I was unfamiliar with the switch function, but there are some really good ways to make the code super clean. This also makes since that it executes faster because it doesn't look down through each if/else statement.
Case "page": $inc = 'page.php'; Break;
include ($inc);
-------------- I also added parts like Case "": echo "Empty string"; Break;
I do have one other question though. With a default value set to like index.php how can I show a case that doesn't exist?
For instance, if someone types switch.php?id=abc and there is no case that defines "abc" how can the case be written to echo "page not found"? I haven't applied this to anything yet, but as I have it now if someone would type in abc as the string it shows the default page and not that the string doesn't exist.
Thank you, Brad
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6309
38674 credits Members referred : 374
It's time to use PHP5!
« Reply #8 on: Dec 06, 2006, 09:34:38 AM »
You can put all possible values into an array and test the value against this array with in_array
With this code, you are safe from XSS attacks, and you are supposed to have a directory with your content which will hold one php file for each page, while you have a way to know that someone asks for a page that does not exists.
Another benefit of this code is that it has the lower possible overhead, as with a switch() you could have problem if you had hundrends of pages.
My Name is Enigo Montoya
Posts: 33
188 credits Members referred : 0
« Reply #10 on: Dec 07, 2006, 06:06:46 AM »
Thank you Olaf. That is one of the nice things about php, many times there is more than one way to do something.
Thank you Nikolas. Excellent piece of code you provided. Ironically the last few sites I have been working on I put the files to be included into a folder named "content" so I didn't have multiple pages in the root directory. I know what you mean, it could get very messy with defining all the case statements while using the switch function. The code you provided will work great with building a template type site where the majority of the content will remain the same. It is shocking that a simple script does so much.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=5013