PDA

View Full Version : سوال: پیدا کردن مسیر اصلی یا ریشه سایت



mostafa_shoakry
چهارشنبه 20 دی 1391, 15:15 عصر
من چطوری می تونم مسیر اصلی سایت رو پیدا کنم.
مثلا من یه پوشه دارم به اسم images که می خوام به صورت زیر به پوشه دسترسی پیدا کنم:


http://localhost/images/
وای چن تا جستجو تو نت زدم دیدم که مسیرو اینجور تشخیص می ده:


http://localhost/projectfolder/images/
چن تا کد هم پیدا کردم که ریشه سایت رو به سورت زیر پیدا می کنه


http://localhost/projectfolder/management/folder/
من فقط این قسمتشو می خوام:


http://localhost/projectfolder/
یا


http://www.mysite.com/projectfolder/
چه جوری اونو درش بیارم؟ :ناراحت:
اینا هم نمونه کد هایی هست که پیدا کردم:



public function base_url(){
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = $this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['HTTP_HOST'].$port.$_SERVER['REQUEST_URI'];
}
public function get_base_url()
{
/* First we need to get the protocol the website is using */
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https://' ? 'https://' : 'http://';

/* returns /myproject/index.php */
$path = $_SERVER['PHP_SELF'];

/*
* returns an array with:
* Array (
* [dirname] => /myproject/
* [basename] => index.php
* [extension] => php
* [filename] => index
* )
*/
$path_parts = pathinfo($path);
$directory = $path_parts['dirname'];
/*
* If we are visiting a page off the base URL, the dirname would just be a "/",
* If it is, we would want to remove this
*/
$directory = ($directory == "/") ? "" : $directory;

/* Returns localhost OR mysite.com */
$host = $_SERVER['HTTP_HOST'];

/*
* Returns:
* http://localhost/mysite
* OR
* https://mysite.com
*/
return $protocol . $host . $directory;
}
public function getBaseUrl()
{
// output: /myproject/index.php
$currentPath = $_SERVER['PHP_SELF'];

// output: Array ( [dirname] => /myproject [basename] => index.php [extension] => php [filename] => index )
$pathInfo = pathinfo($currentPath);

// output: localhost
$hostName = $_SERVER['HTTP_HOST'];

// output: http://
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

// return: http://localhost/myproject/
return $protocol.$hostName.$pathInfo['dirname']."/";
}

mamali-mohammad
چهارشنبه 20 دی 1391, 16:12 عصر
$_SERVER['HTTP_HOST']