如何在核心 php 中定义 Base Url
How to define Base Url in core php
好吧,我正在研究核心 php,我无法为我的应用程序定义常量基数 URL。
我正在 mac os 上使用 xampp,我在 htdocs/myproject/private 目录中有主文件 initilize.php,其中包含要定义的代码一个基地 URL
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$validURL = str_replace("&", "&", $url);
现在,当我将 initialize.php 文件包含在 myproject/index.php 中时,
<?php
require_once 'private/initialize.php';
header('Location: site/');
我得到 BASE_URL 作为 http://localhost/myproject/site ,但是当我在 site/create.php 中包含 initilize.php 文件时,我得到了 BASE_URL 作为
http://localhost/myproject/site/create.php, I am unable to define a base URL for myproject, which in xampp case is localhost/myproject and after deployment it must be http://subdomain.myproject.com 这方面的任何建议。提前致谢。
您可以使用环境变量(.env 文件或 apache/nginx 配置)或者您可以手动设置它,如下所示,没有魔法可以找到一个基础 url你的本地主机,如果你是它的 /myproject 部分。
$protocol = if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$baseUrl = $protocol . $_SERVER['HTTP_HOST'];
if (strstr($baseUrl, 'localhost')) {
$baseUrl = 'http://localhost/myproject/site';
}
您可以为您的 myproject
目录定义静态 base_url,如下所示:
$protocol = if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$servername = filter_input(INPUT_SERVER, 'SERVER_NAME');
$url = $protocol . $servername;
if (strstr($servername, 'localhost')) {
$url .= '/myproject/'; // add project directory on localhost setup
}
$validURL = str_replace("&", "&", $url);
最后,我用下面的代码解决了我的问题
/*
* Assign the root URL to a PHP constant
* Do not need to include the domain
* Use same document root as web server
* Can dynamically find everything in URL up to "/site"
*/
$site_end = strpos($_SERVER['SCRIPT_NAME'], '/site') + 5;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $site_end);
define("WWW_ROOT", $doc_root);
$url = "http://" . $_SERVER['HTTP_HOST'] . WWW_ROOT;
$validURL = str_replace("&", "&", $url);
define("BASE_URL", $validURL);
总是returnBASE_URL=http://localhost/myproject/site
好吧,我正在研究核心 php,我无法为我的应用程序定义常量基数 URL。
我正在 mac os 上使用 xampp,我在 htdocs/myproject/private 目录中有主文件 initilize.php,其中包含要定义的代码一个基地 URL
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$validURL = str_replace("&", "&", $url);
现在,当我将 initialize.php 文件包含在 myproject/index.php 中时,
<?php
require_once 'private/initialize.php';
header('Location: site/');
我得到 BASE_URL 作为 http://localhost/myproject/site ,但是当我在 site/create.php 中包含 initilize.php 文件时,我得到了 BASE_URL 作为 http://localhost/myproject/site/create.php, I am unable to define a base URL for myproject, which in xampp case is localhost/myproject and after deployment it must be http://subdomain.myproject.com 这方面的任何建议。提前致谢。
您可以使用环境变量(.env 文件或 apache/nginx 配置)或者您可以手动设置它,如下所示,没有魔法可以找到一个基础 url你的本地主机,如果你是它的 /myproject 部分。
$protocol = if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$baseUrl = $protocol . $_SERVER['HTTP_HOST'];
if (strstr($baseUrl, 'localhost')) {
$baseUrl = 'http://localhost/myproject/site';
}
您可以为您的 myproject
目录定义静态 base_url,如下所示:
$protocol = if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$servername = filter_input(INPUT_SERVER, 'SERVER_NAME');
$url = $protocol . $servername;
if (strstr($servername, 'localhost')) {
$url .= '/myproject/'; // add project directory on localhost setup
}
$validURL = str_replace("&", "&", $url);
最后,我用下面的代码解决了我的问题
/*
* Assign the root URL to a PHP constant
* Do not need to include the domain
* Use same document root as web server
* Can dynamically find everything in URL up to "/site"
*/
$site_end = strpos($_SERVER['SCRIPT_NAME'], '/site') + 5;
$doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $site_end);
define("WWW_ROOT", $doc_root);
$url = "http://" . $_SERVER['HTTP_HOST'] . WWW_ROOT;
$validURL = str_replace("&", "&", $url);
define("BASE_URL", $validURL);
总是returnBASE_URL=http://localhost/myproject/site