简体   繁体   English

在PHP中包含/调用base_url

[英]Including/Calling base_url in PHP

I learned and worked with Code Igniter before so I know how to do 我之前学习过Code Igniter并与之合作过,所以我知道该怎么做

in CI we call this function 在CI中,我们将此功能称为

   <?php echo base_url('sample.php');?>

but how to do that in pure PHP ? 但是如何在纯PHP中做到这一点?

I tried to include like this 我试图包括这样

<?php include('../../sample.php');?>

Is there any better solution? 有更好的解决方案吗? Thanks 谢谢

Define the base url as a string, then append that constant to your include calls. 将基本url定义为字符串,然后将该常量附加到您的include调用中。

define("BASE_URL", "/var/www/httpdocs/");
include(BASE_URL . 'sample.php');

Its worth noting that once BASE_URL is defined it can be used anywhere. 值得注意的是,一旦定义了BASE_URL ,它便可以在任何地方使用。

EDIT 编辑

Your last comment shows that you are unsure of what you are doing. 您的最后一条评论表明您不确定自己在做什么。 Includes are done via the filesystem on the server or your local machine. 包含是通过服务器或本地计算机上的文件系统完成的。 They are nothing to do with the URL the domain or localhost. 它们与域或本地主机的URL无关。

// This means nothing as the include is done via the file system not via the URL
define("BASE_URL", "localhost:80");
include(BASE_URL . 'sample.php');

// This is what you need, a path on your file system to your included file
define("BASE_URL", "/var/www/httpdocs/");
include(BASE_URL . 'sample.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM