2013-10-23 27 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