How can I view PHP source code on a live site?
No, as it is interpreted on the server-side and the results are sent to the user. If you want to view the source code of a site you control in-browser, consider the FirePHP extension for Firebug, or just access your site files with your preferred method.
Possible to view PHP code of a website?
A bug or security vulnerability in the server (either Apache or the PHP engine), or your own PHP code, might allow an attacker to obtain access to your code.
For instance if you have a PHP script to allow people to download files, and an attacker can trick this script into download some of your PHP files, then your code can be leaked.
Since it's impossible to eliminate all bugs from the software you're using, if someone really wants to steal your code, and they have enough resources, there's a reasonable chance they'll be able to.
However, as long as you keep your server up-to-date, someone with casual interest is not able to see the PHP source unless there are some obvious security vulnerabilities in your code.
Read the Security section of the PHP manual as a starting point to keeping your code safe.
Is it possible to read php code from other sites?
It is only possible if the site's owner intentionally makes the code publicly available, or if the site has a very critical security hole. Usually, no, you cannot read other people's server-side code.
After all, we'd have huge problems if you could just read any server-side code you wanted. If, say, a bank's website were built in PHP, it would be important that raw PHP files could not be read by the public: they might contain private data like database passwords, or simply make it much, much easier to find other key security issues, since, after all, which is easier—finding problems with code you can see, or code you can't see? As much as I am a fan of open-source coding, I would not put my money in any bank that makes its source code available. It's just too risky.
However, if you're interested in learning, there are plenty of open-source web applications out there. Try searching Github for, say, projects written in PHP. It's definitely a good idea to read other people's code, and I'm sure you could find some stellar examples of real-world code if you know where to look. Additionally, if you ask nicely, a website owner may be willing to share parts of the source code with you, though probably not all of it, for security reasons. Couldn't hurt to ask.
Can anyone get access to my PHP source code?
With a correctly configured web server, the PHP code isn't visible to your website visitors. For the PHP code to be accessible by people who visit your website, the server would have to be configured to display it as text instead of processing it as PHP code.
So, in other words, if you visit your website and you see a HTML page and not PHP code, your server is working correctly and no one can get to the PHP code.
How do I show / run a PHP file in a browser? As if it was a webpage
You need to download a server, and install it. If you want to go to the trouble, you can get XAMPP, and once it's installed, move the PHP file to the root of your installation (usually c:\xampp\htdocs\
on windows) and then use the url localhost/script.php
in your browser.
How to display PHP & HTML source code on a page?
You can use html entities <?php
in the html it will be rendered as <?php
You can use htmlspecialchars to encode your code to use html entities.
How do I get the HTML code of a web page in PHP?
If your PHP server allows url fopen wrappers then the simplest way is:
$html = file_get_contents('https://stackoverflow.com/questions/ask');
If you need more control then you should look at the cURL functions:$c = curl_init('https://stackoverflow.com/questions/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
Related Topics
Speeding Up Large Numbers of MySQL Updates and Inserts
Upload Progress Using Pure PHP/Ajax
How to Build a Condition Based Query in Laravel
Check If the Current Date Is Between Two Dates + MySQL Select Query
PHP Error: Cannot Use Object of Type Stdclass as Array (Array and Object Issues)
Bool Parameter from Jquery Ajax Received as Literal String "False"/"True" in PHP
Missing CSS File and Images After Url Rewrite
Difference Between $_Server['Document_Root'] and $_Server['Http_Host']
Custom Repository Class in Symfony2
Mongodb: Benefit of Using Objectid VS a String Containing an Id
Doctrine2 - "Class" Is Not a Valid Entity or Mapped Super Class
Yii Framework 2.0 Login with User Database
In Magento 1.9.0.0 How to Enable Template Path Hint
How to Convert Ldap Timestamp to Unix Timestamp
PHP Using Regex to Get Substring of a String
How to Skip Elements in Foreach Loop
Laravel 4 - Including a "Partial" View Within a View (Without Using Blade Template)