crashmaster Posted April 6, 2008 Share Posted April 6, 2008 Hi there, I need help. How can I write $string to file (.txt) to new line ?? I tried this, but it doesnt work: function ban_ip($ip) { $path = _path ('logs/bans.txt'); $str = '\n'.$ip.'#'.$_SERVER["SCRIPT_NAME"]; @$fp = fopen ($path, 'a+'); @fwrite($fp, $str); @fclose($fp); @@chmod ($path,0777); } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 You need to enclose special characters like new lines in double quotes. Quote Link to comment Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 you mean all special chars line \t \n etc. should be in " " ?? like "\n" ?? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 6, 2008 Share Posted April 6, 2008 Yup. Otherwise php litterally writes \n to the screen. Quote Link to comment Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 IT doesnt work... I will post you whole script here: function _path ($path) { $calls = 0; $calls ++; if($calls > 15) return FALSE; if (!file_exists($path)) { return _path('../'.$path); } else { return $path; } } function ban_ip($ip) { $lol = '/pages/add/message.php'; if ($_SERVER["SCRIPT_NAME"] != $lol) { $path = _path ('logs/bans.txt'); $str = "\n".$ip."#".$_SERVER["SCRIPT_NAME"]; @$fp = fopen ($path, 'a+'); @fwrite($fp, $str); @fclose($fp); @@chmod ($path,0777); } } bans.txt should be like this: 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php 85.25.151.22#badscipt.php ... Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 6, 2008 Share Posted April 6, 2008 try removing the "silencers" from the file write functions and see if they are giving any errors: $fp = fopen ($path, 'a+'); fwrite($fp, $str); fclose($fp); chmod ($path,0777); Also, are you viewing the contents of the file in your browser or in a text editor (vi, gedit, wordpad, etc)? Quote Link to comment Share on other sites More sharing options...
crashmaster Posted April 6, 2008 Author Share Posted April 6, 2008 Oh its ok.. F**ing notepad ) )If I open it in Rapid PHP its ok ) Thank you ) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 6, 2008 Share Posted April 6, 2008 yeah...notepad doesn't recognize "\n" as a new line...it must be "\r\n" (or maybe just "\r", but I don't think so) for notepad to recognize it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.