首页 > 解决方案 > 如何更新php中定义的变量?

问题描述

我正在学习一些PHP编码,并且正在创建一个简单的config.php文件。我API_URL为我的本地服务器定义了一个用于测试的配置文件。

<?php
    DEFINE("API_URL", "http://localhost:8000/api"); 
 ?>

我希望API_URL在用于登台服务器时对其进行更新。例如,我有另一个php脚本将更新所说的API_URL.

<?php
    require_once "config.php";

    $API_URL = API_URL;

    // Here I want to update the API_URL in config file to be something like
    // DEFINE("API_URL", "https://mystagingsite.com/api");
    // And I want this new "API_URL" to be updated in config.php file.
    // How will I do that?
?>

标签: phpconfig

解决方案


您可以从$_SERVER. 您可以使用:$_SERVER['HTTP_HOST'];

所以你可以像这样使用 API URL:

$api_url = $_SERVER['HTTP_HOST'].'/api/';
DEFINE('APIURL',$api_url);

推荐阅读