insejn Posted July 5, 2009 Share Posted July 5, 2009 This is probably an easy task, but I cant seem to find a solution for it.. so I need some help I'm working on a website where I display different parts of the page depending on what comes after the questionmark (?), for example "www.website.com/index.php?home". if(isset($_GET['home'])) { echo "..the content for this page"; } ..that's working fine, the problem is how I do when I enter the index.php without anything after the questionmark, for example "www.website.com/index.php". I thought putting this code in would solve it: if(isset($_GET['home'])) { echo "..the content for this page"; } else { echo "..show something similar as the home-page"; } ..but then that ends up on every other page (products, sales, contact etc.). Is there a way to either show the same content for home or redirecting to index.php?home if someone enters www.website.com? Quote Link to comment https://forums.phpfreaks.com/topic/164855-solved-redirect-to-indexphphome-when-entering-indexphp/ Share on other sites More sharing options...
HPWebSolutions Posted July 5, 2009 Share Posted July 5, 2009 Hi insejn, You could use 'www.website.com/index.php?page=home' and then check $_GET['page'] to determine what web site content to display. Use this: if( $_GET['page']=='home' || !isset($_GET['page') ) { echo "..the content for this page"; } elseif ( $_GET['page'] == 'products' ) { echo "product page"; } A switch statement would work nicely here. Quote Link to comment https://forums.phpfreaks.com/topic/164855-solved-redirect-to-indexphphome-when-entering-indexphp/#findComment-869284 Share on other sites More sharing options...
SuperBlue Posted July 5, 2009 Share Posted July 5, 2009 Its generally something i would recommend strongly ageinst. The user shouldn't be redirected when visiting the main site, just because of poor design. You can easily account for the case where the variable isn't set, so do that, and be done with it. I would also say that you should avoid showing a page in the browsers address bar, unless the user is viewing some Subpage of your site. Showing http://brugbart.com/index.php is optional, you may as well just show http://www.brugbart.com/ Note you can still use url parameters, without specifically including the index.php file. I.e http://www.brugbart.com/?page=subpage Of cause i also recommend that you rewrite these urls on the server, to make them a bit cleaner. You can then dissallow direct access to index.php entirely, by checking the requested path. And that way easily avoid the risk of duplicated content, when your site is accessible on both the domain, and the index. I.e. if ($_get['page'] == 'home') { // Do stuff here } else if ($_get['page'] == 'subpage1') { // Do stuff here } else if ($_get['page'] == 'subpage2') { // Do stuff here } else { // not recognised, so we show the FrontPage } Or even better, use a PHP switch in the top of your source, and include the relevant php as needed, to keep your code as easy to manage as possible. switch ($_get['page']) { default: // not recognised, so we assume the FrontPage. $RequestedPage = 'home'; break; case 'subpage1': // Do stuff here $RequestedPage = 'subpage1'; break; case 'subpage2': // Do stuff here $RequestedPage = 'subpage2'; break; } // Then include the following in your body, depending on your design. include_once $RequestedPage . '.php'; Quote Link to comment https://forums.phpfreaks.com/topic/164855-solved-redirect-to-indexphphome-when-entering-indexphp/#findComment-869286 Share on other sites More sharing options...
insejn Posted July 5, 2009 Author Share Posted July 5, 2009 Thank you for the responses! Blueoden: I tried the first suggestion you gave, the result was that the page only showed the last "else" statement.. I thought I perhaps did something else wrong displaying the URL. I like the "www.website.com/?page=home" best, but it shows the "else" statement instead of the home content. This is what I wrote with the help of your code: if ($_get['page']=='home') { echo "content of Home"; } else if ($_get['page'] == 'products') { echo "content of Products"; } else if ($_get['page'] == 'contact') { echo "content of Contact"; } else { echo "..else content.."; } ..shouldn't this work if I enter www.website.com/?page=home ? ADDITIONAL COMMENT: Oh my god, I'm such a nOOb.. I just needed to change the small letters in get to GET. It seems to work much better now Quote Link to comment https://forums.phpfreaks.com/topic/164855-solved-redirect-to-indexphphome-when-entering-indexphp/#findComment-869298 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.