0

Why doesn't Google Chrome show the PHP in the source?

For example, here's a page I did (mostly with JQuery). I decided to have a small bit of PHP to fetch the IP address and display it (in a beautified way) so that I could login remotely (because my IP changes because of DHCP).

So I wrote it; here's a couple of screenshots.

The source code in my editor:

Imgur

The source code in the browsers viewsource:

Imgur

Just to clarify, I blacked out the IP, but the IP is all it showed on the screen -- not the PHP that echoed the variable.

Why does it not show the actual PHP, and is there any way to view the PHP?

Another example is the site I curled to get the ip, icanhazip.com. If you visit the page, it shows your ip; but if you view source in the browser, the page contains only the text of your ip, and none of the code that makes it happen.

How can I view the PHP source?

Thanks!

evamvid

6
  • You cannot access the site's PHP, it isn't possible unless it is explicitly released. If it were possible, that would have huge security issues.
    – Adam
    Apr 8, 2014 at 22:08
  • Aside of the answer which explains it all, why would you shell_exec to get an ip if you can just use <?php $_SERVER['REMOTE_ADDR']; ?>
    – LPChip
    Apr 8, 2014 at 22:09
  • 1
    $_SERVER['REMOTE_ADDR'] is the client's IP. He he trying to get the server's external IP. I'm not saying shell_exec is the best way to go about it, but your solution doesn't give the result he wants.
    – heavyd
    Apr 8, 2014 at 22:21
  • @heavyd I tried using PHP's curl, but I couldn't get it to work. I don't know PHP, I just wanted to use it for this specific application.
    – evamvid
    Apr 8, 2014 at 22:42
  • For those voting to close: this question is ultimately about functionality in Chrome, not about PHP. It should not be moved to Stack Overflow.
    – user201262
    Apr 11, 2014 at 20:45

1 Answer 1

11

PHP is a server side language and interpreted on the server. After the code is interpreted, only the HTML is transmitted by the webserver to the client (e.g. Chrome).

So, Chrome has no idea the page is even using PHP - all it got from the webserver was HTML, and thus that's all it can show you for source.

Contrast this to a client-side language like javascript, where you can see the raw javascript code . . .

There's no way to see the raw PHP unless the web server is misconfigured and doesn't render PHP, or you access the file directly on the web server, e.g. using FTP, a shell session, etc, instead of via a browser and the web server (e.g. Apache).

2
  • "you access the file directly on the web server (instead of via the webserver)." I know what you mean, but confusing.
    – gwideman
    Apr 8, 2014 at 23:06
  • @gwideman yeah, I realized that was iffy when I wrote it . . . I tried to make it a little clearer . . .
    – ernie
    Apr 8, 2014 at 23:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .