找到默认网站主页的名称

I'd like a bit PHP code to be able to identify the home page of the domain it's running on (so the code is portable). I could do file_exists on the list of common default home pages but that would find e.g. both of index.php and index.html

I could potentially end up with several as some systems recognise default.html, home.html and others as valid defaults. The servers have a priority list so for example if there is both index.php and index.html the server will give preference to index.php - but not necessarily, htaccess can change the priorities and even set someotherfile.html as default.

If the page with my code in it was the home page I could do $_SERVER['PHP_SELF'] but it's not.

=================

Based on one suggested approach I tried using this code:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo(substr($response, 0, curl_getinfo($ch,CURLINFO_HEADER_SIZE)));
?>  

but header doesn't include filename

HTTP/1.1 200 OK Server: nginx Date: Wed, 11 Jun 2014 19:08:09 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PleskLin

Similar result on a different server

HTTP/1.1 200 OK Date: Wed, 11 Jun 2014 18:39:47 GMT Server: Apache X-Powered-By: PHP/5.4.29 Transfer-Encoding: chunked Content-Type: text/html

====================

The reason is I need a list of all the pages on a web site (excluding any on the server but not linked to so I can't just navigate the directories). I've settled for a compromise and will end up listing example.com/ (whatever it finds) and say, index.htm, which is (probably) the same thing, if there are links to it from other pages. I guess I may be able to eliminate the duplication if necessary by doing something like comparing filesize or timestamp.

This should help you determine the path a PHP file is being run from:

$script_path = realpath(dirname(__FILE__));

But you need to be sure this is placed in a document on the root level of your site to get an accurate path value. So depending on the structure of your site, you might be able to place this line of code in index.php or a common config file that index.php would load via include_once or require_once.

That said, for best & most reliable portability it might be best to just set a base path variable in a config file & just change that when your site is shuffled from location to location. Something like this:

$BASE_PATH = '/var/www/path/to/site/root/';

And then when you need to load files for your code just set the paths like this:

include_once($BASE_PATH . 'lib/some.cool.thing.php');

That way, $BASE_PATH is just set in one place in a common config file. And you know you just have to change that one setting when you migrate code from one setup to another.

if you need the name of the page, i can just think of this:

make a curl request to the same domain, get the response header and check "Content-Location" header field.

$url="www.domain.com"    
$defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    $header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
    $result = curl_exec($ch);
    $header = substr($result, 0, $header_size);
    var_dump($header);
    curl_close($ch);

check if there is what you need....