¿Cómo puedo colocar un script PHP fuera del directorio raíz y vincularlo usando HTML?

I am using wamp, and I have two directories; /www and /scripts. In /www I have index.html, and in /scripts I have test.php. I am not too familiar with standard PHP directory conventions, but here is what I would like to have in my index.html file

...html statements...
<run the following php script: /scripts/test.php>
...html statements

rather than include the php script itself in the index.html file.

I have researched the ScriptAlias directive in Apache (http://httpd.apache.org/docs/2.0/mod/mod_alias.html#scriptalias) but I didn't quite understand how to apply it to this situation. Any ideas on how to proceed?

preguntado el 22 de mayo de 12 a las 21:05

Why have you tagges mysql with this question? -

3 Respuestas

You can't link to PHP outside the doc root from within HTML, but you can include PHP from outside the doc root from within PHP. I.e., replace the index.html with an index.php, that looks something like this:

...html statements...
require_once '/path/to/scripts/test.php';
...html statements...

contestado el 22 de mayo de 12 a las 21:05

Thanks everyone for the input. Would be interested to know whether variables declared in test.php will also be passed on to the index.php? For instance, having a private php file that contains a password and using require_once to get those? Obviously not ideal for security but you get the point. - jII

Yes, variables declared in test.php will be available to code that runs in index.php -- but only code después de the require. (So put the require at the top of the file.) - Alex Howansky

If you don't want the variables to be passed you can use something like get_file_contents or curl. - pitchinado

You cannot access files outside of the document root using HTML (luckily).

Think about what can happen when someone can just access stuff outside the document root, e.g.: /etc/passwd o lo que sea.

However if you use php you can access files outside document root by for example using: require or include. And this is IMHO best practice when doing something in PHP. In your document root you will only have a archivo de arranque which accesses files outside the document root.

This is because PHP is server side and HTML is client side.


If you want to access a file 1 directory above the current directory you can you a relative path:

`require_once '../thefile.php';`

contestado el 23 de mayo de 12 a las 08:05

Can't be done with HTML, however if you had an index.php file instead of an index.html file you could do it. I guess you could setup apache to compile html as php if you have to keep it as an html file.

contestado el 22 de mayo de 12 a las 21:05

Thank you for the input, I do not think it would be necessary to configure Apache in such a manner, as that might cause other HTML files in the directory to be parsed as well. - jII

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.