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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.