<p>I have a strange problem with my cake (cake_1.2.0.7296-rc2).
My start()-action runs twice, under certain circumstances, even though only one request is made.</p>

<p>The triggers seem to be :
- loading an object like: <code>$this-&gt;Questionnaire-&gt;read(null, $questionnaire_id);</code>
- accessing $this-data </p>

<p>If I disable the call to <code>loadAvertisement()</code> from the <code>start()</code>-action, this does not happen.
If I disable the two calls inside <code>loadAdvertisement():</code></p>

<pre><code>$questionnaire = $this-&gt;Questionnaire-&gt;read(null, $questionnaire_id);
$question = $this-&gt;Questionnaire-&gt;Question-&gt;read(null, $question_id);
</code></pre>

<p>... then it doesn't happen either.</p>

<p>Why?</p>

<p>See my code below, the Controller is "questionnaires_controller".</p>

<pre><code>function checkValidQuestionnaire($id)
{
    $this-&gt;layout = 'questionnaire_frontend_layout';

    if (!$id)
    {
        $id = $this-&gt;Session-&gt;read('Questionnaire.id');        
    }

    if ($id) 
    {
        $this-&gt;data = $this-&gt;Questionnaire-&gt;read(null, $id);

        //echo "from ".$questionnaire['Questionnaire']['validFrom']." ".date("y.m.d");
        //echo " - to ".$questionnaire['Questionnaire']['validTo']." ".date("y.m.d");


        if ($this-&gt;data['Questionnaire']['isPublished'] != 1 
            //|| $this-&gt;data['Questionnaire']['validTo'] &lt; date("y.m.d")
            //|| $this-&gt;data['Questionnaire']['validTo'] &lt; date("y.m.d")
            )
        {
            $id = 0;
            $this-&gt;flash(__('Ungültiges Quiz. Weiter zum Archiv...', true), array('action'=&gt;'archive'));
        }
    }
    else
    {
        $this-&gt;flash(__('Invalid Questionnaire', true), array('action'=&gt;'intro'));
    }

    return $id;
}



function start($id = null) {
    $this-&gt;log("start");

    $id = $this-&gt;checkValidQuestionnaire($id);

    //$questionnaire = $this-&gt;Questionnaire-&gt;read(null, $id);
    $this-&gt;set('questionnaire', $this-&gt;data);

    // reset flow-controlling session vars
    $this-&gt;Session-&gt;write('Questionnaire',array('id' =&gt; $id));
    $this-&gt;Session-&gt;write('Questionnaire'.$id.'currQuestion', null);
    $this-&gt;Session-&gt;write('Questionnaire'.$id.'lastAnsweredQuestion', null);
    $this-&gt;Session-&gt;write('Questionnaire'.$id.'correctAnswersNum', null);

    $this-&gt;loadAdvertisement($id, 0);

    $this-&gt;Session-&gt;write('Questionnaire'.$id.'previewMode', $this-&gt;params['named']['preview_mode']);

    if (!$this-&gt;Session-&gt;read('Questionnaire'.$id.'previewMode'))
    {
        $questionnaire['Questionnaire']['participiantStartCount']++;
        $this-&gt;Questionnaire-&gt;save($questionnaire);
    }        

}




function loadAdvertisement($questionnaire_id, $question_id)
{

    //$questionnaire = array();
    $questionnaire = $this-&gt;Questionnaire-&gt;read(null, $questionnaire_id);

    //$question = array();
    $question = $this-&gt;Questionnaire-&gt;Question-&gt;read(null, $question_id);

    if (isset($question['Question']['advertisement_id']) &amp;&amp; $question['Question']['advertisement_id'] &gt; 0)
    {
        $this-&gt;set('advertisement', $this-&gt;Questionnaire-&gt;Question-&gt;Advertisement-&gt;read(null, $question['Question']['advertisement_id']));
    }
    else if (isset($questionnaire['Questionnaire']['advertisement_id']) &amp;&amp; $questionnaire['Questionnaire']['advertisement_id'] &gt; 0)
    {
        $this-&gt;set('advertisement', $this-&gt;Questionnaire-&gt;Question-&gt;Advertisement-&gt;read(null, $questionnaire['Questionnaire']['advertisement_id']));
    }

}
</code></pre>

<p>I really don't understand this... it don't think it's meant to be this way.
Any help would be greatly appreciated! :)</p>

<p>Regards,
Stu</p>

