Jump to content

execute PHP codes from within C function in PHP extension


horobey

Recommended Posts

I am coding PHP extension. I need to execute PHP code from withing C function in this extesion. PHP code is stored in the string like

 

char * str = \"<?PHP echo(\'test\'); ?>\";

 

now if I call zend_eval_string() it complains about <?PHP ?> tags. I would like the same functionality like zend_eval_string() but with possibility to pass whole PHP file to contained in the string.

 

Any ideas?

yuriy@horobey.com

 

PS really tired looking to solve this... :(

Link to comment
Share on other sites


<?PHP

echo("PHP block 1<br>");

?>

this is not PHP<br>

<%

echo("php block 2<br>");

$v="this is not end %>";

%>

this is not PHP<br>

<script language="PHP">

$v=<<<EV

<?PHP

this is not PHP block

?>

EV;

</script>

<?

echo("<?PHP this is not PHP block?>");

?>

I think you can continue this sample. I mean it is difficult to split PHP file into valid PHP blocks

I think I need to study Zend code...

Link to comment
Share on other sites

no, that\'s pretty easy with a simple regex. But...I don\'t know what your extension is for, but if its just for you, a warning, don\'t use shorttags, script tags, or asp tags; most servers don\'t have them enabled. stick to <?php ?>.

Link to comment
Share on other sites

If the zend_eval_string function (which you want to use, im assuming its what does the work in zend, but I havn\'t looked and am at school now and cannot) (and which im hoping isn\'t like using eval(), heh ) doesn\'t like opening and closing tags, then you have to remove them. Separate your blocks, strip the tags, then feed them to zend_eval_string separatly. I havn\'t really looked at zend much, but daeken has. I don\'t really get what you mean by \"parse and execute the PHP file contained in the string\", but I\'m assuming you just want your extension to open and parse PHP from files...in which case im wondering why you\'re making the extension at all...

Link to comment
Share on other sites

  • 4 weeks later...
  • 4 years later...

may be horobey want to encrypt some PHP string code, decrypt inside the extension and executing there, right, horobey ?

 

If it is so, his problem is same as me.

 

I want to evaluate php string within an extension, i use zend_eval_string(), success for simple php code like:

 

$str = "print \"hello, world!\";";

 

but, display error when zend_eval_string() parse some php code like:

 

$str = "function {print \"hello, world!\";}";

 

Any solution for this?

 

Please, help.

 

If the zend_eval_string function (which you want to use, im assuming its what does the work in zend, but I havn\'t looked and am at school now and cannot) (and which im hoping isn\'t like using eval(), heh ) doesn\'t like opening and closing tags, then you have to remove them. Separate your blocks, strip the tags, then feed them to zend_eval_string separatly. I havn\'t really looked at zend much, but daeken has. I don\'t really get what you mean by \"parse and execute the PHP file contained in the string\", but I\'m assuming you just want your extension to open and parse PHP from files...in which case im wondering why you\'re making the extension at all...                   

Link to comment
Share on other sites

  • 4 months later...

  char *cBuffer;
  cBuffer = (char*)malloc(2048);
  memset(cBuffer, 0x00, 2048);

  strcpy(cBuffer, "echo 'this is some php text';");

  //execute decrypted php code via ZEND engine
  zend_try {
      zend_eval_string(cBuffer, NULL, "PHP Script");
      free(cBuffer);
  } zend_catch {
      free(cBuffer);
      php_error(E_ERROR, "Error occurred while executing PHP code, ERR - 101\0");
      RETURN_BOOL(0);
  } zend_end_try();

 

You could try this

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.