PHP, nasıl bir php değişken olarak etki sonra herhangi bir dize alabilirim?

5 Cevap php

bu yüzden benim index.php bu olabilir:

<?php
      $restOfURL = ''; //idk how to get this
      print $restOfURL; //this should print 'FOO', 'VAR3', or any string after the domain.
 ?>

5 Cevap

$_SERVER['REQUEST_URI'], herkes işaret ettiği gibi, URL neye benzediğini söyleyebilirim Okuma, ama gerçekten {[(1) isteklerini işaret etmek için bir yol yoksa, istediğiniz şekilde çalışmıyor ]} ve me.com/VALUE2 işlem yapacak script. (Aksi sunucu ... istediğiniz her değer için bir komut dosyası yoksa bir 404 hatası döndürmek, komut zaten değerini bilir hangi durumda olacak)

Eğer apache kullanıyorsanız varsayarsak, mod_rewrite kullanmak istiyorum. Sen yüklemek ve etkinleştirmek modülü ve sonra. Htaccess, httpd.conf veya sanal konak config bazı directives eklemek gerekecek. Bu size $_GET['var'] adlı değeri okuyabilirsiniz böylece me.com/XXX, me.com/index.php?var=XXX için dahili harita için bir istek yapmak sağlar.

Kullanmak istediğiniz,

<?php
    $restOfURL = $_SERVER['REQUEST_URI'];

    // If you want to remove the slash at the beginning you can use ltrim()
    $restOfURL = ltrim($restOfURL, "/");
?>

Sen PHP documentation içinde önceden tanımlanmış sunucu değişkenleri daha bulabilirsiniz.

Update
Based on your comment to the question, I guess you're using something like mod_rewrite to rewrite the FOO, etc and route everything to just one file (index.php). In that case I would expect the rest of the URL to already be passed to the index.php file. However, if not, you can use mod_rewrite to pass the rest of the URL as a GET variable, and then just use that GET variable in your index.php file.

Yani mod_rewrite etkinleştirin ve ardından .htaccess dosyasına böyle bir şey eklerseniz,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Sonra URL kalanı $_GET['url'] değişkeni index.php dosyasında sizin için mevcut olacak.

$var = ltrim( $_SERVER['REQUEST_URI'], '/' )

http://www.php.net/manual/en/reserved.variables.server.php

Just by looking at the examples, i think you are looking for the apache mod_rewrite. You can apply a RewriteRule via an htaccess file, for example:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([\w]+)$ /checkin.php?string=$1 [L]

Örneğin bu url http://foo.com/aka2 checkin.php script tarafından bir süreç olacak ve $ _GET ['string'] olarak geçti "aka2" sahip olacaktır.

Hiç kuşkunuz olmasın, URL hala tarayıcıda görünür olacak http://foo.com/aka2, ancak sunucu olacak aslında süreç http://foo.com/checkin.php?string=aka

mod_rewrite documentation

Neden tüm süslü mod_rewrite / sorgu_dizgesi iş ile rahatsız? Sadece bu tür veriler için $_SERVER['PATH_INFO'] mevcut zaten var.