dravu2000
Guest
|
Well I didn't see the method I use in the two tutorials here, so I thought I might as well show you mine. =3 <?php $pages = array("page1","page2","page3"); if(in_array($_GET['page'],$pages)){ include $_GET['page'].'.php'; } else { include 'error.php'; } ?> Ok, for bigger projects, I like to split everything up into small files and include them later on. In this case, the allowed pages of viewing are page1.php, page2.php, and page3.php. But if anyone tries to go somewhere like example.com/index.php?page=dead, then the if statement will return false and bring up the error.php file. Now to go a little more in-depth, I'll explain the different things in the code. First you'll see this: $pages = array("page1","page2","page3"); That's what's called an array. It basically lets you give a variable more than one value or an array of values. In this case, I wanna give the $pages variable the list of pages I want people to be able to access. Next, you'll see the If Else statement. One of the basic steps of PHP. Basically, it goes: if the statement inside the parenthesis is true, return the value in the brackets, if not, then skip to the Else statement and return the value there. After that, you'll see the in_array function: in_array($_GET['page'],$pages) Basically what this does is takes the value of your choosing and an array of values and compares the two. If the value you gave is in the array, it returns True. Otherwise, it returns false. In this case, if it returns false, it'll make the If statement false and send us over to the Else part. And after that, the $_GET variable. This is actually a system variable. It grabs information from the URL. Since I used $_GET['page'], it basically looks in the URL for the value of "page". To go along with this, if I wanted two variables in my URL, I'd just put an "&" symbol between them like "http://example.com/index.php?page=page2&post=blah". To go along with this is $_POST this grabs information from the headers. Forms for example. When you put "method='post'", it sends the content "invisibly" over to the next page where the $_POST command can grab it and use it without all the restrictions and problems with storing information in the URL. This is all more explained here. And last, but definitely not least, the Include function. This basically grabs the data of a specified file. For example; if you went to "http://example.com/index.php?page=page2", the code would output: include 'page2.php'; That will grab the data in page2.php and output it to the screen. So if page2.php had this in it: <?php echo "blah"; ?> then when you used the include, it'd grab the data and run it, therefor Echoing or outputting "blah" to the screen. Well I hope that helped! ^.^
Last Edit: Feb 22, 2008 1:21:26 GMT by dravu2000
|
|
|
|
I used to use this method, but I changed it recently. It gets annoying to have about 40 different things in an array and have it easily readable. Instead, I do this: $page = $_REQUEST["action"]; // $_REQUEST contains $_GET and $_POST among others if(file_exists("./modules/$page.php")){ include "./modules/$page.php"; } else { include "./modules/error.php"; } Something like that. I check access permissions inside of each file anyway, so if they try to get to it inside the modules directory they'll just get an error.
|
|
|
|
dravu2000
Guest
|
Ah. Well that's a cool method. I've never thought of that. So as long as you're not stupid and put something important in that directory that's readable with the include, that's like the perfect method. Thanks for bringing it up. ^^
Last Edit: Jun 23, 2007 21:29:53 GMT by dravu2000
|
|
|
|
Nice one, Chris. However, the method I am used to is the switch/case method. Although it's not as efficient as that, it allows for a little more customization. For example, I could call the same page from multiple actions. <?php switch($_GET['page']){ case "home": include("news.php"); break; case "news": include("news.php"); break; default: include("news.php"); } ?> Well, something like that.
|
wat
|
|
|
|
Yours is a bit long scorp. This works a bit better. But you can do that with mine too if you had to. Technically, you need to check to see if it's even set originally. <?php switch($_GET['page']){ case "home": case "news": default: include("news.php"); break; } ?>
|
|
|
|