[{'answer_id': 64176, 'author': 'eelco', 'author_id': 8293, 'author_profile': 'https://Stackoverflow.com/users/8293', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to try and find out where it comes from using the debug_print_backtrace() function. (<a href="http://nl.php.net/manual/en/function.debug-print-backtrace.php" rel="nofollow noreferrer">http://nl.php.net/manual/en/function.debug-print-backtrace.php</a></p>\n'}, {'answer_id': 353493, 'author': 'Martz', 'author_id': 44576, 'author_profile': 'https://Stackoverflow.com/users/44576', 'pm_score': 4, 'selected': False, 'text': '<p>Check your layout for non-existent links, for example a misconfigured link to favicon.ico will cause the controller action to be triggered for a second time. Make sure favicon.ico points towards the webroot rather than the local directory, or else requests will be generated for /controller/action/favicon.ico rather than /favicon.ico - and thus trigger your action.</p>\n\n<p>This can also happen with images, stylesheets and javascript includes.</p>\n\n<p>To counter check the $id is an int, then check to ensure $id exists as a primary key in the database before progressing on to any functionality.</p>\n'}, {'answer_id': 943508, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Had the same problem, with a certain action randomly running 2-3 times. I tracked down two causes:</p>\n\n<ul>\n<li><p>Firefox add-on Yslow was set to load automatically from it's Preferences, causing pages to reload when using F5 (not when loading the page from the browser's address bar and pressing Enter).</p></li>\n<li><p>I had a faulty css style declaration within the options of a $html->link(); in some cases it would end up as background-image: url('');, which caused a rerun also. Setting the style for the link to background-image: none; when no image was available fixed things for me.</p></li>\n</ul>\n\n<p>Hope this helps. I know this is quite an old post, but as it comes up pretty high in Google when searching for this problem, I thought it might help others by still posting.</p>\n\n<p>Good luck</p>\n\n<p>Jeroen den Haan</p>\n"}, {'answer_id': 944647, 'author': 'unicorn_crack', 'author_id': 100765, 'author_profile': 'https://Stackoverflow.com/users/100765', 'pm_score': 1, 'selected': False, 'text': "<p>I had a problem like this last week.</p>\n\n<p>Two possible reasons</p>\n\n<ul>\n<li>Faulty routes (DO check your routes configuration)</li>\n<li>Faulty AppController. I add loads of stuff into AppController, especially to beforeFilter() and beforeRender() so you might want to check those out also.</li>\n</ul>\n\n<p>One more thing, are where are you setting the Questioneer.id in your Session? Perhaps that's the problem?</p>\n"}, {'answer_id': 1564586, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>For me it was a JS issue.</p>\n\n<p>Take care of wrap function with jQuery that re-execute JS in wrapped content! </p>\n'}, {'answer_id': 12165162, 'author': 'Aloe', 'author_id': 1631160, 'author_profile': 'https://Stackoverflow.com/users/1631160', 'pm_score': 1, 'selected': False, 'text': '<p>Yes, it occurs when there is a broken link in the web page. Each browser deals with it variously (Firefox calls it 2x). I tested it, there is no difference in CakePHP v1.3 and v2.2.1. To find out who the culprit is, add this line to the code, and then open the second generated file in you <code>www</code> folder:</p>\n\n<pre><code>file_put_contents("log-" . date("Hms") . ".txt", $this-&gt;params[\'pass\'] ); // CakePHP v1.3 \nfile_put_contents("log-" . date("Hms") . ".txt", $this-&gt;request[\'pass\'] ); //CakePHP v2.2.1\n</code></pre>\n\n<p>PS: First I blame jQuery for it. But in the end it was forgotten image for AJAX loading in 3rd part script. </p>\n'}, {'answer_id': 17543801, 'author': 'Vijay Kumbhar', 'author_id': 447509, 'author_profile': 'https://Stackoverflow.com/users/447509', 'pm_score': 0, 'selected': False, 'text': "<p>I had the same problem in chrome, I disabled my 'HTML Validator' add on. Which was loading the page twice</p>\n"}, {'answer_id': 28416302, 'author': 'jtrumbull', 'author_id': 3112730, 'author_profile': 'https://Stackoverflow.com/users/3112730', 'pm_score': 0, 'selected': False, 'text': '<p>I was having a similar issue, the problem seemed to be isolated to case-insensitivity on the endpoint.\n <br/><br/> ie: <br/>\n<a href="http://server/Questionnaires/loadAvertisement" rel="nofollow">http://server/Questionnaires/loadAvertisement</a> -vs- <br/>\n<a href="http://server/questionnaires/loadavertisement" rel="nofollow">http://server/questionnaires/loadavertisement</a></p>\n\n<p>When calling the proper-cased endpoint, the method ran once -whereas the lower-cased ran twice. The problem was occurring sporadically -happening on one controller, but not on another (essentially the same logic, no additional components etc.). I couldn\'t confirm, but believe the fault to be of the browser -not the CakePHP itself.</p>\n\n<p>My workaround was assuring that every endpoint link was proper-cased. To go even further, I added common case-variants to the Route\'s configuration:\n<br/>\n<br/>\napp/config/routes.php</p>\n\n<pre><code>&lt;?php\n// other routes..\n$instructions = [\'controller\'=&gt;\'Questionnaires\',\'action\'=&gt;\'loadAvertisement\'];\nRouter::connect(\'/questionnaires/loadavertisement\', $instructions);\nRouter::connect(\'/QUESTIONNARIES/LOADADVERTISEMENT\', $instructions);\n// ..etc\n</code></pre>\n'}, {'answer_id': 29075468, 'author': 'gdm', 'author_id': 778508, 'author_profile': 'https://Stackoverflow.com/users/778508', 'pm_score': 0, 'selected': False, 'text': '<p>If you miss <code>&lt;something&gt;</code>, for example a View, Cake will trigger a missing <code>&lt;something&gt;</code> error and it will try to render its <code>Error View</code>. Therefore, <code>AppController</code> will be called twice. If you resolve the missing  issue, <code>AppController</code> is called once.</p>\n'}]
['https://Stackoverflow.com/questions/63881', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']