emehrkay Posted August 27, 2008 Share Posted August 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/ Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626943 Share on other sites More sharing options...
emehrkay Posted August 27, 2008 Author Share Posted August 27, 2008 Awesome! What does concatenating a closing php tag to the file content tell eval to do? Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626953 Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626960 Share on other sites More sharing options...
sasa Posted August 27, 2008 Share Posted August 27, 2008 try <?php echo "File:<br/>\n"; echo file_get_contents('hello.php'). "\n<br/>\nOutput:<br/>\n"; echo file_get_contents('http://localhost/test/hello.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626967 Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 Or simply use include or require To grab output, use output buffering. <?php ob_start(); include('hello.php'); $output = ob_get_clean(); echo $output; Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626981 Share on other sites More sharing options...
emehrkay Posted August 27, 2008 Author Share Posted August 27, 2008 Thanks for the help. I will wrap the eval in an ob_start/end_clean block to return any output discomatt, I may try it that way Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-626989 Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 Eval is better for string-based evaluation. If you've already got your code in a PHP file and in an executable state, include or require is the ideal way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/121559-how-do-i-call-and-execute-a-php-script-that-is-in-another-file/#findComment-627011 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.