Jump to content

Using $_GET variable in an include?


dirntknow

Recommended Posts

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

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.';
}
}

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!

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.

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.';
            }}
?>

I see you have ?orgname in all upper letters. When getting data from array its' key is case sensitive so Norasport != NORASPORT (i think :D)

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>';

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 {

 

:confused:

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.

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!  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.