We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19388
    • 297 Posts
    Hi,
    We have an strange problem and I want to launch this question to the air, to see if anyone has experienced some simmilar situations. We have developed a snippet so simple that creates a file with a unique name. We have tested it in a clean Evo 1.0.4 and everthing was OK. But when we deployed it to the production site it is like the snippet is being executed three times. Three files are created instead one, but the output message is shown only one time instead of three.
    There are no redirections.
    There are no error messages.
    We have cleaned cache.
    We have disabled plugins ( PHx, FileDownload and others ).
    We have limited and reduced the snippet to a few lines.
    We have changed snippet name.
    We have executed snippet in other page. ( it was originally beign executed in a protected page ).
    We have tested all the possible cached/uncached combinations.

    Last times we are experiencing some "x-files" here! sad

    Any ideas?
      • 3749
      • 24,544 Posts
      Can you post the snippet code?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19388
        • 297 Posts
        This was the first code:
        <?php
        //This script will receive XFDF form data and store it in a folder for each user
        
        //Check if usser is logged in and if is web user
        $usuario=$modx->userLoggedIn();
        if($usuario==true && $usuario['usertype']=='web') { 
        	//We create an specific folder for the user if it is not created.
        	$id = $usuario['id'];
        	chdir('/home/MYSITE/xfdf/');
        	if (!is_dir($id)) mkdir($id);
        	chdir($id);
        
        	//Read XFDF Data
        	$XFDFData = file_get_contents('php://input');
        	$xfdf = new SimpleXMLElement($XFDFData);
        
        	//Create unique filename
        	$md5 = md5(mktime() . microtime());
        	//Save data as XML
        	$xfdf->asXML($md5 . '.xfdf');
        	
        	//Output
        	echo 'Data saved';
        
        //If not authorized...
        } else {
        	echo 'Not authorized';
        	//$modx->sendForward(37);
        }
        ?>


        Then we made some tests and changes in saving data, from:
        	//Save data as XML
        	$xfdf->asXML($md5 . '.xfdf');

        To fread, file_put_contents, etc. With same result.
        For testing purposes, we reduced the code to 3/4 lines where we only was creating a file an putting some content into. Same result: 3 files were being created.

        Now, we had to made a hack and use session variables to avoid this problem:

        <?php
        //This script will receive XFDF form data and store it in a folder for each user
        
        //Check if usser is logged in and if is web user
        $usuario=$modx->userLoggedIn();
        if($usuario==true && $usuario['usertype']=='web') { 
        	//We create an specific folder for the user if it is not created.
        	$id = $usuario['id'];
        	chdir('/home/MYSITE/xfdf/');
        	if (!is_dir($id)) mkdir($id);
        	chdir($id);
        
        	//Read XFDF Data
        	$XFDFData = file_get_contents('php://input');
        	$xfdf = new SimpleXMLElement($XFDFData);
        
        	//Check if user has been granted. This session variable is created in a plugin executed before this snippet. 
        	if ($_SESSION['submitGranted'] && $_SESSION['submitGranted'] == 1) {
        		//Create unique filename. 
        		$md5 = md5(mktime() . microtime());
        		//Save data as XML
        		$xfdf->asXML($md5 . '.xfdf');
        		$_SESSION['submitGranted'] = $md5 . '.xfdf';
        	}
        	
        	//Output
        	echo 'Data saved';
        
        //If not authorized...
        } else {
        	echo 'Not authorized';
        	//$modx->sendForward(37);
        }
        ?>

          • 25037
          • 32 Posts
          I’m interested to see your .htaccess file.

          A mistake in the .htaccess can cause your site to be executed more than once. You wouldn’t know unless you are serialzing some data (writing to file or inserting data for example).
            • 19388
            • 297 Posts
            In the tests I did, I put a timestamp into files. 3 files were created with different timestamps. Only the last snippet execution was printed to screen and generating an output. I know it because it showed the last timestamp. There was no redirections.

            .htaccess
            # For full documentation and other suggested options, please see
            # http://svn.modxcms.com/docs/display/MODx096/Friendly+URL+Solutions
            # including for unexpected logouts in multi-server/cloud environments
            # and especially for the first three commented out rules
            
            #php_flag register_globals Off
            #AddDefaultCharset utf-8
            #php_value date.timezone Europe/Moscow
            
            Options +FollowSymlinks
            RewriteEngine On
            RewriteBase /
            
            # Fix Apache internal dummy connections from breaking [(site_url)] cache
            RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
            RewriteRule .* - [F,L]
            
            # Rewrite domain.com -> www.domain.com -- used with SEO Strict URLs plugin
            #RewriteCond %{HTTP_HOST} .
            #RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
            #RewriteRule (.*) http://www.example.com/$1 [R=301,L]
            
            # Exclude /assets and /manager directories and images from rewrite rules
            RewriteRule ^(manager|assets)/*$ - [L]
            RewriteRule \.(jpg|jpeg|png|gif|ico)$ - [L]
            
            # For Friendly URLs
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
            
            # Reduce server overhead by enabling output compression if supported.
            #php_flag zlib.output_compression On
            #php_value zlib.output_compression_level 5
              • 19388
              • 297 Posts
              We added some funcionallity like sending emails and same result. 3 emails are received. We are "hacking" with session variables.
                • 19388
                • 297 Posts
                We finally ignored and patched this in a bad way sad But we have more information. We declared a funtion in other snippet and had a redeclare error. It does mean that the snippet is beign executed in the page 3 times itself. It is like if we were calling: [[snippet]] [[snippet]] [[snippet]] instead of only [[snippet]]

                So strange sad

                Fatal error: Cannot redeclare newest() (previously declared in /home/diasan/public_html/manager/includes/document.parser.class.inc.php(770) : eval()’d code:29) in/home/diasan/public_html/manager/includes/document.parser.class.inc.php(770) : eval()’d code on line 31
                  • 6228
                  • 249 Posts
                  Well one eloquent way of treating the symptom (without fully addressing the multiple-execution problem) is using something like so:

                  if (!defined('SNIPPET_XFDF')) {
                     ... code goes here ...
                     define('SNIPPET_XFDF', true);
                  }
                    lo9on.com

                    MODx Evolution/Revolution | Remote Desktop Training | Development
                    • 19388
                    • 297 Posts
                    We are doing something simmilar but using session variables, but thanks for your help. I like the way you do it smiley
                    What I’d like to solve is the multiple execution that is causing us a lot of headaches sad
                      • 14376
                      • 1 Posts
                      Well, this is my first message. I’ll try in my poor english. Thanks for helping.

                      I work with mIDO.

                      Quote from: mIDO at Nov 18, 2010, 09:19 AM

                      We finally ignored and patched this in a bad way sad But we have more information. We declared a funtion in other snippet and had a redeclare error. It does mean that the snippet is beign executed in the page 3 times itself. It is like if we were calling: [[snippet]] [[snippet]] [[snippet]] instead of only [[snippet]]

                      So strange sad

                      Fatal error: Cannot redeclare newest() (previously declared in /home/diasan/public_html/manager/includes/document.parser.class.inc.php(770) : eval()’d code:29) in/home/diasan/public_html/manager/includes/document.parser.class.inc.php(770) : eval()’d code on line 31

                      in fact it’s not really like the document calling three times the snippet. The document only gets the output of the third snippet. So we have to use session variables to store the output of the first calling and don’t allow changes in the next callings.

                      Quote from: cyclissmo at Nov 18, 2010, 09:36 AM

                      Well one eloquent way of treating the symptom (without fully addressing the multiple-execution problem) is using something like so:

                      if (!defined('SNIPPET_XFDF')) {
                         ... code goes here ...
                         define('SNIPPET_XFDF', true);
                      }


                      If we do in this way, we lost the output.