Script to execute code

Hello, is there some PHP script for executing some code on the server? For example, create execute.php and have some text area where i will put php code and execute button to execute script, that is possible?

Yes!

You can store the text from a “text-area” tag into a file with put_file_contents() functions. And, then call it using AJAX to execute it. The results of the script could be either saved in a file that could be loaded or just returned from the AJAX call. You can also send the data to the file instead of saving it in a file. Either way would work. I think for your uses, just sending the data in the AJAX call would be best. Here is a site that explains how to do a simple AJAX call.
http://thisinterestsme.com/simple-ajax-request-example-jquery-php/

Hello, i have found the source code:

<? $memo=$_POST['memo']; if(isset($_POST['save'])) { $fs=fopen('text.txt',"w"); $text=fputs($fs,$memo); fclose($fs); } $text=''; $f=file('text.txt'); for($i=0;$i <? echo $text; ?>

so that code is saving text in the text area to file, so how can I not save but execute it? Do I need to use AJAX to execute it? I don’t know if I have the AJAX installed on the computer do I need another library? I have PHP 7 installed and using php web server that is pre-installed in PHP

Well, put_file_contents is easier than all that file manipulation. File-Put-Contents

But, your old style way does work. Now, assuming you are putting script or in other words PHP code into that file, you would need to do these things to run that code.

   1 - Replace the name text.txt as you can not execute text files.
   2 - Call the file using AJAX  as in the previous link I posted  OR, just go to the new page using redirection
   3 - Have a way to view the results of the code in the new file.  Either by just going to the page or by
        returning data to display from the AJAX call.

A few other notes… You said you wanted to execute code on the server from inputting it on a webpage. This can be done, but, of course is not very secure. Anyone who gets access to your webpage can erase your server files. Therefore, you would need to password protect your webpage. And, also include security code so that you do not execute commands that can damage your server in any ways. Perhaps you could explain why you want to do this type of process.

oh i know it my server is not 24/7 i just turning on when i need it so i can test some scripts, can you please tell me i just fount php Eval function is there possible to put php code in text area to Eval function execute it?

Well, for testing, most people use Wamp or Mamp on their main system. This gives you a full server on your laptop or desktop. That way, you turn it on and then just test your files live. Saving your code in a test.php file or several files as needed is just editing them. You just save your files in a “www” folder under Wamp or Mamp’s folder. Then you can access them by just using your browser to test them. I suggest you look into that.

As far as using your server and the EVAL function, you can do that, but, it is very tricky. You would need to store the code to test and then insert it into your live code and also set up all of the variables before you load the saved codes. That can be tricky to debug.

If you have a server already set up, just use it as it is supposed to be used. Edit a file and view the file in a webpage. If you want to set up a testing system for “snippets” of code, just have your page save the script as a file, a uniquely named file and then have the browser which is running your editor code just open a second browser page with your newly entered code in place. That way is super easy to do.

If you want to use EVAL function, you would need to enter the script in your text-area tag, have a button to process the script. The page would have to refresh itself with the EVAL function displayed below the text-area so you could see it’s results. A very tricky thing to deal with as you would have little in the way of error tracking. It would mean turning on all of the error reporting options. Not too big a deal, but, this would not test scripts very well since you would have to manually enter the data for each variable inline in the script. A tedious way to tests scripts.

actually i found the script :

<form method="post" action="process.php">
<textarea name="code" cols="40" rows="5"></textarea><br>
<input type="submit" value="Submit" />
</form>

and process file:

<?php
$result = eval($_POST['code']); 
echo $result;
?>

can you please tell me how can i put it in one file, so dont use process.php that is possible?

I guess I am not sure what you keep asking about. You keep switching ways to handle this.
Maybe that is why your name is “switchy”. Ha!

So, this last code you posted is a simple form.
It posts to itself.
Once posted, it pulls the text from the textarea tag and displays it.

So that works but does not save the file. So, to save as a file, you need to add a line to save it.
<?PHP
$result = eval($_POST[‘code’]);
file_put_contents(“test1.php”, $result);
echo $result;
?>
After that the file would be saved in the folder that the webpage is in. Once that is done, you can just access it in your browser as test1.php …

Yes trully i am switching ways))) i dont need to save it to file,i just need it to execute the code… script is working well just want to know how to put whole that script in one file. so the form and process file be in one php file

<?PHP
$result = "";
if (isset($_POST['submit'])) {
    $code = $_POST['code'];
    $result = eval($code);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Page</title>
</head>
<body>
<form method="post" action="process.php">
<textarea name="code" cols="40" rows="5"><?PHP echo $code; ?></textarea><br>
<input type="submit" value="Submit" />
</form>
    <h3>Results:</h3>
    <?PHP echo $result; ?>
</body>
</html>

This will handle all of the parts you mentioned. It does NOT save the results as a separate file.
To do this, you would need to add in a filename to save it as and to save it as a new filename.

1 Like

thank you very much, you really helped me… but when you meant about security, now i want to password protect that file))) but thanks anyway thank you!

Well, security is done many ways. You said you have your own server, and you turn it on when you want to work on your code. This means that your code is not sent out over the internet, so it is secure.

But, if you have others in your room and want to protect it, you could just add a password to it.
Just have another input filed type=‘password’ and enter the password. If the password does not match
your secret version it would not process the eval() function. Just do not process the data so it stays blank.

The problem is that if the server you have is not private, then, you would need to set up a database with a
field for the passwords. There is a lot of examples of this online.

1 Like

one more thing i still need process.php file, can i do execution throw index file?

i put index.php file as action but execution failed what i did wrong?

Forms are handled this way…

A form needs a submit button that tells the page (or browser) to submit the data to the server.
The server checks the action= text and transfers all of the posted data to that page. In this case, process.php.
The server then processes the “action” file using the posted data that was sent to it.
Therefore, the page I posted before would need to be named “process.php”. It would post to itself.
Normally, this is the way that forms are posted. Once the file reads all of the code in the posted
action form, it sends the results back out to the browser. If the code needs to switch to another page,
you can do that in code. Such as if a user is an admin, they might have a different page to go to.
But, in general, you just post back to the same page and handle the code as needed.

Hope that makes sense to you…

1 Like

Undefined variable: code C:…\process.php on line 15
i have rename file to process.php here is the error

What you want is HIGHLY dangerous

why? it does not matter I will turn on server when i will be working with it

Yes, it does not matter for this project. Astonecipher didn’t read the top parts of this thread.
But, on his side, this is not normal way to test code. Normally, you just use an editor and create a new
file for each test and save it on the server and run it from a browser. A lot of us here use the Netbeans IDE
for editing. You just link your Netbeans to the folder you have your test files in. Then, you can live edit them.
Much easier than your process…

So, back to the last error… Which line is #15?

<textarea name="code" cols="40" rows="5"><?PHP echo $code; ?></textarea><br>

Sponsor our Newsletter | Privacy Policy | Terms of Service