eval, include and output buffering

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    eval, include and output buffering


    I'm using eval to excute some mixed php and html code but I cannot debug it.
    I am essentially using filegetcontents to load up a php/html file and then
    inserting it into another php/html file and then using eval to execute the
    final product.


    If I were to use include and output buffering instead of filegetcontents
    would it allow be to debug the code? (I have to capture the include so it
    can be modified which is why I used filegetcontents and eval in the first
    place).



    essentially instead of something like eval(mod(filege tcontents()))

    I would have

    ob_start();
    include $filename;
    $contents = mod(ob_get_cont ents());
    ob_end_clean();


    As far as I can remember mod only modifies html code but I can't be
    completely sure. In any case I'm not sure how the include eval the code when
    its buffered as if its just the output or what? Right now everythign is
    working fine and I don't want to screw it up but I'm kinda dragging my feet
    because of the debugging issues.


    Thanks,
    Jon


  • Chung Leong

    #2
    Re: eval, include and output buffering

    On May 10, 4:29 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
    I'm using eval to excute some mixed php and html code but I cannot debug it.
    I am essentially using filegetcontents to load up a php/html file and then
    inserting it into another php/html file and then using eval to execute the
    final product.
    >
    If I were to use include and output buffering instead of filegetcontents
    would it allow be to debug the code? (I have to capture the include so it
    can be modified which is why I used filegetcontents and eval in the first
    place).
    >
    essentially instead of something like eval(mod(filege tcontents()))
    >
    I would have
    >
    ob_start();
    include $filename;
    $contents = mod(ob_get_cont ents());
    ob_end_clean();
    >
    As far as I can remember mod only modifies html code but I can't be
    completely sure. In any case I'm not sure how the include eval the code when
    its buffered as if its just the output or what? Right now everythign is
    working fine and I don't want to screw it up but I'm kinda dragging my feet
    because of the debugging issues.
    >
    Thanks,
    Jon
    An alternative to using eval() is to implement a stream wrapper, then
    using include/require on a custom URL. What I would do is save the
    generated content to a temporary file during debug so that you can
    more easily see where an error occur.

    See http://www.php.net/manual/en/ref.stream.php

    Comment

    • Jon Slaughter

      #3
      Re: eval, include and output buffering


      "Chung Leong" <chernyshevsky@ hotmail.comwrot e in message
      news:1178812844 .248954.101410@ y5g2000hsa.goog legroups.com...
      On May 10, 4:29 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
      >I'm using eval to excute some mixed php and html code but I cannot debug
      >it.
      >I am essentially using filegetcontents to load up a php/html file and
      >then
      >inserting it into another php/html file and then using eval to execute
      >the
      >final product.
      >>
      >If I were to use include and output buffering instead of filegetcontents
      >would it allow be to debug the code? (I have to capture the include so it
      >can be modified which is why I used filegetcontents and eval in the first
      >place).
      >>
      >essentially instead of something like eval(mod(filege tcontents()))
      >>
      >I would have
      >>
      >ob_start();
      >include $filename;
      >$contents = mod(ob_get_cont ents());
      >ob_end_clean() ;
      >>
      >As far as I can remember mod only modifies html code but I can't be
      >completely sure. In any case I'm not sure how the include eval the code
      >when
      >its buffered as if its just the output or what? Right now everythign is
      >working fine and I don't want to screw it up but I'm kinda dragging my
      >feet
      >because of the debugging issues.
      >>
      >Thanks,
      >Jon
      >
      An alternative to using eval() is to implement a stream wrapper, then
      using include/require on a custom URL. What I would do is save the
      generated content to a temporary file during debug so that you can
      more easily see where an error occur.
      >
      See http://www.php.net/manual/en/ref.stream.php
      >
      Ok, I'm not sure if I understand what you mean. Are you saying that I should
      read one php statement at a time and evaluate the php statements?

      That is, I might read the file into an object and then parse it one
      statement at a time and eval each statement. This way I could step through
      the code... not necessarily the best way but does let me debug my code.


      The problem is, that in my code I do something like

      eval('?>'.AddNa vToPage($MainPa ge));

      Where $MainPage is just an html file that acts as a template(I probably
      could have included it aftwards instead of the way I did it but I think its
      probably to late to recode it at this point).

      So when I debug the code I get to this line and then cannot debug whats
      inside.

      What is inside is what AddNavToPage does, which is inserts a php/html file
      $mainpage into the template page to generate the total page.

      Essentially AddNavToPage returns a mixed php/html that was stiched together
      by 2 other pages.

      I suppose I could save it to a temp file like you mentioned and then include
      it and it should work?

      that is, instead of the eval line I could do

      if ($DEBUG)
      {
      file_put_conten ts($temp, AddNavToPage($M ainPage)));
      include $temp;
      }

      (psuedo code)

      I'll try that and see what happens,

      Thanks,
      Jon


      Comment

      • Jon Slaughter

        #4
        Re: eval, include and output buffering

        >
        if ($DEBUG)
        {
        file_put_conten ts($temp, AddNavToPage($M ainPage)));
        include $temp;
        }
        >
        (psuedo code)
        >
        Ok, it works. Shows me all the html and stuff but I guess its better than
        nothing.

        Thanks,
        Jon


        Comment

        • Chung Leong

          #5
          Re: eval, include and output buffering

          On May 10, 7:00 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
          "Chung Leong" <chernyshev...@ hotmail.comwrot e in message
          >
          news:1178812844 .248954.101410@ y5g2000hsa.goog legroups.com...
          >
          >
          >
          >
          >
          On May 10, 4:29 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
          I'm using eval to excute some mixed php and html code but I cannot debug
          it.
          I am essentially using filegetcontents to load up a php/html file and
          then
          inserting it into another php/html file and then using eval to execute
          the
          final product.
          >
          If I were to use include and output buffering instead of filegetcontents
          would it allow be to debug the code? (I have to capture the include so it
          can be modified which is why I used filegetcontents and eval in the first
          place).
          >
          essentially instead of something like eval(mod(filege tcontents()))
          >
          I would have
          >
          ob_start();
          include $filename;
          $contents = mod(ob_get_cont ents());
          ob_end_clean();
          >
          As far as I can remember mod only modifies html code but I can't be
          completely sure. In any case I'm not sure how the include eval the code
          when
          its buffered as if its just the output or what? Right now everythign is
          working fine and I don't want to screw it up but I'm kinda dragging my
          feet
          because of the debugging issues.
          >
          Thanks,
          Jon
          >
          An alternative to using eval() is to implement a stream wrapper, then
          using include/require on a custom URL. What I would do is save the
          generated content to a temporary file during debug so that you can
          more easily see where an error occur.
          >
          Seehttp://www.php.net/manual/en/ref.stream.php
          >
          Ok, I'm not sure if I understand what you mean. Are you saying that I should
          read one php statement at a time and evaluate the php statements?
          >
          That is, I might read the file into an object and then parse it one
          statement at a time and eval each statement. This way I could step through
          the code... not necessarily the best way but does let me debug my code.
          >
          The problem is, that in my code I do something like
          >
          eval('?>'.AddNa vToPage($MainPa ge));
          >
          Where $MainPage is just an html file that acts as a template(I probably
          could have included it aftwards instead of the way I did it but I think its
          probably to late to recode it at this point).
          >
          So when I debug the code I get to this line and then cannot debug whats
          inside.
          >
          What is inside is what AddNavToPage does, which is inserts a php/html file
          $mainpage into the template page to generate the total page.
          >
          Essentially AddNavToPage returns a mixed php/html that was stiched together
          by 2 other pages.
          >
          I suppose I could save it to a temp file like you mentioned and then include
          it and it should work?
          >
          that is, instead of the eval line I could do
          >
          if ($DEBUG)
          {
          file_put_conten ts($temp, AddNavToPage($M ainPage)));
          include $temp;
          >
          }
          >
          (psuedo code)
          >
          I'll try that and see what happens,
          >
          Thanks,
          Jon- Hide quoted text -
          >
          - Show quoted text -
          No, what I mean is you define a stream wrapper, so you can access the
          code generated by your procedure as though it's a file. The wrapper
          would look something like this:

          class AddNavStream {
          var $data;
          var $position;

          function stream_open($pa th, $mode, $options, &$opened_pat h) {
          // extract necessary info from $path
          $url = parse_url($path );

          // get the contents of the file
          $file1 = file_get_conten ts( ... );
          $file2 = file_get_conten ts( ... );

          // do something here to generate the code
          // to be executed...

          if(defined('DEB UG')) {
          // save contents to temp file
          }

          // contents of the pseudo file
          $this->data = $code;
          $this->position = 0;

          return true;
          }

          function stream_read($co unt)
          {
          $ret = substr($this->data, $this->position, $count);
          $this->position += strlen($ret);
          return $ret;
          }

          // other handlers
          }


          You then call stream_wrapper_ register() to register the wrapper:

          stream_wrapper_ register("addna v", "AddNavStre am ");

          After doing that you can then do an include like this:

          include("addnav ://nav.php/contents.php");

          The wrapper handlers in the AddNavStream class will be called to
          handle the request, with stream_open() being the first. The effect of
          this will be like saving the code in a file first, then including it.

          Comment

          Working...