View Full Version : functions executing twice?
superflyc
12-3-08, 01:29 PM
Getting some very strange behavior.
Our site runs on PHP Fusion. A few months ago, newsletters sent from it started being sent twice. No code was changed, nor was the backend any different, and user behavior is the same.
My users said that fiddling with their browsers seemed to stop this behavior, and while this didn't make sense to me, I was content that this was an acceptable workaround.
Yesterday I was experimenting with some code to insert records into the calendar. All of a sudden, I was getting double entries. This persisted until today. I was experimenting with transactions, but nothing that should cause double entries.
Things fixed themselves after I started adding die(); after various lines to try and determine where the repeat was coming from. I've now removed die(); and the code is the same as it was yesterday when I was experiencing the double entry.
I wonder if this is related to the double newsletter issue. Has anyone else experienced this? It's really, really strange.
I'm not clicking "submit" twice. I'm not refreshing a page and getting a double post. It's odd.
Any ideas?
Thanks!
superflyc
1-6-09, 05:57 PM
Following up on this, still experiencing this. Is anyone else?
In a nutshell:
A db query gets a set of users
this set is looped through
each iteration calls sendmail for that user
Each user is getting two emails, so some part of this process is getting called wtice.
The page that sends this email has two submit buttons:
save
send
Save saves the content of the email to the db and doesn't send. Send... well, send sends the email. :)
My users say that clicking "send" eventually results in a blank page (a timeout or some other PHP error?), and then we have duplicate emails. Their workaround is to click "send", then immediately click "save". This makes things work correctly.
I'm working with support to look at some logs to see what's going on, but does anyone have any ideas here?
I'm sure this isn't a syntax error, but something systemic. It's really frustrating.
Thanks!
Croc Hunter
1-6-09, 06:54 PM
I get the same only double email no info posted to page intermittantly on a script I wrote myself. It's been happening for a few months now. I'm no php Guru and can't see the problem in my code. The 'sometimes it does sometimes it doesn't' in my case makes it very hard to track down. How fat is your page? Mine is like 226Kb and growing. Can you give us a link to your page having most trouble?
superflyc
1-7-09, 12:51 PM
Sorry to hear you're having problems, too. Also, apologies for the lengthy post - I want this fixed for the both of us and anyone else.
The page itself is password protected and sends out emails to about 400 people, but it's about a stock newsletter infusion on a PHP Fusion app. That being said, with all of the includes being run, the total page size is ~50kb of PHP code and some inline HTML.
What's killing me is the difficulty testing this. I mean, I can pretty much know that duplicate emails will be sent if I send to all of my members. I have a dev subdomain set up to play with, and if it's just me or a few folks, only one email gets sent. Why the technique of submitting the form once as "send" and again as "save" before that first request has been responded to is beyond me.
I Google'd and have seen other people experiencing duplicate email issues with PHP, but am at work and will have to look at it later.
Here are some of my other thoughts based on my testing, though:
if I disable the line that calls the sendmail function, and just dump the results of the query getting my recipients, I get a list that is correct without any duplicates. That would imply that it's the sendmail function that is causing problems.
Looking at this sendmail function, it's a wrapper for the PHP mail() function. It sets a few variables, but then passes them to mail().
Because of this, I suspect that it's mail() that's causing issues. In my case, since it happens when called multiple times (as in sending personalized emails to a group of ~400), I think it is load dependent.
It would be interesting to see if its load dependent in your case, too.
One thing we both could do is to just iterate n times over the same email address and see if we can consistently reproduce this issue based on load.
It's like the PHP script starts running, perhaps times out, then starts running again in a separate thread and we get double exectution.
Of course, viewing the application logs when this happens would be best. If we're both getting blank or incomplete pages, I can almost guarantee that an error is occurring, and if the output isn't going to the browser, hopefully it's being logged and we can troubleshoot further.
I don't know if I have access to my logs. Do you have to yours?
Have you tried writing to a log file in the sendmail function?
Log all the variables and the time.
superflyc
1-8-09, 02:55 PM
Excellent suggestion, Eric (sorry, couldn't resist with your handle). :)
I'm wondering: do users (me) have access to the logs? Seems like this should be happening already, and I'd rather not reinvent the wheel if I don't have to.
Failing that, I'm a PHP newb. Is there any boilerplate code that can catch an error and write the error variables to a file? I won't be offended by a RTFM suggestion, but if there's something already out there that does this, I'd be hugely appreciative.
thanks!
Chris
superflyc
1-8-09, 03:12 PM
I should check my support tickets before responding - okay, so I viewed the error log via OPS. While I'm glad it's there, there are no timestamps for each line, which makes debugging and identifying problems VERY difficult.
Is that a setting I can change, or is this something on Powweb's end?
In the absence of timestamps, I searched for "mail" and "newsletter_admin". The former should grab references to the include that contains the actual mail() function, and the latter is the page called by the browser that includes the mail page.
No hits on the former, but plenty of references to the latter. The error messages I got were PHP warnings saying that headers couldn't be changed because a response had been sent already. I also got a boatload of out of memory errors throughout the log, and some of those directly followed some of the newsletter admin references.
Does this ring any bells with anyone? Are these error messages just normal noise?
btw, I very much appreciate the responses here, and also thank you to support with my ticket in pointing out where the logs can be accessed.
I very much appreciate the responses here, and also thank you to support with my ticket in pointing out where the logs can be accessed.
The purpose of these forums is to help. Offering thank you's to those working on tickets won't be seen by the 'workers' as staff members only can respond to tickets and most don't view these forums. A better way, once the issue is resolved, is to include a 'thank you' in the OPS ticketing system as part of the flow of the ticket on the issue.
superflyc
1-8-09, 03:40 PM
another good suggestion, and thanks for the heads up. :) I'm all about thanking help in every channel possible. Years of working in the service industry followed by some time in the trenches doing tech support has only reinforced what common sense and courtesy dictate as sensible behavior. :)
You need to add something like this just before the mail( .. ) line.
$variables should contain a string made up of all the variables in the mail function.
$handle = fopen( __FILE__."log" 'a' );
fwrite( $handle , date( 'r' ) ." " . $variables . "\n" );
fclose( $handle );
superflyc
1-8-09, 04:34 PM
perfect. Thanks!
I'll wrap up the call to the mail() function in a try block and also add the above to the catch block along with a dump of the error message.
Doing that assumes that mail() is failing and causing the problem.
You need to log every call to mail, as you are getting double emails.
vBulletin v3.6.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.