问题
As is says in the title, my PHP code is showing up in the browser code inspector like it is commented out. For example,
<?
include("assets/php/dbconn.inc.php");
$conn = dbConnect();
$sql = "SELECT * FROM movies";
$rs = $conn->query($sql) or die ("Movie query failed");
$number_of_rows = $rs->num_rows;
while($row = $rs->fetch_assoc()){
echo("{$row['title']}");
}
?>
displays in the browser as
<!--?
include("assets/php/dbconn.inc.php");
$conn = dbConnect();
$sql = "SELECT * FROM movies";
$rs = $conn--->
and the rest of the code prints on the webpage.
I am testing this using WAMP on my local machine. Any idea where these comment tags are coming from?
回答1:
Never use short open tag <?
in your PHP code. Always use long tags <?php
, simply because <?
can be disabled (or not enabled, may depend on distro) in php.ini
with short_open_tag
directive (and this looks like your culprit). In result, PHP scripts are not processed by PHP interpreter and will usually end up sent to the visitor as plain text, exposing your whole source code (but also DB credentials etc).
回答2:
Posted on behalf of the OP:
SOLVED I was navigating to the file directly instead of through the localhost address. Rookie mistake.
来源:https://stackoverflow.com/questions/20434280/php-code-displaying-as-comments-in-browser-using-wamp