How can I view PHP source code on a live site?
You can generally not view the raw PHP source code of a live website from the client side. When you visit a URL in your browser, the server executes the PHP scripts and returns the resulting HTML, CSS, or JSON to the user. By default, the <?php tags are stripped out during this execution phase, leaving only the rendered output visible.
However, there are specific scenarios where source code leakage occurs, often due to misconfiguration or security oversights. Understanding how PHP handles file serving and error reporting is crucial for both attackers trying to exploit vulnerabilities and administrators aiming to prevent them.
The Default Behavior: Execution vs. Serving
PHP files, such as index.php, are designed to be executed. When a request hits the server for this file, the PHP interpreter runs the logic, outputs data, and then terminates. The raw source is never sent over the network in a standard configuration. If you try to access https://example.com/index.php directly, you will either see the rendered page content or an error message if execution fails.
This behavior differs from static files. If you have access to a web server where PHP modules are not loaded for specific directories, you might be able to view the source code simply by requesting the file path. This is common in shared hosting environments where developers might forget to disable directory indexing or inadvertently allow the .php extension to serve files as text.
Inspecting Response Headers
One of the most effective ways to detect if PHP source code has leaked is to inspect the HTTP response headers. Modern servers and application frameworks often include a header named X-Powered-By or similar indicators. While these headers rarely reveal the full source, their absence can sometimes hint at obfuscation attempts or strict security configurations.
More critically, look for error messages in the HTML body. If a PHP script throws a fatal error and the server is configured to display errors (display_errors enabled), the raw stack traceâincluding file paths and line numbersâwill be rendered as plain text. This effectively exposes parts of the source code structure to anyone who can trigger an exception, such as by accessing a non-existent resource or passing invalid parameters.
Using Developer Tools and Network Inspection
While you cannot download index.php directly from a browser in a secure environment, you can analyze how the content is served. Open your browser's Developer Tools (F12) and navigate to the Network tab. Select the main document request. Right-click on the response and choose "Open in new tab" or inspect the raw payload.
If the status code is 200 OK and the content type is text/html, you are seeing rendered output. If, however, the content type is text/plain or application/octet-stream, or if the file size matches the expected line count of a source file rather than a rendered page, it suggests the server is serving the raw file instead of executing it. This usually happens when .htaccess rules are misconfigured or when specific files have been uploaded with incorrect permissions.
Security Implications and Mitigation
Viewing PHP source code on a live site is almost always a security incident. It reveals internal logic, database credentials stored in variables (if not properly sanitized), and architectural decisions that attackers can leverage for further exploitation, such as SQL injection or Remote Code Execution.
To prevent this, developers should ensure that:
- Error Reporting is Disabled: Set
display_errorstoOffand log errors to a file instead in production environments. - File Permissions are Strict: Ensure PHP files are only executable by the web server user and readable by others if necessary, but never writable by public users.
- Directory Indexing is Controlled: Prevent the server from listing directory contents or serving
.phpfiles as static text unless explicitly intended (which is rare).
If you suspect a site is leaking code, tools like curl can be used to verify the content type and check for specific headers:
curl -I https://example.com/index.php
Look closely at the response. If you see a stack trace or raw PHP tags in the output, immediate remediation is required. For robust application development, frameworks like Laravel handle these configurations by default, ensuring that production environments do not expose source code unless explicitly configured to do so for debugging purposes.
