开发者

PHP include/require a different extension file as .php

I have a .js file with code compatible for both php and js languages. The file is read and edited by php and then fetched by JS. so the extension is JS, is there any way that i can require this file in my php script and the file may execute normally as a require/include would work on a .php file.

I dont want to Eval the file contents in PHP because Eval is time costly and risky.

SO is there any way to include/require non php extension files as a php file ?

edit : the files content are similar to the text below, this file is read/written by php and then included in my page via si开发者_C百科mple js file inclusion as

$my_array = Array();
$my_array['id_01'] = 100;


I have a .js file with code compatible for both php and js languages

hmmm, yes it is possible but I suspect your code is NOT compatible. Include/require works regardless of what the file extension is.

s there any way that i can require this file in my php script and the file may execute normally as a require/include would work on a .php file

You seem to think it's not compatible either.

Your post is so confused its hard to tell what you're really trying to achieve here - but the following code is valid in both languages and would execute where the relevant functions are defined:

// <?php

hello_world("I am bilingual!");


In an .htaccess file:

AddHandler application/x-httpd-php .js

Make sure you set the header content type to application/javascript before you output anything:

header('Content-Type: application/javascript');

EDIT

After reading the edited question I think I understand.

If you take a JavaScript file which has PHP within it:

<?php

$var = 'this is a example';

?>
var foo = 'javascript bar';

You can include this file into a PHP script and access the variable $var, however whatever is outside of the <?php ?> will be outputted to the screen. You could then include this same exact file as a javascript src file and access the variable foo from within JavaScript. I have no idea why you would actually want to do this though.

If you tell us what you are trying to accomplish we might be able to offer alternate solutions.


Any file of any kind can be included/required, including images. To PHP, it's all relatively just text. All that matters to be able to execute PHP code is what's inside the <?php ?> block. When included, all text found within the open/close tags will be executed as PHP, while anything outside will render as normal text.

To test this out, simply open a .JPG file in notepad, add <?php phpinfo(); ?> to the very end, save, then include this file in a PHP script like you would a PHP script file. You'll see a jumble of text, but after that, the full phpinfo dump. You can even use Content-type: image/jpg header to output the page as a true image, but the full phpinfo dump will stil be hidden within.

Edit: be sure to properly label your mime type on your outputted page, as evolve said above, as a courtesy to the site visitor.


In your apache configuration:

AddHandler php-script .js

This tells apache to handle *.js files as PHP scripts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