0

I have a PHP script that and at a certain point i want to include another script, but i want the script to run as if it is apart of the original file and doesn't have to be called by a function or a class:

if(strpos('X-Mailer: Microsoft Outlook', $raw_headers)) {
include 'outlookImageParse.php';
outlookImageParse($html_part);

but the included script doesn't work even when i define it as a function in that file and call it from the original. I've used, require, require_once, include_once, but none perform as how i want it to. What to do to get what i'm trying to achieve?

Tower
  • 1,287
  • 3
  • 15
  • 25

2 Answers2

0

When you include() (or require, etc) the script is treated as if it were dropped directly into the file, exactly where your include was - see the following example.

file1.php

<?php
$var1 = 'hello';
$var2 = 123;

include('file2.php');

echo $var4; // echoes "bbq"

$var5 = 123456;
func1($var5); // echoes "Function! 123456"
?>

file2.php

<?php
echo $var1; // echoes "hello"

$var4 = 'bbq';
function func1 ($var)
{
    echo 'Function! ' . $var;
}
?>
Joe
  • 15,669
  • 4
  • 48
  • 83
  • 1
    don't forget about `return` behavior for included files: http://php.net/manual/en/function.include.php#example-130 – Shad Feb 09 '12 at 00:51
  • Don't forget? I didn't know about it :P Interesting though. – Joe Feb 09 '12 at 00:52
  • I'm doing exactly this, though it still isn't treating it as if it we're dropped directly in the file, in fact it doesn't execute at all. – Tower Feb 09 '12 at 01:01
  • Sounds like there's something going wrong in your included file - step through it with `echo` and find out if it's going wrong somewhere. If that's not the issue, have a Google for "apache php include not working" :) – Joe Feb 09 '12 at 01:04
  • OR, make sure your `if` statement is getting executed. – Paul Dessert Feb 09 '12 at 01:19
  • Something has to be completely wrong, i searched through my entire script and found nothing blocking/coming in conflict with the following example `$b23 = "Microsoft Outlook"; if (strpos("Microsoft Outlook", $b23)) { echo '

    Email Client is Microsoft Outlook

    '; } else { echo '

    Could not identify Email Client

    '; }` Yet it still prints **Could not identify email client**
    – Tower Feb 09 '12 at 02:11
  • Post the source of `outlookImageParse` and the contents of `$raw_headers` in the first post, then drop another comment on here when it's done :) – Joe Feb 09 '12 at 02:13
  • Here is the source of them both: http://pastebin.com/4i4jMw4M $html_part is just the source formatting of the e-mail body, as if you were to right click the page and View the source. – Tower Feb 09 '12 at 02:47
  • Add `return $html_part;` to the end of your `outlookImageParse` function, then change the call to `$html_part = outlookImageParse($html_part);` – Joe Feb 09 '12 at 02:50
  • I did as you said, and still it isn't being included. You do mean call it as this right? `if (strpos('X-Mailer: Microsoft Outlook', $raw_headers)) { include ('outlookImageParse.php'); $html_part = outlookImageParse($html_part); }` – Tower Feb 09 '12 at 03:05
0

Are you referring to code injection? You should look at Best way to avoid code injection in PHP for the best practice advise. I don't know if it will make different, I would also put bracket around included file name.

Community
  • 1
  • 1
KSchoniov
  • 416
  • 4
  • 10