2013-10-23 24 views
1

你会认为我应该在Google上找到这个,但是我不知道。从PHP中的config.ini中读取值

的config.ini

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

我需要阅读主机这里的值:

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

有谁知道?

+1

检查['parse_ini_file()'](http://php.net/manual/en/function.parse-ini-file.php) – pNre

+1

真的吗?谷歌'读取ini文件',第一个链接... –

+0

我认为你只是懒惰地使用谷歌:https://www.google.com/search?q=reading%20values%20from%20config.ini%20in %20PHP。只是搜索你的标题。 – idmean

回答

1

这很简单。使用内置函数parse_ini_file()

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

而且,将用户重定向,你可以这样做:

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(); 
} 

这也将确保$host是一个有效的URL。

+0

它找不到config.ini。我想我需要在那里提供绝对的URL。 – Adi

+0

@Adi:它应该工作。什么'var_dump(file_exists('path-to-config.ini-here'));'输出? –

+0

布尔值为false。怎么样??!!该文件存在那里!源文件夹/ Config/config.ini – Adi