nedir, her zaman doğru doc ​​kök dize sahip iyi bir yolu?

2 Cevap

i gibi bir menü varsa

<a href="index.php">home</a>
<a href="aboutus.php">about us</a>
<a href="contact.php">contact us</a>

menu.php adlı bir dosya içinde

ve bu menu.php dosya tüm uygulama karşısında sayfaları içine dahil olur. ama ne ben yeni bir klasör oluşturun ve portföy diyoruz ve bu klasörde örnek1.php adlı bir dosya var ve menu.php bu dosyada yer alırsa. hakkımızda ev, ve bize kök onların işaret beri artık alışkanlık iş bağlar. bu yüzden bu işlevi yarattı

function getRoot() {

    $self = $_SERVER['PHP_SELF'];
    $protocol = $_SERVER['https'] == 'on' ? 'https:/' : 'http:/';
    $docroot_before = explode("/", $self);
    array_pop($docroot_before);
    $docroot_after = implode("/", $docroot_before);
    $host = $_SERVER['HTTP_HOST'];

    return  $protocol.$host.$docroot_after."/";
}

ve menü şimdi bu gibi görünüyor

<a href="<?=getRoot();?>index.php">home</a>
<a href="<?=getRoot();?>aboutus.php">about us</a>
<a href="<?=getRoot();?>contact.php">contact us</a>

the link should come out as  http://localhost/domain/aboutus.php (for example)
but it comes out as http://localhost:/domain/portfolio/aboutus.php (and this is wrong).

menu.php dahil alır nerede olursa olsun her zaman doğru doc ​​kök almak için en iyi yolu nedir.?

2 Cevap

Bir URL olarak tek bir bölü başlıyorsa (/) o kök klasörüne bağlanacaktır. Sürece sayfanızın kök klasöründe olduğu gibi daha sonra şu çalışacak (ki, http://www.example.com ve http://www.example.com/foo değil):

<a href="/index.php">home</a>
<a href="/aboutus.php">about us</a>
<a href="/contact.php">contact us</a>

Eğer bir alt etki alanı ise, o zaman /subdomain/ için / değiştirin

Ben uygulamanızın kök klasöründe bir dosya koy ... bir homebrew çerçevesinde aşağıdaki kullanmak ve sadece bunu içerir.

define('ABSPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');

$tempPath1 = explode('/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])));

for ($i = count($tempPath2); $i < count($tempPath1); $i++)
    array_pop ($tempPath3);

$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);

if ($urladdr{strlen($urladdr) - 1}== '/')
    define('URLADDR', 'http://' . $urladdr);
else
    define('URLADDR', 'http://' . $urladdr . '/');

unset($tempPath1, $tempPath2, $tempPath3, $urladdr);

Yukarıdaki kod iki sabit tanımlar. URLADDR uygulamanın tam URL'yi içerir ise ABSPATH uygulama (yerel dosya sistemi) kök mutlak yolunu içerir. Bu mod_rewrite durumlarda çalışır.