PHP code opens in browser

Asked

Viewed 11,888 times

5

I am unable to install PHP on my machine. When I run a page in the browser, it returns the code written in . php.

I installed Apache 2.2.9 and PHP 5.2.1, and made all the changes based on this tutorial

Even with all the changes, he keeps making the same mistake. Apache works normally, when I call localhost, it returns me: Its Works, but PHP that can’t run.

Use Windows 7 Home Basic 86x operating system.

  • Fastcgi may be missing. Try configuring by following this tutorial http://php.net/manual/en/install.windows.iis7.php, but this version of PHP is very old, try installing a complete solution like WAMP http://wampserver.com/en, already with a newer version of PHP.

  • in your same tutorial, try to make the described configuration here

  • I have already installed Wamp, owned it on PC and still gives error. Easyphp also.

3 answers

5

Reading your question, I remembered an answer from me on Soen, I believe this is the way:

The problem is that you may have accessed the page through the FILE protocol instead of the HTTP protocol, as already said by Gustavo and Ricardo, I will explain the differences:

Differences between HTTP and FILE

  1. You should learn the differences between "http protocol" and "file protocol".

    HTTP protocol:

    HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server. The client sends an HTTP request message to the server. The server, which provides features such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The reply contains completion status information on the request and may also contain requested content in your message body.

    File protocol:

    The file URI scheme is a URI scheme specified in "RFC 1630" and "RFC 1738", normally used to retrieve files from within your own computer, if you open a PHP file directly the URL in the address bar will usually present this:

protocol file Opera browser

  1. Second, your need to learn about "Developer tools" (network tab/network tab), with Developer tools you can detect "path", page headers and responses to understand specific problems.

    Using Developer tools in Chrome (in some browsers that use Webkit is similar): https://developer.chrome.com/devtools#Improving-network-performance

    Example: network chrome
    (source: Chrome.)

    Using Firefox: https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor

    Example: network firefox

    Using Safari 8: https://developer.apple.com/safari/tools/

    Safari 8 network tools

Tools I recommend to install PHP on your machine:

4


If it is showing the code written in the . php file it is possible that:

  1. You have not placed the project inside a specific folder where it is accessible to the localhost.
  2. has placed in this location accessible to localhost but is accessing your page . php using the file protocol:\.
  3. Use a Xampp, Wamp or easyphp to install and configure everything automatically for you, in the case of these solutions the only thing you should do is to put the project inside the localhost root folder (htdocs for example) and call in the browser with localhost.
  • It was in the htdocs folder anyway, and yet it shows the code. I put the code: "<? php phpinfo(); ? >" (no quotes), inside the folder and run, and it appears in the browser, and does not execute the function. I’ve already installed Wamp and Easyphp and it’s still the same thing.

  • accessed by localhost?

  • Now it’s working, when I pull the browser, putting: "localhost/example.php", but if I run from the folder itself, double-clicking to open the browser, it displays the code. And when I put a form in html, and have an action in php (external link), it also displays the code, regardless of whether I put: sending.php or localhost/sending.php

  • yes when you give a double click you open the page with the protocol file:\\\ by the browser that does not run php, with localhost you run using the protocol http and runs php like you said.

  • Got it, thank you very much. ?

  • Double click on . php and already running like java there is no changing, signal that my answer was useful by marking the green arrow next to it and an upvote (triangle with tip up)

  • Double clicking does not work because PHP, unlike HTML, needs to be interpreted before it is executed. It runs on the server side, that is, using Wamp, a local server is created on your computer. If you test your file on another computer that does not have a local server with PHP installed, the same problem will happen.

Show 2 more comments

-2

If you are accessing via: file://C:/xampp/htdocs/ this is the problem. Just go to: localhost/pasta_do_seu_projeto_no_XAMPP

I hope I’ve helped

Browser other questions tagged

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