PHP ile taban URL almak nasıl?

7 Cevap

I XAMPP, Windows Vista kullanıyorum. Benim gelişmede, ben http://127.0.0.1/test_website/.

Nasıl http://127.0.0.1/test_website/ PHP ile alabilirim?

Ben bu gibi bir şey denedim, ama bunların hiçbiri çalıştı.

echo dirname(__FILE__)
or
echo basename(__FILE__);
etc.

7 Cevap

Fonksiyon uyarı olmadan çalıştırmak için ayarlanabilir:

function url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    }
    else{
        $protocol = 'http';
    }
    return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

Ben $_SERVER superglobal aradığınız bilgi olduğunu düşünüyorum. Böyle bir şey olabilir:

echo $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']

Sen alakalı PHP belgelerine here görebilirsiniz.

Ev url

<?php echo $_SERVER['HTTP_HOST'];?>


current url (like: /myfodler/myfile.php?action=blabla

<?php echo $_SERVER["REQUEST_URI"];?>


ONLY current FILE url (like: /myfodler/myfile.php)

<?php echo $_SERVER['PHP_SELF']; ?>


current working folder location (like: /myfolder/subfolder)

<?php echo dirname($_SERVER["REQUEST_URI"]); ?>


current working folder (with ftp HOME folder)

<?php echo dirname(__FILE__); ?>


You can mix them, like $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']

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

Fun 'base_url' parçacığını!

if (!function_exists('base_url')) {
    function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
        if (isset($_SERVER['HTTP_HOST'])) {
            $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
            $hostname = $_SERVER['HTTP_HOST'];
            $dir =  str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);

            $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
            $core = $core[0];

            $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
            $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
            $base_url = sprintf( $tmplt, $http, $hostname, $end );
        }
        else $base_url = 'http://localhost/';

        if ($parse) {
            $base_url = parse_url($base_url);
            if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
        }

        return $base_url;
    }
}

: Olarak kullanmak kadar basit

//  url like: http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php

echo base_url();    //  will produce something like: http://stackoverflow.com/questions/2820723/
echo base_url(TRUE);    //  will produce something like: http://stackoverflow.com/
echo base_url(TRUE, TRUE); || echo base_url(NULL, TRUE);    //  will produce something like: http://stackoverflow.com/questions/
//  and finally
echo base_url(NULL, NULL, TRUE);
//  will produce something like: 
//      array(3) {
//          ["scheme"]=>
//          string(4) "http"
//          ["host"]=>
//          string(12) "stackoverflow.com"
//          ["path"]=>
//          string(35) "/questions/2820723/"
//      }

Eğer böyle yapabilirsiniz

ama üzgünüm benim ingilizce, yeterince iyi değil

Önce bu basit kod ile ana üs url olsun ..

Ben yerel sunucu tarafından bu kodu test ettik ve kamu ve sonuç iyidir ..

<?php

function home_base_url(){   

// first get http protocol if http or https

$base_url = (isset($_SERVER['HTTPS']) &&

$_SERVER['HTTPS']!='off') ? 'https://' : 'http://';

// get default website root directory

$tmpURL = dirname(__FILE__);

// when use dirname(__FILE__) will return value like this "C:\xampp\htdocs\my_website",

//convert value to http url use string replace, 

// replace any backslashes to slash in this case use chr value "92"

$tmpURL = str_replace(chr(92),'/',$tmpURL);

// now replace any same string in $tmpURL value to null or ''

// and will return value like /localhost/my_website/ or just /my_website/

$tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);

// delete any slash character in first and last of value

$tmpURL = ltrim($tmpURL,'/');

$tmpURL = rtrim($tmpURL, '/');


// check again if we find any slash string in value then we can assume its local machine

    if (strpos($tmpURL,'/')){

// explode that value and take only first value

       $tmpURL = explode('/',$tmpURL);

       $tmpURL = $tmpURL[0];

      }

// now last steps

// assign protocol in first value

   if ($tmpURL !== $_SERVER['HTTP_HOST'])

// if protocol its http then like this

      $base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';

    else

// else if protocol is https

      $base_url .= $tmpURL.'/';

// give return value

return $base_url; 

}

?>

// and test it

echo home_base_url();

çıktı bu gibi olacak:

local machine : http://localhost/my_website/ or https://myhost/my_website 

public : http://www.my_website.com/ or https://www.my_website.com/

Web sitenizin index.php home_base_url işlevini kullanın ve bunu tanımlamak

ve sonra gibi url yoluyla script, css ve içerik yüklemek için bu işlevi kullanabilirsiniz

<?php

echo '<script type="text/javascript" src="'.home_base_url().'js/script.js"></script>'."\n";

?>

Bu gibi çıktı yaratacak:

<script type="text/javascript" src="http://www.my_website.com/js/script.js"></script>

ve bu script çalışıyor ise,!

$ Http = isset ($ _SERVER ['HTTPS']) && $ _SERVER ['HTTPS'] == 'on'? "Https://": "http://";

$ Url = $ http. $ _SERVER ["SERVER_NAME"]. $ _SERVER ['REQUEST_URI'];