How to open a PHP file in the browser?

Asked

Viewed 52,481 times

3

I’m trying to open files with the extension .php.

  • In Firefox, it asks to download.
  • In the IE it asks to carry out the download.
  • Already in the Chrome it displays the PHP code.

Before it wasn’t like this, it was like this for some time. How can I open PHP files in my browser?

  • 6

    You need to use a web server (even local) to serve the file. Access with file:///, the result is this.

  • 1

    @bfavaretto doesn’t seem to be the problem.

  • Well, so I accessed I believe, HTML runs smoothly, PHP does not run, I understand that to work completely need to be on a server, even if local, but at least it was to be displayed the document even if empty, instead of requesting download.. HTML files open like this: file:///C:/testes/meuarquivo.html Phps also: file:///C:/testes/meuarquivo.php

  • 1

    But PHP will never be interpreted if it does not run on a server. With file:///, whether the browser will show the source code or force a download is its decision (maybe it’s even configurable, but I’m not sure).

  • Firefox, Chrome, IE, none like to display my PHP document, alias, in Chrome it displays document code, which already sounds weird. I have a WAMP server, to display PHP files, and ask to download. But regardless of that, without or with server, I already accessed PHP in firefox to structure HTML without having these problems.

  • In sense... the Navigator just interprets the HTML tags and "draws" your file. php by definition is a file that requires a PHP interpreter regardless of whether it has HTML tags or not.....

  • 1

    Well, here PHP worked by accessing the wamp server address, not the location where the file is. Was accessing with file:/.... and now it’s going by localhost/, without requesting a download or anything like that. Even so, I get a mystery in my head.. I opened PHP before, 10 days ago, right in the browser, by the file address... and opened normally, PHP was not executed, but HTML was read and the page was displayed. That sounds strange to me!... I thank you all, and if anyone has anything to say about it, I’m curious.

  • About your last comment, it may have been a browser update that changed the behavior.

  • @Alexandrec.Caus, the best you have to do is show your code here. So we can test in our browsers and analyze more deeply error.

  • 1

    It may just be a variation on the tags of the pages you tested before. There are things that appear, there are things that do not. The result will be random even as an unprocessed PHP will behave like a broken HTML, so it will depend on how browsers handle exceptions. The best is to view the source code by the browsers instead of trying to see rendered.

  • I’ve had this problem too. It’s strange, but Firefox simply asked to download PHP every time I would open the page on localhost

Show 6 more comments

1 answer

15


PHP is a language of scripts interpreted on the server side.

It’s not like HTML is a hypertext markup language, which will simply run when opening the file.

Once the PHP interpreter starts to act, all the code inserted into the document begins to be processed. This processing generates an HTML file and in this file will exist only tags HTML or any other technology client-side, that will be returned to those who requested the file.


For you to be able to run files with extension .php, it is necessary to have a apache web server to turn the computer into a server that will interpret the scripts PHP, and do not display the codes on the screen.

Although it is possible to download the "components" (Database, Apache, PHP) individually, it is necessary a reasonable knowledge to configure them, since it can be extremely complicated set them up the first time.

However, there are some alternatives (programs) ready, prepared with a default configuration, which already comes with what you need, which most of the time is the PHP language, the Mysql database, and the Apache server.

These programs are known as:

  • WAMP’s (Windows, ToPache, MySQL, PHP),
  • LAMP’s (Linux, ToPache, MySQL, PHP) and
  • MAMP’s (Mac, ToPache, MySQL, PHP)

You can see more information of these programs here.

Your problem

After having read this introduction and being with the installed programs of your choice, when you start Windows, it is necessary "startar" the server (in my case WAMP).

You click on its icon (on the desktop, or on the start menu, or on the installation directory) and when it becomes "greenish", you can open a new tab of your browser and type in the address bar (if your setting is the default) localhost or 127.0.0.1.

Important: You need to move the files (from your project) to the folder within your web server program (if you are using one of the above mentioned programs), such as XAMPP - in the folder htdocs - or WAMP - in the folder www - or create a aliasen.

Remember, PHP is a language of scripts interpreted from the server side, then open the files as HTML:

file:///C:/testes/meuarquivo.php

It will result in your code being shown on the screen instead of being interpreted, regardless of what your browser is. So you should open it with the server protocol, something like:

http://localhost/testes/meuarquivo.php

Another important thing which I forgot to mention: In case there is a problem of, for example, opening some unexpected page, or not opening anything, it is because (probably) you have some program that is using port 80 (default).

Programs installed as Skype, or IIS (which is usually installed on most development machines) can cause this. Then you need to change the server port.

You can see more related information on the official website (in my case Wampen), or more information here.

I will leave a brief solution here, in case you are using WAMP:

  • Click the WAMP> Apache> httpd.conf server icon, then change the Listen 80 line to Listen 8080 or any port you want.
    • If you cannot access through the icon, go to the directory in C: wamp bin apache apache2.4.23 conf> httpd.conf and do what was said above.

Can watch this video also, which was indicated in the answer that was linked above, answered in our Big Brother SO.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.