1

I am trying to include different header files based on the variable passed from another page. The variable is $section. Here is my code:

 <?php include("header".$section.".php"); ?>

I am trying to include headerAbout.php or headerMenu.php or.... based on the variable $section. But I am getting an error:

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

I dont know why the variable is not being concatenated. can someone clarify this?

4
  • 1
    What happens when you do a var_dump on $section? Commented Jun 27, 2011 at 17:09
  • just put echo for that variable, weather it get value or not? Commented Jun 27, 2011 at 17:11
  • I am sorry. I found out the mistake. Kindly refer to my comment below. Commented Jun 27, 2011 at 17:20
  • Consider path traversal exploits if you take that $section as raw URL parameter. Commented Jun 27, 2011 at 23:28

3 Answers 3

2

Given that it's not being concatenated, the variable's not defined at the point you try to use it in the include. Where are you setting its value?

Sign up to request clarification or add additional context in comments.

1 Comment

I found out the problem. I was previewing it from firefox directly and it does not give any value to the url variable. Hence the value for the variable for $section was not being set. If I give the value in the url, its working fine.
1

Yes, the include language construct just expects a string as parameter value. This string value can either be from a string literal, a variable/constant or any other expression that returns a string (e.g. function/method calls).

The cause of your error is probably that $section doesn’t contain a value you are expecting.

Note that you should be aware that if you allow user input to be part of the file location you might be vulnerable to path traversal attacks. So make sure to validate the value.

1 Comment

Thank you. Kindly refer to my comment.
1

It is working correctly. Its likely that $section does not contain a value in the current scope or at that point in execution.

1 Comment

Thank you. Kindly refer to my comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.