By fossie on
Hi everybody,
I don't know if this issue is 'an issue', but it seems that php code that I'm using in a page (input format php code) is executed twice. Drupal: 4.6.5. I've just tested with a clean installation, no extra modules configured, default theme and the result is the same.
Example to test:
$admin = "info@admin.com";
$subj = "contact...";
$cont = "test content body mail";
$email = "info@admin.com";
mail($admin, $ubj, $cont, "From: $email");
print '<p>Mail ok</p>';
and when I load the page, I recieve two emails.
Can anybody help me with this?
TIA,
Bart
Comments
yeah, that can happen
You can dodge it programmatically if you want, by setting an $already_sent sorta flag.
What's probably happening is the code is getting evaluated each time you preview it, and each time the teaser is prepared, as well as each time you 'view' it.
inline PHP is better for rendering stuff rather than doing actual actions.
.dan.
http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Ok, doing actions
Probably that's right about php is good to render iso doing actions, but what's a workaround? What's the good way to perform actions?
TIA
Bart
Same here
I have a problem with duplicate 2 emails inside drupal. Thought it must have been the server, but on reading this thread it starts to make sense that it is drupal...
Did you test this code
Hi,
Did you test the code above to see if the problem occurs?
- create a page
- input type: php
- paste the code (and change the email addresses)
Log out and view the created page.
I allready got an answer (bug reporting) that someone tried it and he got only one email. Maybe he uses another drupal version or is working with another php version....
Bart
Yes
just tested your code on my setup and i got duplicate emails. So you are not alone! I am using Drupal 4.6.5, 2005-12-12 on php 4.4.1 with Mysql 4.0.25-standard and apache 1.3.34 (Unix)
How can we set up this alreadysent variable to avoid this?
no idea
I don't know, if I knew, maybe I didn't ask this question in the first place :-)
Hopefully someone can help us,
Bart
Well...
..my question was kind of directed at dan, as he suggested it! :) But open to anyone to chip in...
my guess
.. is that you will get duplicate emails just by following those steps - as submitting the changes will send you directly to the 'view' page.
Which will neccessarily evaluate the code you just put in.
Each time you view (preview even) code constructed like that, it will run.
so you gotta be clever enough to detect what context you are being called in (non-trivial)
or pass yourself a flag or an argument that says 'do this now'.
Just triggering an action on page-view is going to cause you grief, especially during development & testing. Add an 'op' flag or something to the page request to really be sure you want the page to load and run, not just display.
By 'op' flag I mean send a script argument like "?op=send" to your URL. op just happens to be the vernacular within Drupal for "operation" ... or "action"
Thus you need to check in your script for
You follow?
This will mean your links to this page (I can't imagine what you are trying to do) can work, while your editing of this page will NOT trigger the action.
.dan.
http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thx for the explanation
Hi Dan,
Thx for your suggestion. What I want to do:
Domain check (name + tld), if available for registration, go to page where people can fill in name, address, ... necessary to create a profile.
I first saw the problem with this version of drupal (4.6.5) and I was wondering if I was doing something wrong. The profile was created twice, just the last profile number showed up (of course).
I tested the same (email send php code from above) in Drupal 4.6.3 same server, same settings and the email was just sent once. Probably there is really something wrong/changed in the 4.6.5 release.
I'll let you know if the workaround works, but I do think this parameter - op - like the others will be available for the two executions.
Regards,
Bart
Hi Dan
I don't completely follow what you have written above, (and i am concerned with the execution of the page rather than editing etc) but i was thinking, wouldn't it be possible to add an if clause around the mail part of the script:
I appreciate this is probably completely wrong, but the idea of testing to see if the email has sent is surely a possibel path.
In my case, i am starting to think it is the fact that this page is executed for the teaser and then executed a second time for the whole page (what you mentioned earlier in this thread)
I actually had this script converted from asp for me by a php coder, and he left it with me in the end sending dupes, saying that on his system it sent only one!
Problem seems to be fixed
Hi everybody,
I think the nodewords modules caused the problem, because when I disable it, the mail is only sent once!
I looked at the modules included in my install and for the 4.6.5 I added it and therefor the problem still existed with 'a clean install', but the clean install wasn't clean, sorry!
If anybody encounters the same issue, can you check if the nodewords module is installed? By disabling it, probably you'll loose your allready inserted keyword data, I'm not sure about that.
HTH,
Bart
right both times
Yes, the $already_sent flag I suggested would have been one way to do it. It's fine, Just feels a bit clunky code-structure-wise.
Using an $op argument is supposed to make your process flow clearer to follow, not add a special-case clause in the middle of it.
And I think you are right about the teaser - you realize that nodewords retrieves the teaser as the description, right?
And your code is in the beginning of the node text.
Thus, your code is evaluated twice.
I guess the whole thing may have been avoided by putting one of those break comments above your code in the content.
But you see why trying to develop real code in a php textarea is a bit of a pain.
.dan.
http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
break
Right on Dan, just needed the break comment inside the php code:
<!--break-->No more duplicates! Cheers once again.
re: break
How do you go about getting Drupal to recognize the break code inside the PHP code? I've tried several incarnations, and whenever the page type is set to "PHP", Drupal ignores my break tag.
Most recently I tried this:
and this (not expecting it to work, but still tried it)
Is there another option I'm not thinking of?
duplication function solution
Here was my solution to the problem.
First off, as others pointed out in the thread, the problem is caused by Drupal taking the PHP code in the body and using it as the teaser. If you have a short bit of code that fires when the page is loaded, this will cause that duplicate code to fire when the page's teaser is displayed on an index page.
Alternatively, if you are using the nodewords module, that self-same teaser text will be used as the default description in the meta tags. As a result, you can end up with duplicate actions, such as email being sent twice, since there are now two instances of the script on the page (one in the meta tag, one in the body of the page). I believe this is also the reason why Drupal will complain about functions being declared twice in custom scripts, though I haven't tested that yet.
My solution is two fold:
1) If you are using nodeword, make sure that all custom PHP pages have meta descriptions associated with them; if you don't include a description, it's going to use the PHP teaser and cause you headaches.
2) To avoid the script firing as part of an index, use one of the methods described in the other posts, or use the excerpt module to override the contents of the teaser. As a result the PHP code will not appear on the index page.
My understanding (based on this thread) is that using the normal "break" comment tag won't work with PHP, though for cases like this it would be nice if it did.
I'm not sure how the solution differs under Drupal 4.7.