FatDank Posted June 26, 2011 Share Posted June 26, 2011 Hi all. Can someone help me please. Below is the code I have. What I want is instead of having to go index.php?page=home I just want to go to the root folder so say example.com/ and it will display what ?page=home is. So just to clarify; I want this: example.com/index.php?page=home To be this: example.com <?php if ($_GET['page'] == "home"){ include 'status.php'; }else if ($_GET['page'] == "todays"){ include("todays.php"); }else{ // . . . } ?> Quote Link to comment Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 You could do this: $page = (isset($_GET['page'])) ?: 'home'; That will only work with PHP 5.3+ though, so if you are using something lower than that you would have to do this: $page = (isset($_GET['page'])) ? $_GET['page'] : 'home'; After that you just use $page instead of $_GET['page'] in your if statements: $page = (isset($_GET['page'])) ?: 'home'; if ($page == 'home') { // include home page } if ($page == 'someotherpage') { // include some other page } In case you don't know, ? : is called a ternary operator. It's basically like a mini if statement. It does the same thing as this if statement: if (isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 'home'; } Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 26, 2011 Share Posted June 26, 2011 Since your $_GET['page'] variable has the same name as the files, here's a little variation that will handle any page name: <?php $page = isset($_GET['page']) && $_GET['page'] != '' ? $_GET['page'] : 'home'; include_once($page.'.php'); ?> Quote Link to comment Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 That's extremely insecure WebStyles. An attacker could use that to include PHP scripts located pretty much anywhere on the server. (http://yourserver.com/index.php?page=../someotherfile) Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 26, 2011 Share Posted June 26, 2011 No they couldn't his includes are hardcoded... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 26, 2011 Share Posted June 26, 2011 Personally I'd use a white list array ad do it this way: $page = isset($_GET['page']) ? strtolower($_GET['page']) : 'home'; $whiteList = array('home','about','contact'); if ( in_array($page, $whiteList) ) { include $page . '.php'; } You could also do: switch($_GET['page']) { case 'about': include 'about.php'; break; case 'contact': include 'contact.php'; break; default: include 'home.php'; break; } Quote Link to comment Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 @Andy, I was talking about WebStyle's code, not FatDank's code. FatDank's code is fine, but WebStyle's certainly is not. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 27, 2011 Share Posted June 27, 2011 My bad, sorry mate 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.