View Full Version : Insert a variable into an include path??
corypark
05-31-2008, 12:46 AM
In a crazy actionScript/php hybrid it would look something like this:
include("dir/"+$var+".inc.php");
If that makes no sense here's the long version:
If I've got a file "neato.inc.php" in the "dir" directory that I want to include into the "test.php" file in the root directory
and I've called "test.php" with a link href='test.php?page=neato'
and "test.php" has "$var=$_GET['page'];" string on it...
am I going about this all wrong?:confused:
*actual file and variable names have been changed to protect the innocent
I tried very hard to use a variable in an include a while back and was not able to do it. What happened when you tried it? I'd like to know if you get it to work. Good luck!
corypark
06-02-2008, 06:14 PM
hmmm... judging from the sole response and lack of any others.. is it impossible?? Seems strange.
How about when I make the variable? Something more like:
$var="dir/"$_GET['page']".inc.php";
Is there no way to insert a variable???!
corypark
06-02-2008, 06:57 PM
<?php
$var=$_GET['var'];
$file ="dir/$var.inc.php";
include($file);
?>
I is so smart;)
You can actually just do this:
<?php
include('./path/to/file/'.$_GET['var']);
?>
The . concatenates or merges string, whereas the + preforms the mathematical function of addition.
"string"+"String" == 0
"String"."String" == "StringString"
And, since the order of functions goes from inside parenthesis to outside, the string in concatenated before the value goes to the include function.
The problem with this code:
$var="dir/"$_GET['page']".inc.php";
Is that it is missing the concatenation. It should be: (Notice the red dots)
$var="dir/".$_GET['page'].".inc.php";
Security should be taken into account, however, on what files can actually be included since you are using the $_GET super global, which any user can edit. Thus, the user can include whatever file they want. Probably a bad idea.
I hope that answers your questions. :)
Bravo Jas!! This is what I was trying to do:
In the first file, I set the variables and include the template file:
$rightcol = 'right-hm.html';
include_once('check-template.html');
Then in check-template.html there is this:
include($rightcol);
So would this be it?
include('$_GET['$rightcol']); (it's on the include path)
I'd like to get it straight before I try it for the 25th time :) Thanks, e
Not sure what you are trying to do :confused: But
include('$_GET['$rightcol']);
should be:
include($_GET['$rightcol']);
Other then that, I need more information.
You are getting the value form the user's URL? Or are you getting it from the variable $rightcol ?
Master_script_maker
06-04-2008, 04:59 PM
jas, i think by rightcol he was refering to the variable .i think you confused him with the word superglobal ( like you could access vars with it like the var was a global):
$rightcol = 'right-hm.html';
include_once('check-template.html');
so if you are trying to access the variable it would be: include($rightcol);
Oops, I see the extra quote..sorry (typo).
OK, now I would love to understand this, because it didn't make sense to me at the time. I agree with MSM that include($rightcol); should have worked. But I assure you, it did not. All it gave was a blank section on the page.
The ONLY way I could get it to work was to place the content of the right-hm.html file into a database field and load it using mySQL, and then use this in check-template.html:
<?php echo $rightcol; ?>
It may have something to do with having an include inside an included file (?).
Jas, what I am trying to do is to be able to use the same template file to create several similar pages, but give each one of them a different right column. Simple really. I am able to do it using the above method (mySQL), but would prefer to use MSM's method. I want to try your command as soon as I get a chance to see if it will let me. Can either of you please explain this seeming anomaly? Thanks. e :)
It may have something to do with having an include inside an included file (?).
No, that should not cause any problems. I have done that many times before.
Are you actually printing something out in the $rightcol file? And is the path correct? It might be good to post the code in the two pages you've mentioned.
It might also help to see an include script in action, so try playing around with this:
Name this test.php
<?php
$file = './file.php';
include($file);
echo $var;
?>
Name this file.php
<?php
$var = 1;
?>
Put both in the same directory and open test.php in your browser.
Dear Jas: I finally found time to get back to this and it worked perfectly the way you explained it. Thank you so much! I was making it much more complicated than it actually was. I use this technique quite often so now can do it in a much simpler way. Thanks again. erin :)
techietim
07-01-2008, 11:52 AM
If you are ever include things like that with $_GET, make sure to sanitize it first. A rouge user would be able to include other files from your site that you don't want them to see.
Just a word of warning.
Dear tt: Actually I'm not using $_GET, just include($filename). Please explain how someone could include files from my site.. I'm not sure what to protect against. Thanks. I may be a bit oblivious to what creepy people are trying to do and why.
techietim
07-01-2008, 12:55 PM
Say you had a htaccess protected page named users/secretstuff.txt
And lets also say that index.php would include anything in $_GET['hello']. A user could go to example.org/index.php?hello=users/secretstuff.txt and see the contents of that text file, would would be very horrible.
I went back to the beginning of this post and now I see what you are talking about -- providing too much information in the query string visible in the address bar. Yeah, I can see how that would be dangerous. I'm not doing anything like that, just including different headers etc, but thanks for the warning. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.