1

I have two files, one config.php and source.php in a subdirectory. In the config file I have something that looks like this

<?php
//app settings
$GLOBALS['app_url'] = 'http://www.website.com/subdirectory'; //ex: 
//demo mode
$GLOBALS['demo_mode'] = 0; //possible values: 0 or 1
$GLOBALS['db_table']['sms'] = 'sms_numbers';
$GLOBALS['db_table']['sms_history'] = 'sms_history';
?>

In the config.php file, I have this string $GLOBALS['app_url'] = 'http://www.website.com/subdirectory'; for the base URL and I'd like to make it so that the base URL is automatically detected. I'd like to use something similar to this <?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>. I'm using these files under different directories and that's why I'd like to combine them in order to make it automatic without me having to update the base URL manually every time I create a new subdirectory. Also, inside config.php I have:

//Admin access
$GLOBALS['admin_username'] = 'admin';
$GLOBALS['admin_password'] = 'password';

These values are not in a database but a local file named source.php and I'd like to be able to update the values "admin" and "password" from this source.php file. Inside the source.php file I guess I'd have to have something thta'd look like this: $username = 'admin'; I'm really sorry but I'm new and would like to learn this stuff. I appreciate any help I can get. Thank you

1

1 Answer 1

1

Use global constants in config.php

define('APP_URL', 'http://www.website.com/subdirectory'); 

Then anywhere in the code you can do:

$path = APP_URL . "/path/to/file"

I dont recommend storing admin_username and admin_password as global variables or constants, instead you can create a class in your config.php that contains the values.

Config Example:

config.php

define('APP_URL','http://www.website.com/subdirectory');  
define('DEMO_MODE',0); //possible values: 0 or 1
.....

class DB{
    var $conn;

    public function __construct()
    {
       $user = "admin";
       $pass = "pass";
       $host = "127.0.0.1";
       $database = "database_name";
       $this->conn = mysqli_connect($host, $user, $pass, $database);
    }
}

Then in your index.php file you do:

require_once("config.php")
$db = new DB;
$conn = $db->conn();
....
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! @deepblue, I'll try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.