Single PHP file - *.php = process, *.phps = source.

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

    #1

    Single PHP file - *.php = process, *.phps = source.

    Hi All,

    Hoping for some direction here (I have searched, without avail, for a
    solution already).

    I was toying with trying to find a way whereby I could have a single
    PHP file, and then specify whether to either see the processed output
    or the source code by using the extensions php and phps (respectively).

    So, with one file called "theFile.ph p" on the server, going to
    "www.server .com/theFile.php" would show the end result of the php
    actions, whereas going to "www.server .com/theFile.phps" would show the
    actual content of the file itself.

    I thought this could be done by modifying the htaccess file as follows:

    AddType application/x-httpd-php-source phps

    RewriteEngine on
    RewriteCond %{REQUEST_FILEN AME} !-f
    RewriteCond %{REQUEST_FILEN AME} !-d
    RewriteRule ^(.+)\.phps$ $1.php

    However this does not work:
    - if a *.php file exists, then accessing *.php or *.phps returns the
    end result of the php actions.
    - if a *.phps file exists, then accessing *.php returns a 404 error,
    and *.phps shows the actual file contents.

    Looking for any direction, references, or assistance.

    Thanks

  • Rik

    #2
    Re: Single PHP file - *.php = process, *.phps = source.

    On Fri, 26 Jan 2007 02:04:18 +0100, Lucanos <lucanos@gmail. comwrote:
    Hi All,
    >
    Hoping for some direction here (I have searched, without avail, for a
    solution already).
    >
    I was toying with trying to find a way whereby I could have a single
    PHP file, and then specify whether to either see the processed output
    or the source code by using the extensions php and phps (respectively).
    >
    So, with one file called "theFile.ph p" on the server, going to
    "www.server .com/theFile.php" would show the end result of the php
    actions, whereas going to "www.server .com/theFile.phps" would show the
    actual content of the file itself.
    >
    I thought this could be done by modifying the htaccess file as follows:
    >
    AddType application/x-httpd-php-source phps
    >
    RewriteEngine on
    RewriteCond %{REQUEST_FILEN AME} !-f
    RewriteCond %{REQUEST_FILEN AME} !-d
    RewriteRule ^(.+)\.phps$ $1.php
    >
    However this does not work:
    - if a *.php file exists, then accessing *.php or *.phps returns the
    end result of the php actions.
    - if a *.phps file exists, then accessing *.php returns a 404 error,
    and *.phps shows the actual file contents.
    >
    Looking for any direction, references, or assistance.
    First of all, let's make it clear that what you want is a major, major
    security risk... Add very rigid security fot the ones allowed to see the
    code, both in authenticating and in what directories they are allowed to
    see.


    Now, for your solution:

    Make 2 files:
    ----highlighter.php-----
    highlight_file( $_GET['file']);
    ------------------------

    ---.htaccess:-----------
    RewriteCond $1.php -f
    RewriteCond $1.phps !-f
    RewriteRule ^(.*)\.phps$ highlighter.php ?file=$1
    ------------------------

    VoilĂ .
    --
    Rik Wasmus

    Comment

    • Toby Inkster

      #3
      Re: Single PHP file - *.php = process, *.phps = source.

      Lucanos wrote:
      I was toying with trying to find a way whereby I could have a single
      PHP file, and then specify whether to either see the processed output
      or the source code by using the extensions php and phps (respectively).
      As Rik said, for production code this can cause security problems. Of
      course, properly secured code doesn't rely on security-through-obscurity,
      but still, obscurity helps sometimes.

      That said, I often use it as a technique when posting example code for
      people.

      Method one: symbolic links. Create the file mycode.php, then create a
      symbolic link to it using the following command and the command line:

      ln -s mycode.php mycode.phps

      This effectively creates two copies of the file, one called "mycode.php "
      and one called "mycode.php s", but any updates to the PHP file will also
      show up in the PHPS file.

      Method two: PHP. Add the following code to the top of each PHP file:

      <?php
      if ($_GET['source'])
      {
      highlight_file( $_SERVER['SCRIPT_FILENAM E']);
      exit();
      }
      ?>

      You can now add "?source=1" to a URL to show its source.

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • Dikkie Dik

        #4
        Re: Single PHP file - *.php = process, *.phps = source.

        I was toying with trying to find a way whereby I could have a single
        PHP file, and then specify whether to either see the processed output
        or the source code by using the extensions php and phps (respectively).
        >
        So, with one file called "theFile.ph p" on the server, going to
        "www.server .com/theFile.php" would show the end result of the php
        actions, whereas going to "www.server .com/theFile.phps" would show the
        actual content of the file itself.

        I would do this in another way: create a PHP script that takes a
        filename as a parameter, checks this to see if it is a file that is
        allowed to be seen, and sends the contents of the file as plaintext to
        the browser.

        You would call it with something like


        Of course, you can instruct apache to turn that into any other URL with
        rewriting.

        Best regards

        Comment

        Working...