是否可以检测页面是否包含PHP?

My site is basically 5 "pages" and about 30 "helper pages" that are called by php includes, sometimes 1 or 2 "helper pages" per "page", sometimes 10+ per "page."

Because these are includes I have certain variables and my mysql connection code in the "page" and the helper pages relies on these variables. For example, here is my "edit.php" page:

<form method="POST" enctype="text/plain" action="<?php echo $actual_link; ?>">

<div class="one-hundred"><?php include("include/pages/vehicle.php"); ?></div>

<div class="fifty"><?php include("include/pages/vehicle_valuations.php"); ?></div>
<div class="fifty"><?php include("include/pages/vehicle_features.php"); ?></div>

<div class="thirty-three">
    <?php 
        include("include/pages/purchased_info.php"); 
        include("include/pages/vehicle_Titles.php"); 
    ?>
</div>

<div class="thirty-three"><?php include("include/helper_content/warranty_lookup.php"); ?></div>
<div class="thirty-three"><?php include("include/pages/vehicle_advertising.php"); ?></div>

<div id="buttons">
    <input type="submit" id="Submit" value="Submit">
</div>

What I'm wondering is if I direct my browser directly to www.____.com/include/pages/vehicle.php the page fails, because of the missing mysql connection script and missing variables. Is there a way to detect if someone is accessing the page directly, or through an iframe or ajax or include?

You could look for a $_GET parameter inside the php file that was "included."

For example,

include(".../file.php?included=true");

Then, inside file.php...

<?php

if isset($_GET['included']) {

//page was "included!"

}

But when the user visits the page via http://... there's no $_GET parameter added to the URL string, hence the page wasn't included.

Yes, but it's kinda messy. First, you call get_included_files. It returns the array of all files that have been included so far, so you just validate each of them against __FILE__

Define your connection in a separate PHP file and include it in the pages / sub-pages that need it, using include_once() instead of include(). That way, you can use your connection on all pages without re-including it unnecessarily.