Hi again all,

I'm not really 100% sure what I did here but something messed up overnight in my script, and now I'm trying to find an alternate way to do the following:

I want to include a file depending on user given input, for example, include ('whatever.php'), except the "whatever" part of "whatever.php" has to be a variable. This is what I've come up with so far:

include ($content.'.php');

However, it's not working the way I wanted it to (for some reason I'm getting errors today.

ERROR: include(.php) [function.include]: failed to open stream: No such file or directory

Yesterday I was assisted to create this code (for which that include is included in) and it is as follows:

<?php
$content = (isset($_GET['con']) ? $_GET['con'] : NULL);
if (($content) == NULL) {
	include ('home.php');
}
else {
	include ($content.'.php');
}

?>

It was meant to function as my home page. If a user goes to index.php ($content being null) then it should bring them to the home page. Otherwise, take the value of $content and tack a .php on the end and go there. As long as I am running in the else statement on my page, it works fine, but as soon as I try to access index.php alone when $content is NULL, I get the error / notice posted above, however it doesn't really affect the site too much (over maybe performance, however that could just be my server).

Does anyone have any ideas? 😃

    Perhaps you should use:

    $content = (isset($_GET['con']) && !empty($_GET['con'])) ? $_GET['con'] : 'home';
    include_once($content.'.php');

    Make sure it's set, and if it is, make sure it's not an empty string (or a string with nothing but spaces in it 😉 )

      Wow, again, pure gold input 🙂 I love these forums 😃 Thanks a bunch! Took care of my crappy if statement and got rid of the errors!

        As long as you understand why your code didn't work, and why this does, please mark this thread as resolved.

          Of course, this allows them to inclue() any particular PHP file they like (including others in different directories), so usually you'd want to make a list of valid choices:

          $valid = array('home', 'members', 'contact');
          
          if(in_array($content, $valid))
              include_once $content . '.php';

          EDIT: By the way, to mark a thread resolved, use the link on the "Thread Tools" menu.

            Write a Reply...