How can I avoid showing “#!/usr/bin/php” on PHP?












9














I want PHP scripts to run both on command line and website (I use Apache and Nginx) so I put #!/usr/bin/php in the first line of my scripts but that appears on the website...










share|improve this question
























  • If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
    – Qwerty
    Nov 5 '10 at 11:08
















9














I want PHP scripts to run both on command line and website (I use Apache and Nginx) so I put #!/usr/bin/php in the first line of my scripts but that appears on the website...










share|improve this question
























  • If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
    – Qwerty
    Nov 5 '10 at 11:08














9












9








9


2





I want PHP scripts to run both on command line and website (I use Apache and Nginx) so I put #!/usr/bin/php in the first line of my scripts but that appears on the website...










share|improve this question















I want PHP scripts to run both on command line and website (I use Apache and Nginx) so I put #!/usr/bin/php in the first line of my scripts but that appears on the website...







php apache shell nginx command-line-interface






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 6 '16 at 22:15









Viliam Simko

1,0461026




1,0461026










asked Nov 5 '10 at 11:03









brainsqueezer

61126




61126












  • If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
    – Qwerty
    Nov 5 '10 at 11:08


















  • If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
    – Qwerty
    Nov 5 '10 at 11:08
















If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
– Qwerty
Nov 5 '10 at 11:08




If you already use apache for serving PHP, just configure nginx as reverse proxy for apache.
– Qwerty
Nov 5 '10 at 11:08












6 Answers
6






active

oldest

votes


















18














I solved the problem using output buffering.
My script now looks like this:



#!/usr/bin/php
<?php
@ob_end_clean();
...


Note: There is no ?> at the end of the file. This is actually a good practice when writing PHP scripts. This prevents any garbage text to be accidentally printed.



Note: The PHP documentation for ob_end_clean() says that:




The output buffer must be started by ob_start() with
PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags.
Otherwise ob_end_clean() will not work.




It seems that this is done automatically when PHP runs from command line.






share|improve this answer



















  • 1




    Very nice solution! Works, thanks!!!
    – CDR
    Oct 31 '12 at 9:23






  • 2




    Do you need an ob_start(); as well? Probably worth mentioning.
    – Floris
    Apr 3 '14 at 15:32










  • I think you can mark this answer as accepted (already 5+ years) :-)
    – Viliam Simko
    Apr 29 '16 at 13:23










  • This is beautiful.
    – Cryptopat
    Feb 7 '18 at 18:35










  • Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
    – Sz.
    Nov 12 '18 at 23:59





















3














There is no need to have #!/usr/bin/php in your code, just run CLI script using php, for example php /path/to/file.php or /usr/bin/php /path/to/file.php.






share|improve this answer





















  • But if you want to create an executable script that can run from shell, you need to solve this issue.
    – Viliam Simko
    Nov 6 '14 at 16:19



















2














I generally find it a good idea to separate logic from presentation. When I do something like this, I put as much as possible in a library, and then write separate cli and web interfaces for it.



That said, calling it with the php command is probably an easier fix.






share|improve this answer

















  • 1




    +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
    – ircmaxell
    Nov 5 '10 at 12:24



















1














@ViliamSimko's wicked trick is almost there, but, unfortunately, flawed. (It did actually break my header-sending sequence, for instance, despite not polluting the output with the shebang.)



TL;DR, here's the fix*:



#!/usr/bin/php
<?php @ob_end_clean(); if(ini_get('output_buffering')) ob_start();
...


or, less obscene-looking, but still just an euphemism for the same offense ;) :



#!/usr/bin/php
<?php if (ob_get_level()) { ob_end_clean(); ob_start(); }
...


(And see also the "UPDATE" part below for perhaps the maximum we can do about this...)



Explanation:



@Floris had a very good point in the comments there:




Do you need an ob_start(); as well? Probably worth mentioning.




You sure do. But where? And when?



