2016-11-20 28 views
-1
$file = 'userdata.txt'; 
file_put_contents($file, print_r($_SERVER['HTTP_USER_AGENT']), 
FILE_APPEND); 

在userdata.txt我:1(不知道爲什麼)

最後,我要保存到.txt$_SERVER['HTTP_USER_AGENT']$_SERVER['REMOTE_ADDR']

+0

歡迎來到SO。請閱讀[我可以問哪些主題](http://stackoverflow.com/help/on-topic)和[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask )和[完美的問題](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)和[如何創建一個最小,完整和可驗證的例子](http:///stackoverflow.com/help/mcve) – mazedlx

回答

2
$file = 'userdata.txt'; 
file_put_contents($file, print_r($_SERVER['HTTP_USER_AGENT']), FILE_APPEND); 

print_r function(在上述情況下)將輸出打印到stdout並返回TRUE給來電者。所以,你有什麼是相同的:

$file = 'userdata.txt'; 
print_r($_SERVER['HTTP_USER_AGENT']); 
file_put_contents($file, TRUE, FILE_APPEND); 

TRUE就變成1當它是converted to string

最簡單的方式做你想要的是簡單地跳過print_r電話:

$file = 'userdata.txt'; 
file_put_contents($file, $_SERVER['HTTP_USER_AGENT'], FILE_APPEND); 

或者,如果你想通過print_r產生的特定的格式,你可以添加第二個參數:

$file = 'userdata.txt'; 
file_put_contents($file, print_r($_SERVER['HTTP_USER_AGENT'], TRUE), FILE_APPEND); 

這將導致print_r返回輸出,而不是直接打印。