Jay Marz
Jay Marz

Reputation: 1912

How to update defined variable in php?

I am learning some PHP coding and I am creating a simple config.php file. I that config file I have defined an API_URL for my local server for testing.

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

I want this API_URL to be updated when used to staging server. For example I have this another php script that will update the said 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?
?>

Upvotes: 0

Views: 134

Answers (1)

Jaymin
Jaymin

Reputation: 1661

You can take dynamic variables from $_SERVER. You can use: $_SERVER['HTTP_HOST'];

So you can use like this API URL:

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

Upvotes: 1

Related Questions