Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Code executed twice

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
TeleTeddy
Smarty n00b


Joined: 08 Jul 2010
Posts: 2

PostPosted: Thu Jul 08, 2010 7:42 am    Post subject: Code executed twice Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jul 08, 2010 9:05 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
TeleTeddy
Smarty n00b


Joined: 08 Jul 2010
Posts: 2

PostPosted: Thu Jul 08, 2010 12:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Thu Jul 08, 2010 12:41 pm    Post subject: Re: Code executed twice Reply with quote

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
View user's profile Send private message
rondvorak
Smarty n00b


Joined: 29 Sep 2011
Posts: 1

PostPosted: Thu Sep 29, 2011 3:23 am    Post subject: workaround Reply with quote

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
View user's profile Send private message
foranyone
Smarty n00b


Joined: 16 Jul 2012
Posts: 2

PostPosted: Mon Jul 16, 2012 11:19 am    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Jul 16, 2012 6:31 pm    Post subject: Reply with quote

See my answer at stackoverflow
Back to top
View user's profile Send private message
foranyone
Smarty n00b


Joined: 16 Jul 2012
Posts: 2

PostPosted: Tue Jul 17, 2012 6:47 am    Post subject: Reply with quote

U.Tews wrote:
See my answer at stackoverflow


unfortunately the problem exists!
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Jul 17, 2012 6:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
jebediah
Smarty Rookie


Joined: 28 Jan 2012
Posts: 16

PostPosted: Sun Aug 05, 2012 12:17 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP