Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Does PHP replace HTML files?

  1. Jan 17, 2017 #1
    For example instead of uploading an index.html to a server, would I just use index.php instead?
    Basically I want to be able to use PHP code to make my page more interactive.
     
  2. jcsd
  3. Jan 17, 2017 #2
    Yes but you may need to configure your webserver to find that by default, especially if you have both index.html and index.php
     
  4. Jan 17, 2017 #3
    Would it work if I only had a index.php?
     
  5. Jan 17, 2017 #4
    Are you using apache webserver?
     
  6. Jan 17, 2017 #5

    DaveC426913

    User Avatar
    Gold Member

    As he said, if it's in the list of defaults, yes, otherwise no.

    Look for a .htaccess file.


    BTW, stackoverflow.com is to programming what PF is to science.
    Register there. Ask questions. You won't regret it.
     
  7. Jan 18, 2017 #6

    jtbell

    User Avatar

    Staff: Mentor

  8. Jan 18, 2017 #7

    QuantumQuest

    User Avatar
    Gold Member

    If your server is connected with a PHP engine (as is the case for WAMP you installed), then yes. When a request is made to the web server, the server "recognizes" that it is not HTML (from the file extension) and the page is "sent" to the PHP interpreter and this in turn returns just HTML, that will be sent back to client. This happens in very rough lines - you can read the details if you want, by googling it. So, you can generate HTML inside your index.php file in the usual manner
    PHP:
    echo "<HTML tag></HTML tag>
                ..........
               "
    ;
    along with your other PHP code

    or just use the closing PHP tag
    Code (Text):
    ?>
    , write pure HTML and open again PHP in order to write other PHP code you need and so on - effectively creating some interspersed HTML inside your PHP code.
     
  9. Jan 18, 2017 #8
    Wait, you are seriously misunderstanding how websites work. PHP will not make your page interactive, Javascript will.

    When you get a web request, Apache loads whatever you point it at. It can be a static HTML page, which it will simply return, or it can be an executable which it will run and return the output, or it can be PHP. The PHP is run just like an executable, exactly the same as if you had put
    Code (Text):
    php index.php
    In your command line. (Not exactly, but for these purposes, it's good enough.)

    Like if you told apache to run an executable, it listens to stdout, and then returns that. echo sends data to stdout.
     
  10. Jan 18, 2017 #9

    jtbell

    User Avatar

    Staff: Mentor

    ...on the server. The viewer's browser sees only the HTML/CSS/Javascript code that the PHP code outputs. I don't think there is any way for the browser to distinguish whether the code that it receives comes from a PHP script or not, assuming of course that the code is properly written so it doesn't simply send the browser the raw PHP code. (I'll happily stand corrected if that's not the case!)
     
  11. Jan 18, 2017 #10
    I'm pretty sure I'm misunderstanding how PHP works.
    As far as I know - A PHP file generates an HTML page. Then I wonder how do you generate another HTML file. Also wouldn't using JS be a problem since it makes your code public and others can copy it?
     
  12. Jan 18, 2017 #11

    QuantumQuest

    User Avatar
    Gold Member

    PHP is a server-side scripting language. This means that you can do various computations and calculations i.e. programming with it, connect to a database and store/retrieve information and do protocol operations. You also output HTML code, in the manner that I described in #7. You also include whatever other client code you have (like CSS and JavaScript) and then it is the job of client program engines to interpret all these(HTML, CSS, JavaScript). One very important thing is to distinguish in a clear way, what takes place on the server side and what on the client side (your browser or whatever client software "talks" to the server).

    If you write JavaScript code and send it as it is to the client, then yes it can be copied/stolen. That's were the obfuscation concept comes into play. Obfuscation is basically converting code to a form that is unreadable, so there's no point to be stolen. Take a look at this tool for instance. There are many such free tools on the net.
     
  13. Jan 18, 2017 #12

    DaveC426913

    User Avatar
    Gold Member

    You can do this - but why?

    There is (should be) nothing in your JavaScript or HTML that is sensitive. Code, like oxygen, should be free too everyone. Data is the currency.
     
  14. Jan 18, 2017 #13

    QuantumQuest

    User Avatar
    Gold Member

    Yes I agree. As a web developer I know that very well. I just gave the OP the way that he/she can do this should this be needed - there are many cases that it does but in general it is not something mandatory. It depends on a number of factors.
     
  15. Jan 18, 2017 #14

    DaveC426913

    User Avatar
    Gold Member

    It was directed at the OP, who said:
    If one is worrying about others copying one's HTML and JS, one is looking at web dev wrong.
    By its very nature, web code is served to the user.
     
  16. Jan 19, 2017 #15

    QuantumQuest

    User Avatar
    Gold Member

    While I don't disagree in general, this has finally become an ideal - unfortunately like many other things, the primary reason being that there is a whole new "generation" out there of copy-pasters or "the bad guys" in this case if you will, and as JavaScript coding is already on steroids for a fairly long time, there is real need to protect your code from prying eyes. The average user may not care about this but development world vastly does. It is the case that everyone can find virtually everything, regarding resources to learn and above all for free, but not everyone can create code diamonds and more importantly some people are not willing to take the pains to write their own code at all. Now, if you combine this with having a job in a (mainly big) development company, code becomes a real asset and there is no other way to protect it, than obfuscating it. The same rationale is applied to other programming languages - beyond JavaScript, as well. Now, if you ask me if this is morally correct, it is not in my opinion but clearly, some unmoral behaviors created this need. In some companies, obfuscation is a formal part of their policies and there's no way to bypass it. So, beyond data that is the real asset as you pointed out, code is an asset as well in many cases and needs protection.

    On the other extreme, code obfuscation is used some times to serve everything but innocent purposes, but this is an inevitable path that utilization of a thing creates beyond its primary purpose.
     
  17. Jan 19, 2017 #16

    DaveC426913

    User Avatar
    Gold Member

    Beyond the first decade of web dev, when we had a 20th century idea of proprietary software, I cannot recall the last time I heard of a web dev or a web dev shop think about their front-end dev as needing protection. It's now about deliberately making one's work as easy as possible to grab - any dev or dev shop worth their salt puts their work in GitHub or other repos with the full intention of people using it.

    Theft is just not how the front end world is. And fear of theft is .. archaic - backwards.
     
  18. Jan 20, 2017 #17
    The only people worrying about obfuscating JS code are those that are doing fishy things.
    Like those scam 'whose viewing your facebook profile' websites.
     
  19. Jan 20, 2017 #18
    yeah you can do easily set your server and in the directory placed the index.php and it's start working !
     
  20. Jan 20, 2017 #19

    QuantumQuest

    User Avatar
    Gold Member

    Now, to clarify things a bit, it is not like that. What do you mean by web dev and web dev shops? It is about open source code or not. Open source code is put as we all know on GitHub and that's great, but up to there. Proprietary software is still out there and is still developed. A company may have only open source code or may have proprietary code and open source code - take Oracle as an example, there is a real lot of others as well. Front end development is no different at all. Haven't you seen big sites with obfuscated JavaScript code here and there? If not then check it.

    That's just a bold statement. As I said in #15

     
  21. Jan 20, 2017 #20
    The problem with obfuscation is that it's either easy to undo or it will increase the amount of code that's executed, a lot of which is pure overhead.
    What most big sites use is minified code =/= obfuscated. Even in the development tools the code gets reformatted in that case.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted