jhb Posted November 8, 2010 Share Posted November 8, 2010 I have a menu-item that links to for example example.com?page=y Then I need my php code to pull that "y" and put it in "include("pages/'$thatpage'.php");" Got this, that doesn't work: if (isset($_GET['page']) && $_GET['page'] == $page) { include("pages/'$page'.php"); } else { include("pages/index.php"); } ^ $page should then become the desired page, for example "y" This does though: if (isset($_GET['page']) && $_GET['page'] == y) { include("pages/y.php"); } else { include("pages/index.php"); } But i need it to be flexible, so i don't have to update my file every time i add a new page. Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/ Share on other sites More sharing options...
trq Posted November 8, 2010 Share Posted November 8, 2010 Why have you got single quotes around $page ? And where is $page defined? Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/#findComment-1131576 Share on other sites More sharing options...
jhb Posted November 8, 2010 Author Share Posted November 8, 2010 yeah. just figured it out But. I dont have a clue to how to show an error page if the desired file that are to be included don't exists. Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/#findComment-1131580 Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 How is $page defined? Do you have the code for that. It is *very* risky using variables to include files. For example, if $page is simply a clean equivalent of the $_GET variable..that's not good. On your other question, you would use file_exists(): if (isset($_GET['page']) && $_GET['page'] == $page) { if(file_exists("pages/$page.php")){ include("pages/$page.php"); }else{ //error page or code } } else { include("pages/index.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/#findComment-1131582 Share on other sites More sharing options...
jhb Posted November 8, 2010 Author Share Posted November 8, 2010 Got this to work: $page = $_GET['page']; if (isset($_GET['page']) && $_GET['page'] == $page) { if (file_exists("pages/$page.php")) { include("pages/$page.php"); } else { echo "<h1>Finner ikke siden</h1>"; } } else { include("pages/index.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/#findComment-1131584 Share on other sites More sharing options...
trq Posted November 8, 2010 Share Posted November 8, 2010 The first line will throw an error if the 'page' index doesn't exist. that is the entire point of the if statement following it. Code should be.... if (isset($_GET['page'])) { $page = $_GET['page']; if (file_exists("pages/$page.php")) { include "pages/$page.php"; } else { echo "<h1>Finner ikke siden</h1>"; } } else { include "pages/index.php"; } Quote Link to comment https://forums.phpfreaks.com/topic/218056-flexible-url-examplecompagedesiredpage/#findComment-1131588 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.