dirntknow Posted April 17, 2010 Share Posted April 17, 2010 Sorry for the noobish question but how do I get a $_GET variable from the url and put it into an include? The include now is this: <?php include("../includes/norasport.php"); ?> But i'd like to replace the norasport with whatever is in the $_GET['orgname'] if that makes sense? I know this coding is wrong but hopefully it demonstrates what i'd like to achieve!! <?php include("../includes/". $_GET['orgname'].".php"); ?> Thanks!! Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/ Share on other sites More sharing options...
SaMike Posted April 17, 2010 Share Posted April 17, 2010 <?php $fileget = $_GET['orgname']; include('../includes/'.$fileget.'.php'); ?> But i hope you arent going to use it exactly like that since that can be major security issue. Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1043774 Share on other sites More sharing options...
dirntknow Posted April 17, 2010 Author Share Posted April 17, 2010 That works fine but as a php noob, learning is slow! Is there any simple workaround for such an issue? I have a news page and would like to be able to populate it dynamically according to different organisations... Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1043783 Share on other sites More sharing options...
SaMike Posted April 17, 2010 Share Posted April 17, 2010 If there's fixed amount of organisations (pages to be included), one way to securily do it would be with arrays. Just make array like this: $pages['identifier'] = "file to be included". $orgname = $_GET['orgname']; $pages['Noobs'] = "organisations/noobs.php"; $pages['Leets'] = "organisations/leets.php"; if(!in_array($orgname, $pages)){ echo 'Error - Organisation not found!'; } else { if(file_exists($pages[$orgname])){ include($pages[$orgname]); } else { echo 'Error - Index not found, please contact administrator.'; } } Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1043787 Share on other sites More sharing options...
dirntknow Posted April 17, 2010 Author Share Posted April 17, 2010 Lol, I thought I almost understood it but when I tried to implement it on my page it displays 'Error - Organisation not found' I've got the correct file paths to the includes I'm sure and the $pages info is set to what is displayed in the url...where am I going wrong? <?php $orgname = $_GET['orgname']; $pages['British%20Supermoto'] = "../includes/scottishsupermoto.php"; $pages['Norasport'] = "../includes/norasport.php"; $pages['Southern%20Supermoto'] = "../includes/southernsupermoto.php"; $pages['Scottish%20Supermoto'] = "../includes/scottishsupermoto.php"; if(!in_array($orgname, $pages)){ echo 'Error - Organisation not found!'; } else { if(file_exists($pages[$orgname])){ include($pages[$orgname]); } else { echo 'Error - Index not found, please contact administrator.'; }} ?> p.s. I acknowledge the noobish spaces between the words, a long story but I didn't know i'd try using them like this! Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1043834 Share on other sites More sharing options...
DavidAM Posted April 17, 2010 Share Posted April 17, 2010 I think the $_GET variables are automatically url_decoded so you use a space instead of the %20. I'm not 100% sure of that, but it is worth a try. If it does not work, try echoing out the $orgname after you pull it from the $_GET array: $orgname = $_GET['orgname']; echo $orgname; you may have to look at the source (View->Page Source) because the browser may interpret HTML characters rather than display them. Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1043961 Share on other sites More sharing options...
dirntknow Posted April 18, 2010 Author Share Posted April 18, 2010 I've echoed the results of the variables and something doesn't look right. $orgname is good but $pages echos the word 'array'. I've uploaded the page and it's under the news tab: http://www.pitlanepass.com/pages/orgdetail.php?orgname=NORASPORT <?php $orgname = $_GET['orgname']; $pages['British Supermoto'] = "../includes/scottishsupermoto.php"; $pages['Norasport'] = "../includes/norasport.php"; $pages['Southern Supermoto'] = "../includes/southernsupermoto.php"; $pages['Scottish Supermoto'] = "../includes/scottishsupermoto.php"; if(!in_array($orgname, $pages)){ echo 'Error - Organisation not found! <br />'; echo $orgname .'<br />'; echo $pages; } else { if(file_exists($pages[$orgname])){ include($pages[$orgname]); } else { echo 'Error - Index not found, please contact administrator.'; }} ?> Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044080 Share on other sites More sharing options...
SaMike Posted April 18, 2010 Share Posted April 18, 2010 I see you have ?orgname in all upper letters. When getting data from array its' key is case sensitive so Norasport != NORASPORT (i think ) You could work it around like this: make all indexes lower letters in array $pages['norasport'] = "blahblah"; $pages['british supermoto'] = "bloo"; (You could also try replacing space with _ so $pages['british_supermoto']; and use it.) and then: $orgname = strtolower($_GET['orgname']); Also, you cant echo array. If you wish to print out array, use print_r($pages); Like this: echo '<pre>'; print_r($pages); echo '</pre>'; Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044112 Share on other sites More sharing options...
dirntknow Posted April 18, 2010 Author Share Posted April 18, 2010 OK, this code works: <?php $orgname = $_GET['orgname']; $pages['British Supermoto'] = "../includes/britishsupermoto.php"; $pages['NORASPORT'] = "../includes/norasport.php"; $pages['Southern Supermoto'] = "../includes/southernsupermoto.php"; $pages['Scottish Supermoto'] = "../includes/scottishsupermoto.php"; if(file_exists($pages[$orgname])){ include($pages[$orgname]); } else { echo 'Error - Index not found, please contact administrator.'; } ?> So, I guess the error is in the preceding section here: if(!in_array($orgname,$pages)){ echo 'Error - Organisation not found!'; } else { Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044184 Share on other sites More sharing options...
SaMike Posted April 18, 2010 Share Posted April 18, 2010 Well it should give you that index missing warning if it cant find the right page, but you could try this: if(!isset($pages[$orgname])){ echo 'Error - Organisation not found!'; } else { Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044207 Share on other sites More sharing options...
SaMike Posted April 18, 2010 Share Posted April 18, 2010 Well it should give you that index missing warning if it cant find the right page, but you could try this: if(!isset($pages[$orgname])){ echo 'Error - Organisation not found!'; } else { Ye you should defo add that since now i checked your page and it gives internal server error if you modify orgname to organization that doesnt exist. Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044217 Share on other sites More sharing options...
dirntknow Posted April 18, 2010 Author Share Posted April 18, 2010 Well it should give you that index missing warning if it cant find the right page, but you could try this: if(!isset($pages[$orgname])){ echo 'Error - Organisation not found!'; } else { Yes, that worked a treat. Many thanks for your patience and assistance above and beyond the call of duty! Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044237 Share on other sites More sharing options...
dirntknow Posted April 18, 2010 Author Share Posted April 18, 2010 Spoke to soon...I don't think I refreshed the page as it no longer works! The norapsort page is fine but the other three have a space inbetween... Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044266 Share on other sites More sharing options...
dirntknow Posted April 18, 2010 Author Share Posted April 18, 2010 Scrub that last comment!! It works when uploaded to the server but not locally...I can live with that!! Link to comment https://forums.phpfreaks.com/topic/198854-using-_get-variable-in-an-include/#findComment-1044277 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.