Jump to content

How do I call and execute a php script that is in another file


emehrkay

Recommended Posts

I am creating my own CMS (i know i know) and in it there is a script editor where I want to give people the ability to write little php scripts to do certain tasks. So if someone writes

 

<?php
    echo 'hello';
?>

 

and saves it as hello.php, how do I execute hello.php from inside another file and how would I grab the output if there were any?

 

This seems like something that should be simple.

 

Thanks

When you are intentionally executing code that someone else has written, this is a perfect use for the eval() function. We have to do the same thing for our internal CMS at work, and you will find yourself using eval() quite a bit within a full CMS. Now, keep in mind that you have to do some extensive checking for security measures, too, or they will be able to run any script they can write. If you have them on their own server, this won't be as big a deal, though.

 

<?php
$txt = file_get_contents('hello.php');
eval('?>' . $txt);
?>

Awesome! What does concatenating a closing php tag to the file content tell eval to do?

 

It closes out the PHP tags of the executing script so that the included script doesn't throw a parse error. If your eval'd script doesn't have PHP tags around it, it is not needed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.