who-aditya-nawandar
who-aditya-nawandar

Reputation: 1344

reading values from config.ini in PHP

You would think I should have found this on Google, but I couldn't.

config.ini

 [ENVIRONMENT]
env = prod
js_path = js/
css_path = css/
;host= www.xxxxxxxx.com
    host=http://localhost:8080/home  
test = 0

I need to read the value of host here:

<?php
header('Location: ?????????????????');
echo 'Thank you for contacting us. We will be in touch with you very soon.';
}
?>

Anyone knows?

Upvotes: 1

Views: 133

Answers (1)

Amal Murali
Amal Murali

Reputation: 76666

It's really simple. Use the built-in function parse_ini_file():

$ini_array = parse_ini_file('../containing-folder/config.ini');
$host = $ini_array['host'];

And, to redirect the user, you can do:

if (isset($host) && filter_var($host, FILTER_VALIDATE_URL)) {
    echo 'Thank you for contacting us. We will be in touch with you very soon.';
    header("Location: $host");
    exit();
}

This will also make sure that $host is a valid URL.

Upvotes: 1

Related Questions