Cases to consider:




  1. As Viliam said (and just confirmed it with PHP 7.2), the shebang is fortunately eaten by the php command itself, when you run your script with php yourscript.php, so the entire trick is redundant.


  2. In web mode, it's actually config-dependent: if output_buffering is on in the config (and sure enough, it's usually on, but it's not even the default), an ob_start has already been done implicitly at the start of the script (you can check it with ob_get_level()). So, we can't just abruptly cancel it with an ob_end_clean and call it a day: we need to start another one, to keep the levels balanced!



  3. If output_buffering is off in the config, then, sad face, we are out of luck: ob_get_clean() does nothing, and the shebang will end up in the top of the page.



    Note: there is no fix for this one, other than turning it on.




  4. In command-line mode, the manual says about output_buffering:




    This directive is always Off in PHP-CLI.




    But, instead of failing the same hopeless way as in 3., the implicit shebang cleanup (see 1.) saves the day.






* "Fix" in the sense that this audacious hack will work in a lot more cases. If you are in full control of your PHP env., it can be just fine (as is in my case). Otherwise, it can still break in lots of subtle ways unexpectedly (consider auto-prepended code, custom extensions, or other possible ways to control output buffering etc.). Also, for example, when included from other scripts in CLI mode (where there's no buffering), you are still out of luck: the shebang will show up in the output, no matter what (unless, of course, filtered out manually by the caller). Not only that, but it would also break up your own buffering, if you happened to have any, while including such a naughtified script.





UPDATE: Just for the fun of it, here's an "almost correct" version, which plays along nicely with an ongoing buffering, be it implicit or user-level:



#!/usr/bin/php
<?php
if (ob_get_level()) {
$buf = ob_get_clean();
ob_start();
// Refill the buffer, but without the shebang line:
echo substr($buf, 0, strpos($buf, file(__FILE__)[0]));
} // else { out of luck... }


Still only "almost" correct, as nothing can fix output_buffering = 0 in web mode, and the "being included with no buffering" case can only be solved if the calling script adds an explicit ob_start - ob_end_... wrapping. Also, most of the caveats above still apply: various subtleties can still break it (e.g. the current output buffer must have the (fortunately default) PHP_OUTPUT_HANDLER_CLEANABLE flag etc.).)






