View previous topic :: View next topic |
Author |
Message |
TeleTeddy Smarty n00b
Joined: 08 Jul 2010 Posts: 2
|
Posted: Thu Jul 08, 2010 7:42 am Post subject: Code executed twice |
|
|
I hit some strange behaviour today.
Using Smarty, I had the problem that all my php code (entire file) was executed twice, while only the second
execution did show up in the browser. I played around with the die() command and found out that everything
worked well until the $smarty_class_handler->display(...) command was executed.
Yet, the code didn't ALWAYS execute twice, but depending on which IF-path was taken in the code. Creating and
discarding various theories, I finally noticed, that the whole thing was triggered, if there was too much (text)
output prior to the smarty->display command. My code displayed various lines of debug information, and these
seemed to trigger the 'double-execution'. Shortening the debug information (or skipping them completely)
resolved the problem...
Of course, if you use Smarty as it is intended, no text or output should be generated via php code, but this
is somewhat annoying in the coding/debugging stage.
Is this a known issue? Am I right with my conclusions at all? I'd appreciate any kind of feedback.
- - -
Computer Setup:
Smarty version: 2.6.26
PHP: 5.3.0
Webserver: Wampserver 2.0
OS: Windows 7 |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jul 08, 2010 9:05 am Post subject: |
|
|
It is pretty likely that you have an empty img-src- or script-src- or link-href-stylesheet-attribute in your output.
The browser tries to load the image relative to the current URL and makes this fetch your index.php a second time. |
|
Back to top |
|
TeleTeddy Smarty n00b
Joined: 08 Jul 2010 Posts: 2
|
Posted: Thu Jul 08, 2010 12:31 pm Post subject: |
|
|
After some time of testing things got even stranger.
I have no emtpy src whatsoever in my template or anywhere else (though I tested that this indeed leads to the same behaviour). I tested, that I need 1870 characters (i.e. 'x') of text output to trigger the problem (1869 won't do). I tested, that that the simple template of
Code: | <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head> |
will trigger the problem (given enough text output prior to the smarty->display command is provided) when using K-Meleon, while commenting that line out resolves the problem. Using Firefox won't trigger it, yet I already have encountered the problem with Firefox in the past, too (although I can't reproduce the situation right now).
It seems that this problem is really difficult to trigger, as it needs many circumstances, but nonetheless it does exist.
Any ideas what might happen here? |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Thu Jul 08, 2010 12:41 pm Post subject: Re: Code executed twice |
|
|
I don't think there is anything that smarty could do to make your PHP code execute twice. In order for that to happen, the page would have to be requested twice.
I would just put this at the top of your PHP code (if you have email delivery set up).
mail('youremailaddresshere', 'debug info', date('r').' '.print_r($_REQUEST, 1).print_r($_SERVER,1));
Then look at the request vars and also see if you see anything strange in $_SERVER, especially HTTP_REFERER, REQUEST_URI, QUERY_STRING |
|
Back to top |
|
rondvorak Smarty n00b
Joined: 29 Sep 2011 Posts: 1
|
Posted: Thu Sep 29, 2011 3:23 am Post subject: workaround |
|
|
I had the same problem, the following code executed twice:
if (isset($_POST['send_email'])) {
mail( ... );
}
I fixed it by unsetting the condition that caused the mail() function to be executed(after first execution). The new code is:
if ($_POST['send_email']) {
mail( ... );
unset($_POST['send_email']);
} |
|
Back to top |
|
foranyone Smarty n00b
Joined: 16 Jul 2012 Posts: 2
|
Posted: Mon Jul 16, 2012 11:19 am Post subject: |
|
|
I have the same problem!
my code executes 4 or 5 times!!!
I am really confused!
I've been searching the internet since 2days ago but I cant find any answer.
when I remove this line my code works correctly
$q=$smarty -> fetch('index.tpl');
echo($q);
======OR====
$smarty ->display('index.tpl');
The same problem at Stack Overflow:
http://stackoverflow.com/questions/11501621/sql-queries-execute-more-than-a-time-with-smarty |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jul 16, 2012 6:31 pm Post subject: |
|
|
See my answer at stackoverflow |
|
Back to top |
|
foranyone Smarty n00b
Joined: 16 Jul 2012 Posts: 2
|
Posted: Tue Jul 17, 2012 6:47 am Post subject: |
|
|
U.Tews wrote: | See my answer at stackoverflow |
unfortunately the problem exists! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 17, 2012 6:20 pm Post subject: |
|
|
The problem is solved. (see stackoverflow).
It was cause by a problem like this
Quote: | A Common mistake is to have an empty img-tag or script-tag in your html output - this makes the browser re-request the current page. |
|
|
Back to top |
|
jebediah Smarty Rookie
Joined: 28 Jan 2012 Posts: 16
|
Posted: Sun Aug 05, 2012 12:17 pm Post subject: |
|
|
Hi there,
also, some browsers trigger a request to retrieve the favicon that's displayed in the address bar/on the tab pages.
Depending on you web-server URL matching/rewrite and caching rules your index template might be called:
- one time only for the first request (caching enabled for te retured output)
- every single request (no caching)
So depending on your setup, there can be multiple requests:
- one time to get the content for the current URL
- one time for the favicon
Bests,
Jeb |
|
Back to top |
|
|