How to execute PHP code entered from textbox or textarea

Advertisement

Yesterday, Sujit asked me how can we execute the php code entered through textarea in PHP. In many cases, it’s not good to execute the php code entered from textarea in PHP from the secruity point of view but in some cases you may have to do this and I’m going to tell you here how you can do this in PHP.

 

Execute the PHP code entered through textbox or textarea

echo "echo 'test';"; //just prints echo 'test';

Above line just prints echo ‘test'; , you can’t do it from that way. For executing the php code entered form textarea, you have to take the help of useful PHP function called eval() . Take a look at few examples of eval() function to execute the PHP code which is supplied as a string.

eval("echo 'test';"); // prints test in browser
eval(" phpinfo(); "); //outputs the information provided by phpinfo()

Isn’t that easy to do? eval() comes handy when you’ve to execute the PHP code which is supplied as a string . But always be careful, there are a lot of dangerous things can be done if PHP code can be executed from eval() to invade the security of your website.

Tags:

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

2 Comments on “How to execute PHP code entered from textbox or textarea”

  • Tarquin wrote on 20 October, 2008, 19:55

    Only just saw this,

    So pretty much it allows you to, lets say, create a page in a administration panel for your website, where you can run SQL and PHP commands from frontend instead of creating a new page to do it?

    This seems like a good idea, my only suggestion would be to add a master password or validation tool if you use it. This can cause serious problems if the wrong person got their hands on to it.

    I might use this though, so thanks again for a great post!

Trackbacks

  1. How to execute PHP code entered from textbox or textarea | Programmers Blog

Write a Comment

 


Copyright © 2014 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.