Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Insert a variable into an include path??

  1. #1
    Join Date
    May 2008
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Insert a variable into an include path??

    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?

    *actual file and variable names have been changed to protect the innocent

  2. #2
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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!

  3. #3
    Join Date
    May 2008
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    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???!

  4. #4
    Join Date
    May 2008
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default done did it

    <?php
    $var=$_GET['var'];
    $file ="dir/$var.inc.php";
    include($file);
    ?>

    I is so smart

  5. #5
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    You can actually just do this:
    PHP Code:
    <?php
    include('./path/to/file/'.$_GET['var']);
    ?>
    The . concatenates or merges string, whereas the + preforms the mathematical function of addition.
    Code:
    "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:
    Code:
    $var="dir/"$_GET['page']".inc.php";
    Is that it is missing the concatenation. It should be: (Notice the red dots)
    Code:
    $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.
    Last edited by Jas; 06-04-2008 at 03:20 AM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  6. The Following User Says Thank You to Jas For This Useful Post:

    corypark (06-04-2008)

  7. #6
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Bravo Jas!! This is what I was trying to do:

    In the first file, I set the variables and include the template file:

    Code:
    $rightcol = 'right-hm.html';
    include_once('check-template.html');
    Then in check-template.html there is this:

    Code:
    include($rightcol);
    So would this be it?

    Code:
    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
    Last edited by kuau; 06-04-2008 at 03:23 AM. Reason: forgot code tags as usual

  8. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Not sure what you are trying to do But
    Code:
    include('$_GET['$rightcol']);
    should be:
    Code:
    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 ?
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  9. The Following User Says Thank You to Jas For This Useful Post:

    kuau (06-04-2008)

  10. #8
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    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):
    Code:
    $rightcol = 'right-hm.html';
    include_once('check-template.html');
    so if you are trying to access the variable it would be: include($rightcol);
    Last edited by Master_script_maker; 06-04-2008 at 05:04 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  11. The Following User Says Thank You to Master_script_maker For This Useful Post:

    kuau (06-04-2008)

  12. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    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:

    Code:
    <?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
    Last edited by kuau; 06-04-2008 at 05:24 PM. Reason: add info

  13. #10
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    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 Code:
    <?php
    $file 
    './file.php';
    include(
    $file);
    echo 
    $var;
    ?>
    Name this file.php
    PHP Code:
    <?php
    $var 
    1;
    ?>
    Put both in the same directory and open test.php in your browser.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  14. The Following User Says Thank You to Jas For This Useful Post:

    kuau (07-01-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •