#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    64
    Rep Power
    9

    Include a files Class or Data without displaying the page.


    Wanted to see if anyone has a solution for my problem off the top of their head.

    We have a set of pages for our system that are autogenerated by a program. (PHP Report Maker). These php pages contain both html and php. We have another section of the website where we post quick data based on the PHP Report Maker program. The solution we have in place right now is to copy the class out of the file. I would like to be able to some how include the file, or scrape the page instead of copying the class over so that changes are automatically handeled. I can't just use file_get_content because the site requires a log in. I am pretty sure it can be done with cURL, but the solution is likely to be over complicated for what we are doing and my supervisor won't accept the code. Another solution could be to use ob to caputre the output from the page and throw it away, but that isn't really a very eloquent solution and again is likely to be rejected by my supervision.

    Is there a simple obvious solution I am missing?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    I think your explanation is a bit confusing as it relates to the title. You use 'include' or 'require' for PHP code and nothing is output until you have PHP output it. On the other hand 'file_get_contents' is used for reading HTML (as well as other non-PHP things) with PHP. In your case, if I understand, you don't have to worry about non-log users since you can locate the HTML and included PHP files in a private directory. The public PHP code is what would handle authentication then load the include files and output what ever HTML you want from the private directory or directories. The user running HTTPD would be the only one that needs read access to it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0
    Please, don't get it twisted. You should be able to use : include() or require() to call your class, html or even php page on the fly without disclosing the page.

    Go on top of the page you want to call the class or html document and type:

    Include('pagename.html');
    Include('myclass.php');

    Comments on this post

    • gw1500se disagrees : Incorrect. It the pages are in a public directory a user can get at them one way or another.
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    There is no way to include another PHP file without executing all of the code in it. In other words, if you have a PHP file that contains a mixture of PHP functions, classes and in-line code you can't include the functions and classes without executing the in-line code too. Using output buffering and then just discarding the output is really the only way you could do this; of course, that assumes that the in-line code doesn't change the state of the data at all.

    Neither file_get_contents nor cURL can be used to include PHP functions or classes from another file.

    You could write a script that modifies the generated output and then run that afterwords to get just the code you want to keep from it.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    64
    Rep Power
    9
    Originally Posted by E-Oreo
    There is no way to include another PHP file without executing all of the code in it. In other words, if you have a PHP file that contains a mixture of PHP functions, classes and in-line code you can't include the functions and classes without executing the in-line code too. Using output buffering and then just discarding the output is really the only way you could do this; of course, that assumes that the in-line code doesn't change the state of the data at all.

    Neither file_get_contents nor cURL can be used to include PHP functions or classes from another file.

    You could write a script that modifies the generated output and then run that afterwords to get just the code you want to keep from it.
    Sorry I didn't completely make clear that I need either the class, or the generated output after variable are passed in. So include doesn't work since it executes the page, but without the proper post/get variable being set. I have a couple of ideas now, and I will post back for postarity if something presents itself as a clear answer.
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    You can set $_GET and $_POST manually before including the file.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    64
    Rep Power
    9
    Originally Posted by E-Oreo
    You can set $_GET and $_POST manually before including the file.
    Silly me, see that was the obvious answer I new was out there. Ultimately this isn't going to work either. After having put some thought into it. The variables are just as likely to change as the SQL.

    I think what I will do is suggest we write a puppet script that auto-generates the new class file from the PHPReportMaker file each time we puppet (which is how we distribute new content/changes anyway)

    Thanks for being my sounding board.

IMN logo majestic logo threadwatch logo seochat tools logo