Current location: Hot Scripts Forums » Programming Languages » PHP » execute php from post data?


execute php from post data?

Reply
  #1 (permalink)  
Old 10-08-06, 01:36 PM
NabZ NabZ is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 1 Time in 1 Post
execute php from post data?

Hey i was wondering if anyone knows how to execute php code from post data.
eg:

i enter the following in a form:

PHP Code:

echo "test"

then i submit it and then it posts the data onto a php page which attempts to execute the code, the current code i got is:

PHP Code:

ob_start();

eval("?>".$_POST['code']."<?php ");
$return=ob_get_contents();
ob_end_clean(); 
echo 
$return;
any ideas why it wont work?
Reply With Quote
  #2 (permalink)  
Old 10-08-06, 02:46 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Take out the closing/opening php tags inside of the eval(...). Any comments or code you might have seen elsewhere in this forum about having an initial closing tag had to do with executing the contents of a file that had its own opening php tag and/or the file started out with HTML...

The following code works, but don't put this on any public web site (the last person who did something like this got his web page deleted using his own code) -
PHP Code:

ob_start(); 

eval(
$_POST['code']); 
$return=ob_get_contents(); 
ob_end_clean();  
echo 
$return
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3 (permalink)  
Old 10-08-06, 04:05 PM
NabZ NabZ is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 1 Time in 1 Post
hmm the above code does not seem to work for me :S
Reply With Quote
  #4 (permalink)  
Old 10-08-06, 04:27 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Post your form code that sends to this.

Also, this code is subject the same problems that would stop normal code from executing. Add the following after your opening <?php tag in the file this is in -
PHP Code:

error_reporting(E_ALL); 

__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old 10-08-06, 04:45 PM
NabZ NabZ is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 1 Time in 1 Post
these are the errors im getting:


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/nabz/public_html/NX1_245.php(54) : eval()'d code on line 1

Parse error: syntax error, unexpected $end in /home/nabz/public_html/NX1_245.php(54) : eval()'d code on line 1

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/nabz/public_html/NX1_245.php(54) : eval()'d code on line 1

Parse error: syntax error, unexpected $end in /home/nabz/public_html/NX1_245.php(54) : eval()'d code on line 1
Reply With Quote
  #6 (permalink)  
Old 10-08-06, 04:57 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
It looks like the magic quote setting is adding slashes to the form data. Use stripslashes(...) around the $_POST[...] data. Also, if you post the code that goes along with the error message it would help.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #7 (permalink)  
Old 10-08-06, 05:08 PM
NabZ NabZ is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 1 Time in 1 Post
after using stripslashes() it worked. thanks for that!
Reply With Quote
  #8 (permalink)  
Old 10-08-06, 07:49 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
PHP Code:

ob_start();

[B]eval("?>".$_POST['code']."<?php ");[/B]
$return=ob_get_contents();
ob_end_clean(); 
echo 
$return;
This is fantastically dangerous, probably one of the most hazardous uses of eval() that I've ever seen. It makes me cringe just looking at it. It would be a hugely bad idea to ever put this code on the web. And I mean hugely as in Titanic proportions.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #9 (permalink)  
Old 10-11-06, 01:13 PM
Sheepymot's Avatar
Sheepymot Sheepymot is offline
Newbie Coder
 
Join Date: Jan 2006
Location: England
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Indeed, such code is impossible to police.. I mean you could literally leave your server wide open.

I can't think of a logical reason why you'd want to execute this PHP anyway.. IN what situation would you want to give control of your server to strangers?

Do not use this code in a live envioronment.. Please.
__________________
PHPGurus - Free PHP Help
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking to data from this php format? Columns Help? cyberfolli PHP 3 05-06-06 02:09 AM
saving data with PHP form sXenoGJ PHP 4 04-30-04 11:25 PM
PHP: check data transfer on certain directories? Agum PHP 7 12-30-03 03:58 AM
php code not pulling data from DB simone PHP 1 11-22-03 02:08 AM


All times are GMT -5. The time now is 05:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.