You are invited to Log in or Register a free Frihost Account!

[PHP] Executing a local file

Stubru Freak
I want php to open a local file, but instead of just opening the source code, the file should be executed as it would be if a browser requested the file. I don't want to use include() as the file should run seperatly from the main page (it should also be executable by users).
Is there another way then opening the file via an external connection? (i.e. starting with scheme://)
I am confused. What language is this file written in?

If it is in PHP all you need to do is put "<?php .............. ?>" tags around the scripting for it to execute everytime someone loads that page.

Any more information?
if you save the file with the extension .php, and put all the scripting within <?php .. ?> tags, then the file will be executed automatically...
The file should execute properly if you have given it the extension '.php' and have written it properly without any errors.
If you have done all this and yet it isn't executing then there's something wrong with your account's php which is most unlikely OR you are trying to open it straight from your control panel. Try typing the full URL and then see if it works.
Stubru Freak
It's working when a user is requesting it, but I want another php script to execute it.
Then you will have to use include() or require() [which is the same thing but with a stop error].

I'm not sure why you need to run the script "separately." Is it to preserve variable values? If it is the easiest way to convert your existing code so this doesn't happen would be to make the code on the included page into a function, with parameters for the variables you don't want to change.

You can then include the file, which just defines the function and doesn't execute it. Then directly after the include(), call the function as necessary. Functions have a separate memory space so shouldn't change anything you want to keep.
Stubru Freak
The problem is that I made an AJAX system that fetches those pages from the server, gets the title and set the current title to that title, and displays the page in a <div>.

For users without javascript I want to do this server-side, but I don't want to create every page twice, so php should get this page the same way as AJAX, also remove the title from it, then display the rest on screen.

I'll think about the include method you said. My current solution is to get the page using cURL, but that sure isn't the best way.
read the file contents into a string

Use eval() to execute the file
Hmmm... I'm not sure what eval() will do if it encounters raw HTML... it might throw errors.

Also, note that file_get_contents() only works with PHP >= v4.3, if you have a version lower than that then if you read the link KV posted there's a function definition which performs the same job in earlier versions.
Stubru Freak
kv wrote:
read the file contents into a string

Use eval() to execute the file

Not a good solution because I want to be able to put separate variables in the file, also the file has to be able to redirect.
And isn't that what you're doing the same as an include?
Maybe you can use header() or a javascript function to open the page?
Stubru Freak
I use javascript but I need a server-side solution for browsers that don't use javascript, and I guess you mean a Location: header, I don't want to redirect, I want to get the output of php code to use in another file.

But nvm I think cURL is the best solution.
Have you searched for fun and relevant things? Try searching for "execute" and see what comes up in the function list.
Stubru Freak
No nothing relevant
Related topics
Cant access admin area after updateing to 2.0.15
please help..
users online, using php and text file
PHP function(); &file=""?
Site error
about install phpbb forum
.htaccess problem
PHP Problem: Unable to execute ext. commands
[PHP] Weird Errors and lost files.
PHP Read File Help needed.
PHP Snippet
php script for Changing file permissions & owner
Session directory permissions issue
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.