2

I am wondering why php does certain things twice, instead of once, when a certain meta tag is in the html portion of the file and the file is browsed by Firefox.

The code is like this:

<?  /*...normal php code, including writing record to MySQL...*/
send('dan@example.com',$subject,$body);    
?>
<!DOCTYPE html><html>
<!--PROBLEM on next line--> 
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head>
<title><?= $thisPage?></title>
<link href="<?= $cssURL?>css.freedom-and-purpose.css" rel="stylesheet" type="text/css" media="screen, projection"  />
<?
include $dataPath   . 'data.php';
?>

The result is TWO records written the database and TWO emails sent, whenever the page is called by FIREFOX. IE and Chrome not producing the problem.

There is a lot of other code in the program, but the reason I showed the portion above is that removing the line that starts with <META...

solves the problem. enter image description here

That meta tag is in there because one of the packages I run included it in their code sample.

So, what is that meta tag causes php to double do on DB-writes? And same thing on sending email?

DanAllen
  • 352
  • 1
  • 4
  • 15
  • Could just be the bad formatting (missing quotes around `Content-Type`) forcing Firefox to reload the page in quirks mode or something. I'm not sure but try using `` or the more modern `` – Phil Feb 13 '15 at 03:37
  • why not just leave it out? Works fine without it? Sorry if that is a stupid question – DanAllen Feb 13 '15 at 03:39
  • If you ever want to show unicode characters on your site, you'll probably want it there. See http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Phil Feb 13 '15 at 03:39
  • Phil, the change you suggested fixed the problem! Boom! – DanAllen Feb 13 '15 at 03:41
  • off topic, but can you give an example of a unicode character I might want to show that won't work if this is missing? – DanAllen Feb 13 '15 at 03:42
  • 確かに、方法については、この. It depends on the content-type that your server sends for the file as well but in my experience, it's always good to have the character set defined in the document. StackOverflow for example, has it in the server response header so doesn't need it in the HTML. – Phil Feb 13 '15 at 03:44
  • phil, if you post an answer I will accept! – DanAllen Feb 13 '15 at 03:54
  • Nah, just vote to close as a typo. Don't delete it though, it's interesting that Firefox loads the page double due a bad meta tag and handy information for others to find – Phil Feb 13 '15 at 04:00
  • Found a reported bug here though it's very old ~ https://bugzilla.mozilla.org/show_bug.cgi?id=109076 – Phil Feb 13 '15 at 04:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70830/discussion-between-danallen-and-phil). – DanAllen Feb 13 '15 at 05:45
  • Unfortunately, the problem has returned. Using , and this all generate duplicate email and db records when the page is hit with FireFox. So there must be another error in the html causing this? – DanAllen Feb 13 '15 at 22:14
  • 1
    PHP is not responsible for the undesired behaviour. The data is inserted twice in the database and two emails are sent because the script is requested twice from the network. The `META` element doesn't tell FF to request the page again. You better check the log files of your server to find out what page was requested (if you use URL rewriting it's possible that you misconfigured it and it also handles requests that it shouldn't handle; for example, `/favicon.ico` as [this answer](http://stackoverflow.com/a/29172030/4265352) suggests). – axiac Mar 20 '15 at 17:21

2 Answers2

1

Chances are this is actually a request for favicon.ico being caught by your main PHP file. Putting an empty file in favicon.ico or preventing your PHP from handling that URL should do the trick

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

enter code hereI would suggest you go through your code in some details and check its formatting.

Phil mentioned the meta tag about which I agree with. His suggestion of <meta charset="utf-8"> would be my preference.

Secondly the line sending the email looks odd. Single quotes aren't an option in php for data replacement, so the line send('dan@example.com','$subject','$body); would result in an email with the subject "$subject" and body just "$body".

Additionally send('dan@example.com','$subject','$body); appears to be missing a quote after $body.

I would advise you to move away from short php tags for opening and closing chunks of php <? ?> and get in the habit of <?php for clarity and to ensure the server you're using processes the code correctly.

Finally, I hope include $dataPath . 'data.php'; adds a </head> and a <body> to the html, as you're currently missing those too.

Harry B
  • 2,864
  • 1
  • 24
  • 44
  • Thank you, Harry, for going through this. I took your advice, and went through the code I posted. The first line bombs, but It works if comment tags surround it, so I made that change. The send command was not typed correctly, so I fixed that too. Regarding the email content, the purpose of that email was to test if mail would be sent more than once, and for that purpose, I was thinking leaving stuff out not relevant to the test was ok. – DanAllen Mar 20 '15 at 16:38
  • I iike php short tags. Is there another programming language using as a delimiter? The code I am working on runs on my servers only. I always run short tags enabled. Maybe the php committee needs to disallow short tags. Since they are allowed, I figure they are there to use. I guess I am asking, where is the lack of clarity in the short tag.? – DanAllen Mar 20 '15 at 16:45
  • The immediate example I would offer is xml where short tags get super confusing. Also when switching between HTML and php the clarity that the longer version brings is invaluable when coding on your own or as part of a team. There is no benefit in short tags (although = is an exception) other than saving you 3 characters. – Harry B Mar 21 '15 at 17:41