Mmarzex Posted April 11, 2010 Share Posted April 11, 2010 I'm using a simple php include code that will include a text file with content inside the main layout of a page. Now its working fine for including the main default page but whenever I try and include another content page by navigating to whatever.com/index.php?=test it doesn't work and still just shows the main text file I'm not sure why. and the code I'm using is <?php if (!$id) { $id = "main"; $include = $id . ".txt"; } else { $include = $id . ".txt"; } if (is_file($include) == "1") { include $include; } else { include "404.txt"; } ?> I'm also getting this error Notice: Undefined variable: id in /opt/lampp/htdocs/beta/index.php on line 8 Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/ Share on other sites More sharing options...
Mchl Posted April 11, 2010 Share Posted April 11, 2010 http://php.net/manual/en/reserved.variables.get.php Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040114 Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 At the start of the script use // this will set $id to the value in the URL, if none then use 'main' $id = isset($_GET['id']) ? $_GET['id'] : 'main'; Also, you say whatever.com/index.php?=test well that is wrong, you haven't specificed the id variable name. You'd need to use whatever.com/index.php?id=test Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040118 Share on other sites More sharing options...
Mmarzex Posted April 11, 2010 Author Share Posted April 11, 2010 At the start of the script use // this will set $id to the value in the URL, if none then use 'main' $id = isset($_GET['id']) ? $_GET['id'] : 'main'; Also, you say whatever.com/index.php?=test well that is wrong, you haven't specificed the id variable name. You'd need to use whatever.com/index.php?id=test I meant to tyype index.php?id=test and the code you gave me solved the error of it not working but I'm still getting the undefined variable error. This is the code I have now <?php if (!$id) { $id = isset($_GET['id']) ? $_GET['id'] : 'main'; //$id = "main"; $include = $id . ".txt"; } else { $include = $id . ".html"; } if (is_file($include) == "1") { include $include; } else { include "404.txt"; } ?> Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040150 Share on other sites More sharing options...
the182guy Posted April 11, 2010 Share Posted April 11, 2010 This line $id = isset($_GET['id']) ? $_GET['id'] : 'main'; Needs to go above this line if (!$id) { In other words, put it as the first line of the script. Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040153 Share on other sites More sharing options...
Mmarzex Posted April 11, 2010 Author Share Posted April 11, 2010 This line $id = isset($_GET['id']) ? $_GET['id'] : 'main'; Needs to go above this line if (!$id) { In other words, put it as the first line of the script. Thank you very much for your help its working perfectly now. Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040155 Share on other sites More sharing options...
Mchl Posted April 11, 2010 Share Posted April 11, 2010 Actually now it is no longer needed, because $id will alway have some value. Anyway, please now take a look at point 6 here: http://www.phpfreaks.com/tutorial/php-security (Please do read other points as well) Link to comment https://forums.phpfreaks.com/topic/198237-php-include-question/#findComment-1040158 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.