share|improve this answer































    0














    Call the script using the php command






    share|improve this answer





















    • How can be done? You mean for Nginx? I use php-fpm on debian
      – brainsqueezer
      Nov 5 '10 at 11:08










    • It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
      – Viliam Simko
      Feb 14 '17 at 9:51



















    -1














    The output buffering solution above is a hack. Don't do that.



    First thing, you are actually better using the env command to determine which php is being used:



    #!/usr/bin/env php


    Then give it permission to be executed by itself:



    chmod +x myfile


    So instead of calling 'php myfile', you now just run:



    ./myfile


    From that folder. Hope this helps!






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4105278%2fhow-can-i-avoid-showing-usr-bin-php-on-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      18














      I solved the problem using output buffering.
      My script now looks like this:



      #!/usr/bin/php
      <?php
      @ob_end_clean();
      ...


      Note: There is no ?> at the end of the file. This is actually a good practice when writing PHP scripts. This prevents any garbage text to be accidentally printed.



      Note: The PHP documentation for ob_end_clean() says that:




      The output buffer must be started by ob_start() with
      PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags.
      Otherwise ob_end_clean() will not work.




      It seems that this is done automatically when PHP runs from command line.






      share|improve this answer



















      • 1




        Very nice solution! Works, thanks!!!
        – CDR
        Oct 31 '12 at 9:23






      • 2




        Do you need an ob_start(); as well? Probably worth mentioning.
        – Floris
        Apr 3 '14 at 15:32










      • I think you can mark this answer as accepted (already 5+ years) :-)
        – Viliam Simko
        Apr 29 '16 at 13:23










      • This is beautiful.
        – Cryptopat
        Feb 7 '18 at 18:35










      • Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
        – Sz.
        Nov 12 '18 at 23:59


















      18














      I solved the problem using output buffering.
      My script now looks like this:



      #!/usr/bin/php
      <?php
      @ob_end_clean();
      ...


      Note: There is no ?> at the end of the file. This is actually a good practice when writing PHP scripts. This prevents any garbage text to be accidentally printed.



      Note: The PHP documentation for ob_end_clean() says that:




      The output buffer must be started by ob_start() with
      PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags.
      Otherwise ob_end_clean() will not work.




      It seems that this is done automatically when PHP runs from command line.






      share|improve this answer



















      • 1




        Very nice solution! Works, thanks!!!
        – CDR
        Oct 31 '12 at 9:23






      • 2




        Do you need an ob_start(); as well? Probably worth mentioning.
        – Floris
        Apr 3 '14 at 15:32










      • I think you can mark this answer as accepted (already 5+ years) :-)
        – Viliam Simko
        Apr 29 '16 at 13:23










      • This is beautiful.
        – Cryptopat
        Feb 7 '18 at 18:35










      • Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
        – Sz.
        Nov 12 '18 at 23:59
















      18












      18








      18






      I solved the problem using output buffering.
      My script now looks like this:



      #!/usr/bin/php
      <?php
      @ob_end_clean();
      ...


      Note: There is no ?> at the end of the file. This is actually a good practice when writing PHP scripts. This prevents any garbage text to be accidentally printed.



      Note: The PHP documentation for ob_end_clean() says that:




      The output buffer must be started by ob_start() with
      PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags.
      Otherwise ob_end_clean() will not work.




      It seems that this is done automatically when PHP runs from command line.






      share|improve this answer














      I solved the problem using output buffering.
      My script now looks like this:



      #!/usr/bin/php
      <?php
      @ob_end_clean();
      ...


      Note: There is no ?> at the end of the file. This is actually a good practice when writing PHP scripts. This prevents any garbage text to be accidentally printed.



      Note: The PHP documentation for ob_end_clean() says that:




      The output buffer must be started by ob_start() with
      PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags.
      Otherwise ob_end_clean() will not work.




      It seems that this is done automatically when PHP runs from command line.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 27 '16 at 15:37

























      answered Jul 21 '11 at 7:47









      Viliam Simko

      1,0461026




      1,0461026








      • 1




        Very nice solution! Works, thanks!!!
        – CDR
        Oct 31 '12 at 9:23






      • 2




        Do you need an ob_start(); as well? Probably worth mentioning.
        – Floris
        Apr 3 '14 at 15:32










      • I think you can mark this answer as accepted (already 5+ years) :-)
        – Viliam Simko
        Apr 29 '16 at 13:23










      • This is beautiful.
        – Cryptopat
        Feb 7 '18 at 18:35










      • Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
        – Sz.
        Nov 12 '18 at 23:59
















      • 1




        Very nice solution! Works, thanks!!!
        – CDR
        Oct 31 '12 at 9:23






      • 2




        Do you need an ob_start(); as well? Probably worth mentioning.
        – Floris
        Apr 3 '14 at 15:32










      • I think you can mark this answer as accepted (already 5+ years) :-)
        – Viliam Simko
        Apr 29 '16 at 13:23










      • This is beautiful.
        – Cryptopat
        Feb 7 '18 at 18:35










      • Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
        – Sz.
        Nov 12 '18 at 23:59










      1




      1




      Very nice solution! Works, thanks!!!
      – CDR
      Oct 31 '12 at 9:23




      Very nice solution! Works, thanks!!!
      – CDR
      Oct 31 '12 at 9:23




      2




      2




      Do you need an ob_start(); as well? Probably worth mentioning.
      – Floris
      Apr 3 '14 at 15:32




      Do you need an ob_start(); as well? Probably worth mentioning.
      – Floris
      Apr 3 '14 at 15:32












      I think you can mark this answer as accepted (already 5+ years) :-)
      – Viliam Simko
      Apr 29 '16 at 13:23




      I think you can mark this answer as accepted (already 5+ years) :-)
      – Viliam Simko
      Apr 29 '16 at 13:23












      This is beautiful.
      – Cryptopat
      Feb 7 '18 at 18:35




      This is beautiful.
      – Cryptopat
      Feb 7 '18 at 18:35












      Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
      – Sz.
      Nov 12 '18 at 23:59






      Nice idea, but @Floris had a crucial point here. Please see my answer, where I've addressed that issue.
      – Sz.
      Nov 12 '18 at 23:59















      3














      There is no need to have #!/usr/bin/php in your code, just run CLI script using php, for example php /path/to/file.php or /usr/bin/php /path/to/file.php.






      share|improve this answer





















      • But if you want to create an executable script that can run from shell, you need to solve this issue.
        – Viliam Simko
        Nov 6 '14 at 16:19
















      3














      There is no need to have #!/usr/bin/php in your code, just run CLI script using php, for example php /path/to/file.php or /usr/bin/php /path/to/file.php.






      share|improve this answer





















      • But if you want to create an executable script that can run from shell, you need to solve this issue.
        – Viliam Simko
        Nov 6 '14 at 16:19














      3












      3








      3






      There is no need to have #!/usr/bin/php in your code, just run CLI script using php, for example php /path/to/file.php or /usr/bin/php /path/to/file.php.






      share|improve this answer












      There is no need to have #!/usr/bin/php in your code, just run CLI script using php, for example php /path/to/file.php or /usr/bin/php /path/to/file.php.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 5 '10 at 11:08









      David Kuridža

      4,89752125




      4,89752125












      • But if you want to create an executable script that can run from shell, you need to solve this issue.
        – Viliam Simko
        Nov 6 '14 at 16:19


















      • But if you want to create an executable script that can run from shell, you need to solve this issue.
        – Viliam Simko
        Nov 6 '14 at 16:19
















      But if you want to create an executable script that can run from shell, you need to solve this issue.
      – Viliam Simko
      Nov 6 '14 at 16:19




      But if you want to create an executable script that can run from shell, you need to solve this issue.
      – Viliam Simko
      Nov 6 '14 at 16:19











      2














      I generally find it a good idea to separate logic from presentation. When I do something like this, I put as much as possible in a library, and then write separate cli and web interfaces for it.



      That said, calling it with the php command is probably an easier fix.






      share|improve this answer

















      • 1




        +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
        – ircmaxell
        Nov 5 '10 at 12:24
















      2














      I generally find it a good idea to separate logic from presentation. When I do something like this, I put as much as possible in a library, and then write separate cli and web interfaces for it.



      That said, calling it with the php command is probably an easier fix.






      share|improve this answer

















      • 1




        +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
        – ircmaxell
        Nov 5 '10 at 12:24














      2












      2








      2






      I generally find it a good idea to separate logic from presentation. When I do something like this, I put as much as possible in a library, and then write separate cli and web interfaces for it.



      That said, calling it with the php command is probably an easier fix.






      share|improve this answer












      I generally find it a good idea to separate logic from presentation. When I do something like this, I put as much as possible in a library, and then write separate cli and web interfaces for it.



      That said, calling it with the php command is probably an easier fix.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 5 '10 at 11:23









      Chris

      1,20586




      1,20586








      • 1




        +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
        – ircmaxell
        Nov 5 '10 at 12:24














      • 1




        +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
        – ircmaxell
        Nov 5 '10 at 12:24








      1




      1




      +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
      – ircmaxell
      Nov 5 '10 at 12:24




      +1 for separation of logic. Typically CLI and web will require separate interfaces (interacting with $argv instead of $_GET for example). So there's no real need for the same file to be called from both.
      – ircmaxell
      Nov 5 '10 at 12:24











      1














      @ViliamSimko's wicked trick is almost there, but, unfortunately, flawed. (It did actually break my header-sending sequence, for instance, despite not polluting the output with the shebang.)



      TL;DR, here's the fix*:



      #!/usr/bin/php
      <?php @ob_end_clean(); if(ini_get('output_buffering')) ob_start();
      ...


      or, less obscene-looking, but still just an euphemism for the same offense ;) :



      #!/usr/bin/php
      <?php if (ob_get_level()) { ob_end_clean(); ob_start(); }
      ...


      (And see also the "UPDATE" part below for perhaps the maximum we can do about this...)



      Explanation:



      @Floris had a very good point in the comments there:




      Do you need an ob_start(); as well? Probably worth mentioning.




      You sure do. But where? And when?



      Cases to consider:




      1. As Viliam said (and just confirmed it with PHP 7.2), the shebang is fortunately eaten by the php command itself, when you run your script with php yourscript.php, so the entire trick is redundant.


      2. In web mode, it's actually config-dependent: if output_buffering is on in the config (and sure enough, it's usually on, but it's not even the default), an ob_start has already been done implicitly at the start of the script (you can check it with ob_get_level()). So, we can't just abruptly cancel it with an ob_end_clean and call it a day: we need to start another one, to keep the levels balanced!



      3. If output_buffering is off in the config, then, sad face, we are out of luck: ob_get_clean() does nothing, and the shebang will end up in the top of the page.



        Note: there is no fix for this one, other than turning it on.




      4. In command-line mode, the manual says about output_buffering:




        This directive is always Off in PHP-CLI.




        But, instead of failing the same hopeless way as in 3., the implicit shebang cleanup (see 1.) saves the day.






      * "Fix" in the sense that this audacious hack will work in a lot more cases. If you are in full control of your PHP env., it can be just fine (as is in my case). Otherwise, it can still break in lots of subtle ways unexpectedly (consider auto-prepended code, custom extensions, or other possible ways to control output buffering etc.). Also, for example, when included from other scripts in CLI mode (where there's no buffering), you are still out of luck: the shebang will show up in the output, no matter what (unless, of course, filtered out manually by the caller). Not only that, but it would also break up your own buffering, if you happened to have any, while including such a naughtified script.





      UPDATE: Just for the fun of it, here's an "almost correct" version, which plays along nicely with an ongoing buffering, be it implicit or user-level:



      #!/usr/bin/php
      <?php
      if (ob_get_level()) {
      $buf = ob_get_clean();
      ob_start();
      // Refill the buffer, but without the shebang line:
      echo substr($buf, 0, strpos($buf, file(__FILE__)[0]));
      } // else { out of luck... }


      Still only "almost" correct, as nothing can fix output_buffering = 0 in web mode, and the "being included with no buffering" case can only be solved if the calling script adds an explicit ob_start - ob_end_... wrapping. Also, most of the caveats above still apply: various subtleties can still break it (e.g. the current output buffer must have the (fortunately default) PHP_OUTPUT_HANDLER_CLEANABLE flag etc.).)






      share|improve this answer




























        1














        @ViliamSimko's wicked trick is almost there, but, unfortunately, flawed. (It did actually break my header-sending sequence, for instance, despite not polluting the output with the shebang.)



        TL;DR, here's the fix*:



        #!/usr/bin/php
        <?php @ob_end_clean(); if(ini_get('output_buffering')) ob_start();
        ...


        or, less obscene-looking, but still just an euphemism for the same offense ;) :



        #!/usr/bin/php
        <?php if (ob_get_level()) { ob_end_clean(); ob_start(); }
        ...


        (And see also the "UPDATE" part below for perhaps the maximum we can do about this...)



        Explanation:



        @Floris had a very good point in the comments there:




        Do you need an ob_start(); as well? Probably worth mentioning.




        You sure do. But where? And when?



        Cases to consider:




        1. As Viliam said (and just confirmed it with PHP 7.2), the shebang is fortunately eaten by the php command itself, when you run your script with php yourscript.php, so the entire trick is redundant.


        2. In web mode, it's actually config-dependent: if output_buffering is on in the config (and sure enough, it's usually on, but it's not even the default), an ob_start has already been done implicitly at the start of the script (you can check it with ob_get_level()). So, we can't just abruptly cancel it with an ob_end_clean and call it a day: we need to start another one, to keep the levels balanced!



        3. If output_buffering is off in the config, then, sad face, we are out of luck: ob_get_clean() does nothing, and the shebang will end up in the top of the page.



          Note: there is no fix for this one, other than turning it on.




        4. In command-line mode, the manual says about output_buffering:




          This directive is always Off in PHP-CLI.




          But, instead of failing the same hopeless way as in 3., the implicit shebang cleanup (see 1.) saves the day.






        * "Fix" in the sense that this audacious hack will work in a lot more cases. If you are in full control of your PHP env., it can be just fine (as is in my case). Otherwise, it can still break in lots of subtle ways unexpectedly (consider auto-prepended code, custom extensions, or other possible ways to control output buffering etc.). Also, for example, when included from other scripts in CLI mode (where there's no buffering), you are still out of luck: the shebang will show up in the output, no matter what (unless, of course, filtered out manually by the caller). Not only that, but it would also break up your own buffering, if you happened to have any, while including such a naughtified script.





        UPDATE: Just for the fun of it, here's an "almost correct" version, which plays along nicely with an ongoing buffering, be it implicit or user-level:



        #!/usr/bin/php
        <?php
        if (ob_get_level()) {
        $buf = ob_get_clean();
        ob_start();
        // Refill the buffer, but without the shebang line:
        echo substr($buf, 0, strpos($buf, file(__FILE__)[0]));
        } // else { out of luck... }


        Still only "almost" correct, as nothing can fix output_buffering = 0 in web mode, and the "being included with no buffering" case can only be solved if the calling script adds an explicit ob_start - ob_end_... wrapping. Also, most of the caveats above still apply: various subtleties can still break it (e.g. the current output buffer must have the (fortunately default) PHP_OUTPUT_HANDLER_CLEANABLE flag etc.).)






        share|improve this answer


























          1












          1








          1






          @ViliamSimko's wicked trick is almost there, but, unfortunately, flawed. (It did actually break my header-sending sequence, for instance, despite not polluting the output with the shebang.)



          TL;DR, here's the fix*:



          #!/usr/bin/php
          <?php @ob_end_clean(); if(ini_get('output_buffering')) ob_start();
          ...


          or, less obscene-looking, but still just an euphemism for the same offense ;) :



          #!/usr/bin/php
          <?php if (ob_get_level()) { ob_end_clean(); ob_start(); }
          ...


          (And see also the "UPDATE" part below for perhaps the maximum we can do about this...)



          Explanation:



          @Floris had a very good point in the comments there:




          Do you need an ob_start(); as well? Probably worth mentioning.




          You sure do. But where? And when?



          Cases to consider:




          1. As Viliam said (and just confirmed it with PHP 7.2), the shebang is fortunately eaten by the php command itself, when you run your script with php yourscript.php, so the entire trick is redundant.


          2. In web mode, it's actually config-dependent: if output_buffering is on in the config (and sure enough, it's usually on, but it's not even the default), an ob_start has already been done implicitly at the start of the script (you can check it with ob_get_level()). So, we can't just abruptly cancel it with an ob_end_clean and call it a day: we need to start another one, to keep the levels balanced!



          3. If output_buffering is off in the config, then, sad face, we are out of luck: ob_get_clean() does nothing, and the shebang will end up in the top of the page.



            Note: there is no fix for this one, other than turning it on.




          4. In command-line mode, the manual says about output_buffering:




            This directive is always Off in PHP-CLI.




            But, instead of failing the same hopeless way as in 3., the implicit shebang cleanup (see 1.) saves the day.






          * "Fix" in the sense that this audacious hack will work in a lot more cases. If you are in full control of your PHP env., it can be just fine (as is in my case). Otherwise, it can still break in lots of subtle ways unexpectedly (consider auto-prepended code, custom extensions, or other possible ways to control output buffering etc.). Also, for example, when included from other scripts in CLI mode (where there's no buffering), you are still out of luck: the shebang will show up in the output, no matter what (unless, of course, filtered out manually by the caller). Not only that, but it would also break up your own buffering, if you happened to have any, while including such a naughtified script.





          UPDATE: Just for the fun of it, here's an "almost correct" version, which plays along nicely with an ongoing buffering, be it implicit or user-level:



          #!/usr/bin/php
          <?php
          if (ob_get_level()) {
          $buf = ob_get_clean();
          ob_start();
          // Refill the buffer, but without the shebang line:
          echo substr($buf, 0, strpos($buf, file(__FILE__)[0]));
          } // else { out of luck... }


          Still only "almost" correct, as nothing can fix output_buffering = 0 in web mode, and the "being included with no buffering" case can only be solved if the calling script adds an explicit ob_start - ob_end_... wrapping. Also, most of the caveats above still apply: various subtleties can still break it (e.g. the current output buffer must have the (fortunately default) PHP_OUTPUT_HANDLER_CLEANABLE flag etc.).)






          share|improve this answer














          @ViliamSimko's wicked trick is almost there, but, unfortunately, flawed. (It did actually break my header-sending sequence, for instance, despite not polluting the output with the shebang.)



          TL;DR, here's the fix*:



          #!/usr/bin/php
          <?php @ob_end_clean(); if(ini_get('output_buffering')) ob_start();
          ...


          or, less obscene-looking, but still just an euphemism for the same offense ;) :



          #!/usr/bin/php
          <?php if (ob_get_level()) { ob_end_clean(); ob_start(); }
          ...


          (And see also the "UPDATE" part below for perhaps the maximum we can do about this...)



          Explanation:



          @Floris had a very good point in the comments there:




          Do you need an ob_start(); as well? Probably worth mentioning.




          You sure do. But where? And when?



          Cases to consider:




          1. As Viliam said (and just confirmed it with PHP 7.2), the shebang is fortunately eaten by the php command itself, when you run your script with php yourscript.php, so the entire trick is redundant.


          2. In web mode, it's actually config-dependent: if output_buffering is on in the config (and sure enough, it's usually on, but it's not even the default), an ob_start has already been done implicitly at the start of the script (you can check it with ob_get_level()). So, we can't just abruptly cancel it with an ob_end_clean and call it a day: we need to start another one, to keep the levels balanced!



          3. If output_buffering is off in the config, then, sad face, we are out of luck: ob_get_clean() does nothing, and the shebang will end up in the top of the page.



            Note: there is no fix for this one, other than turning it on.




          4. In command-line mode, the manual says about output_buffering:




            This directive is always Off in PHP-CLI.




            But, instead of failing the same hopeless way as in 3., the implicit shebang cleanup (see 1.) saves the day.






          * "Fix" in the sense that this audacious hack will work in a lot more cases. If you are in full control of your PHP env., it can be just fine (as is in my case). Otherwise, it can still break in lots of subtle ways unexpectedly (consider auto-prepended code, custom extensions, or other possible ways to control output buffering etc.). Also, for example, when included from other scripts in CLI mode (where there's no buffering), you are still out of luck: the shebang will show up in the output, no matter what (unless, of course, filtered out manually by the caller). Not only that, but it would also break up your own buffering, if you happened to have any, while including such a naughtified script.





          UPDATE: Just for the fun of it, here's an "almost correct" version, which plays along nicely with an ongoing buffering, be it implicit or user-level:



          #!/usr/bin/php
          <?php
          if (ob_get_level()) {
          $buf = ob_get_clean();
          ob_start();
          // Refill the buffer, but without the shebang line:
          echo substr($buf, 0, strpos($buf, file(__FILE__)[0]));
          } // else { out of luck... }


          Still only "almost" correct, as nothing can fix output_buffering = 0 in web mode, and the "being included with no buffering" case can only be solved if the calling script adds an explicit ob_start - ob_end_... wrapping. Also, most of the caveats above still apply: various subtleties can still break it (e.g. the current output buffer must have the (fortunately default) PHP_OUTPUT_HANDLER_CLEANABLE flag etc.).)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 18:53

























          answered Nov 12 '18 at 23:56









          Sz.

          1,2911824




          1,2911824























              0














              Call the script using the php command






              share|improve this answer





















              • How can be done? You mean for Nginx? I use php-fpm on debian
                – brainsqueezer
                Nov 5 '10 at 11:08










              • It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
                – Viliam Simko
                Feb 14 '17 at 9:51
















              0














              Call the script using the php command






              share|improve this answer





















              • How can be done? You mean for Nginx? I use php-fpm on debian
                – brainsqueezer
                Nov 5 '10 at 11:08










              • It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
                – Viliam Simko
                Feb 14 '17 at 9:51














              0












              0








              0






              Call the script using the php command






              share|improve this answer












              Call the script using the php command







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 5 '10 at 11:06









              Petah

              32.7k17126188




              32.7k17126188












              • How can be done? You mean for Nginx? I use php-fpm on debian
                – brainsqueezer
                Nov 5 '10 at 11:08










              • It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
                – Viliam Simko
                Feb 14 '17 at 9:51


















              • How can be done? You mean for Nginx? I use php-fpm on debian
                – brainsqueezer
                Nov 5 '10 at 11:08










              • It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
                – Viliam Simko
                Feb 14 '17 at 9:51
















              How can be done? You mean for Nginx? I use php-fpm on debian
              – brainsqueezer
              Nov 5 '10 at 11:08




              How can be done? You mean for Nginx? I use php-fpm on debian
              – brainsqueezer
              Nov 5 '10 at 11:08












              It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
              – Viliam Simko
              Feb 14 '17 at 9:51




              It's true that you can run the script using php command. However, if you want to make an executable script (chmod +x yourscript.php) you need to add the hashbang (#!) to the first line of your script.
              – Viliam Simko
              Feb 14 '17 at 9:51











              -1














              The output buffering solution above is a hack. Don't do that.



              First thing, you are actually better using the env command to determine which php is being used:



              #!/usr/bin/env php


              Then give it permission to be executed by itself:



              chmod +x myfile


              So instead of calling 'php myfile', you now just run:



              ./myfile


              From that folder. Hope this helps!






              share|improve this answer


























                -1














                The output buffering solution above is a hack. Don't do that.



                First thing, you are actually better using the env command to determine which php is being used:



                #!/usr/bin/env php


                Then give it permission to be executed by itself:



                chmod +x myfile


                So instead of calling 'php myfile', you now just run:



                ./myfile


                From that folder. Hope this helps!






                share|improve this answer
























                  -1












                  -1








                  -1






                  The output buffering solution above is a hack. Don't do that.



                  First thing, you are actually better using the env command to determine which php is being used:



                  #!/usr/bin/env php


                  Then give it permission to be executed by itself:



                  chmod +x myfile


                  So instead of calling 'php myfile', you now just run:



                  ./myfile


                  From that folder. Hope this helps!






                  share|improve this answer












                  The output buffering solution above is a hack. Don't do that.



                  First thing, you are actually better using the env command to determine which php is being used:



                  #!/usr/bin/env php


                  Then give it permission to be executed by itself:



                  chmod +x myfile


                  So instead of calling 'php myfile', you now just run:



                  ./myfile


                  From that folder. Hope this helps!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 12 '17 at 10:21









                  delboy1978uk

                  7,2591726




                  7,2591726






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4105278%2fhow-can-i-avoid-showing-usr-bin-php-on-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python