I need a little help understanding PHP behavior that doesn't make sense to me, in advance for the help.
First of all, is there a difference between using \r\n and \n\r ? I think logically not, but I think there should be a practical reason why they are listed separately in the PHP document. I am currently using \r\n , but sometimes I get a line break, sometimes I don’t do this, they confuse it.
Secondly, I know that if I repeated the information in the browser, I would use the nl2br() function, but I do not display the information in the browser, it is collected and combined into a string, which is then sent via mail() and what’s there, where the visual inconsistency is displayed.
Here is a script, pretty straight forward, nothing scary here:
<?php $browser = $_SERVER['HTTP_USER_AGENT']; $page = $_SERVER['PHP_SELF']; $ip = $_SERVER['REMOTE_ADDR']; $host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $from_page = $_SERVER['HTTP_REFERER']; $time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']); $uri = $_SERVER['SCRIPT_URI']; $to = "***"; $subject = "Visitor Log"; $message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . "Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri; $headers = "From: ***" . "\r\n" . "Reply-To: ***" . "\r\n" . "X-Mailer: PHP/" . phpversion(); mail($to,$subject,$message,$headers); ?>
But, as you can see, I have lines that are added to $message for the explicit readability of the letter I receive. But sometimes letters arrive, and everything goes on a separate line, as it should be, and sometimes not. So I thought that the method of adding a line break is not the best and that I am doing something wrong.
So the question is what should I use in order to get the right line breaks if what I use is wrong, and if it is right, why doesn't it always work? Your help in adding some clarity to this will be greatly appreciated.
If it matters or matters, I am using a Linux 5 server (5.5.28) with Apache.
UPDATED RESPONSE
Thank you for your help, but I ended up fixing it as follows and even added a content type for a good grade (TY @TOM).
<?php $browser = $_SERVER['HTTP_USER_AGENT']; $page = $_SERVER['PHP_SELF']; $ip = $_SERVER['REMOTE_ADDR']; $host = gethostbyaddr($_SERVER['REMOTE_ADDR']); $from_page = $_SERVER['HTTP_REFERER']; $time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']); $uri = $_SERVER['SCRIPT_URI']; $to = "***"; $subject = "Visitor Log"; $message = "IP Address : " . $ip . " ( HOST: " . $host . " )" . "\r\n"; $message .= "Browser : " . $browser . "\r\n"; $message .= "From : " . $from_page . "\r\n"; $message .= "Page : " . $page . "\r\n"; $message .= "Time : " . $time . "\r\n"; $message .= "Script : " . $uri; $headers = "From: ***" . "\r\n"; $headers .= "Reply-To: ***" . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; $headers .= "Content-Type: text/html; charset=\"utf-8\"" . "\r\n"; mail($to,$subject,nl2br($message),nl2br($headers)); ?>
NOTE The new formatting is not part of the solution, it is only for me. The solution abandons the simple one and comes with html and uses the nl2br function to force breaks, not elegant ones, and not what I wanted entirely, but it works.