2

Okay, so I'm using require_once to load the contents of a .js file. However, there seems to be a 1 added to the end of the file after the require is done.

PHP:

echo require_once("test.js");

JS:

var newFunc = function() {
    console.log('yay!');
};

rendered html when the PHP file is loaded:

var newFunc = function() {
    console.log('yay!');
};1
CJT3
  • 2,788
  • 7
  • 30
  • 45
  • 1
    The way you are doing it seems completely contrary to best practices. You should add your scripts by using script tags. – Ermir Jun 24 '14 at 09:20
  • Objection noted, but this is for a document loaded via Ajax. I have a function that appends JS code to a script tag after the request has loaded. – CJT3 Jun 24 '14 at 09:23
  • 1
    require_once is closely same as [include](http://www.php.net/manual/en/function.include.php) and is mainly targeted for including php-scripts. Do not use it to include js. – Fdr Jun 24 '14 at 09:23
  • Seems to me you are leaving your site wide open to a JS injection vulnerability. Maybe you can see some other way you could achieve your goals? – Ermir Jun 24 '14 at 09:27
  • @Ermir this is not applied to anything submitted by a user. – CJT3 Jun 24 '14 at 09:28
  • @Ermir any suggestions on loading javascript per ajax page request? – CJT3 Jun 24 '14 at 09:31
  • 1
    Place your different scripts in different files, and then get only the URL of the script that you need. This way, you can then just create a script element in HTML and set the src attribute as the URL you just got. What this achieves is guaranteeing that only code written in predefined script files can be loaded, so it's safer. To really secure such as system, make sure to activate [Same Origin Policy](http://en.wikipedia.org/wiki/Same_origin_policy) – Ermir Jun 24 '14 at 09:59
  • @Ermir I decided to experiment with your idea. In order for it to remain dynamic enough, I decided that the ajax page would tell javascript to load its custom JS or CSS files with a header. This way a user couldn't inject a header (as they are sent before any user content is rendered), thus keeping the script secure and dynamically loadable. – CJT3 Jul 03 '14 at 10:39

3 Answers3

9

require_once() returns 1 if the include was successful. So you're echoing the return value, which is not what you thought it was.

Use file_get_contents() instead, although this is odd usage, you're probably heading in the wrong direction with whatever you're trying to do with this...

i-CONICA
  • 2,361
  • 9
  • 30
  • 45
  • I tried that, but it's not loading the file even though it's in the same directory. – CJT3 Jun 24 '14 at 09:24
  • What does file_get_contents() return? use print_r(file_get_contents($your_path)); to see what it returns. – i-CONICA Jun 24 '14 at 09:24
  • Warning: file_get_contents(test.js): failed to open stream: No such file or directory in... – CJT3 Jun 24 '14 at 09:29
  • Your path must be incorrect. Is your path quoted correctly as a string and in the same directory as the running PHP script? – i-CONICA Jun 24 '14 at 09:30
  • yes, it's the exact same path as I used for require_once – CJT3 Jun 24 '14 at 09:31
  • Try setting the second parameter to true, so file_get_contents("./test.js", true); – i-CONICA Jun 24 '14 at 09:34
  • Nope: file_get_contents(./test.js): failed to open stream: No such file or directory in... – CJT3 Jun 24 '14 at 09:36
  • Found the issue, I have to do this: `echo file_get_contents(dirname( __FILE__ )."/test.js", true);` – CJT3 Jun 24 '14 at 09:40
  • You don't "have" to use dirname(__FILE__). That's a bodge because you've used absolute path to the root of the drive. / is the root of the drive, it isn't relative to the current directory. "./" is, so you probably wanted file_get_contents("./test.js"), or just file_get_contents("file.js") – i-CONICA Apr 12 '15 at 20:13
2

If you want to import a script "on the fly" and want it to be only loaded once, we can "combine" require_once with a script, like so:

<script>
  <?php require_once('your-script.js'); ?>
</script>
<div>Foo</div>

Now, let's you have a component with that structure. If some of your templates includes that component many times, your script will only be embedded once, in the first one.

Felipe N Moura
  • 1,367
  • 11
  • 13
1

You should remove echo from "echo require_once("test.js");"

you can include the js file by following method

<script src="test.js" language="javascript"> </script>
Vigneshwaran
  • 387
  • 1
  • 2
  • 14