12 Replies Latest reply on Mar 30, 2010 12:02 PM by Matt Le Fevre

    Hits counter. HELP :'(

    chandly9

      Hello,

       

      I will get on my website a hits counter. I would like to increment a number in a textField (MXML) which "id" is "textfieldhits" everytime someone is connected to the website. Is it possible to do this in actionscript ?

       

      To resume, I would like to realise a hits counter in actionscript, which appears in a TextField or something else in MXML

       

      Is it possible ?

       

      Thank you very much for helpings

       

      Bye !

        • 1. Re: Hits counter. HELP :'(
          Matt Le Fevre Level 4

          You cannot create a hit counter from just actionscript, you'll need to access a serverside script to increment the hitcounter file for you, be it javascript or php or whatever.

           

          I've told and shown you how to do it in PHP, if you want to do it in javascript you'll have to store the number of hits in a variable and return them from javascript to flex using an external interface call.

          1 person found this helpful
          • 2. Re: Hits counter. HELP :'(
            chandly9 Level 1

            Yes, I know but as I'm a newbie... lol

             

            I don't understand the tutorial. In fact I made a php script. Here it is :

             

            <?php
            $fp = fopen("compteur.txt","r+");
            $nbvisites = fgets($fp,10);
            if ($nbvisites=="") $nbvisites = 0;
            $nbvisites++;
            fseek($fp,0);
            fputs($fp,$nbvisites);
            fclose($fp);
            echo "$nbvisites";
            ?>

             

            It is in a "test.php" in my src folder.

             

            I have no idea about getting back this $nbvisites in a Flex textField in Actionscript.

             

            I know that I have to get back $nbvisites in an actionscript variable (but how ?) and to put it in a textField in Flex (but how ?)

             

            Thanks for your patience

            • 3. Re: Hits counter. HELP :'(
              Matt Le Fevre Level 4

              chandly9 wrote:

               

              Yes, I know but as I'm a newbie... lol

               

              I don't understand the tutorial. In fact I made a php script. Here it is :

               

              <?php
              $fp = fopen("compteur.txt","r+");
              $nbvisites = fgets($fp,10);
              if ($nbvisites=="") $nbvisites = 0;
              $nbvisites++;
              fseek($fp,0);
              fputs($fp,$nbvisites);
              fclose($fp);
              echo "$nbvisites";
              ?>

               

              It is in a "test.php" in my src folder.

               

              I have no idea about getting back this $nbvisites in a Flex textField in Actionscript.

               

              I know that I have to get back $nbvisites in an actionscript variable (but how ?) and to put it in a textField in Flex (but how ?)

               

              Thanks for your patience

               

               

              Flex and PHP communicate by using XML.

               

               

              so

              echo "$nbvisites";

               

              will need to become something like:

               

              echo "<nbVisits>".$nbvisites."</nbVisits>";

               

               

              infact, before we get ahead of ourselves, do you have WAMP/XAMMP server running on your machine?

              • 4. Re: Hits counter. HELP :'(
                chandly9 Level 1

                Yes, I have Mamp and Flex Builder so do I really need Mamp ?

                • 5. Re: Hits counter. HELP :'(
                  Matt Le Fevre Level 4

                  chandly9 wrote:

                   

                  Yes, I have Mamp and Flex Builder so do I really need Mamp ?

                   

                  Yes you will need Mamp, Flex cannot run PHP files by itself, it needs a server behind it.

                   

                   

                  right, in the root folder for your local server, create a file called "count.php"

                   

                  in this file, put the following code:

                   

                  <?php
                  
                      
                      $counterPage = ("hitcounter.txt");
                      $noHits = file($counterPage);
                      $noHits[0] ++;
                      $fp = fopen($counterPage , "w");
                      fputs($fp , "$noHits[0]");
                      fclose($fp);
                      print "<counter>".$noHits[0]."</counter>";
                  
                  ?>
                  

                   

                  create a new flex application, and put in the following code:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="getHits.send()">
                  
                      <mx:Script>
                          <![CDATA[
                              import mx.rpc.events.ResultEvent;
                              
                              [Bindable]
                              public var noVisits:String;
                              
                              private function handleGetHits(evt:ResultEvent):void
                              {
                                  noVisits =  evt.result.counter;
                              }
                              
                          ]]>
                      </mx:Script>
                      
                      <mx:HTTPService id="getHits" result="handleGetHits(event)" url="http://localhost/count.php" method="POST"/>
                  
                      <mx:TextInput text="Visits: {noVisits}" x="361" y="241"/>
                          
                  </mx:Application>
                  

                   

                   

                  run that, and it should work.

                  1 person found this helpful
                  • 6. Re: Hits counter. HELP :'(
                    chandly9 Level 1

                    Ok, fine. I run that and I have the Textfield with :

                     

                    Visits : null

                     

                    And the hitcounter.txt was not create.

                     

                    :/

                    • 7. Re: Hits counter. HELP :'(
                      Matt Le Fevre Level 4

                      instead of

                       

                      <mx:HTTPService id="getHits" result="handleGetHits(event)" url="http://localhost/count.php" method="POST"/>

                       

                      try

                       

                      <mx:HTTPService id="getHits" result="handleGetHits(event)" url="http://127.0.0.1:8888/count.php" method="POST"/>
                      • 8. Re: Hits counter. HELP :'(
                        Matt Le Fevre Level 4

                        you could always try going to

                         

                         

                        http://127.0.0.1:8888/count.php

                         

                        in your browser and seeing if a count is being displayed, or if any errors are shown.

                        • 9. Re: Hits counter. HELP :'(
                          chandly9 Level 1

                          Ok, THANK YOU ! It works fine. But I test in my "test" project.

                           

                          But in my "real" project, it doesn't work because :

                           

                          I made a script in order to obtain the date :

                           

                           

                              <mx:Label text="{df.format(new Date ())}" y="293" horizontalCenter="135" color="#FFFFFF" show="resourceManager.localeChain = ['en_US', 'fr_FR']" fontFamily="Verdana"/>

                          But it was in English and I have to put it in french, so I change the language due to : show="resourceManager.localeChain = ['en_US', 'fr_FR']" and in the flex project properties : I changed the line "locale en_US" in "locale fr_FR"
                          And now the hit counter doesn't because of the <HTTPService>, so when I delete it it works it's only with it or Webservice or URLLoader ... Strange :s
                          I have the errors :
                          Unable to resolve resource bundle "messaging" for locale "fr_FR" (8 times)
                          Unable to resolve resource bundle "rpc" for locale "fr_FR" (3 times)
                          What can I do ?
                          • 10. Re: Hits counter. HELP :'(
                            chandly9 Level 1

                            Well, I succeed in resolving my problem but, and I think, it will be my last question. When I upload my project on the server, what will be the place where should be "hitcounter.txt" ? Because I don't think that it's possible to modify the number of the hist count on a server.

                            • 11. Re: Hits counter. HELP :'(
                              Matt Le Fevre Level 4

                              you will need to change the HTTP url location of the php file to wherever you want to place that.

                               

                               

                              The .txt file needs to be in the same directory as the php file.

                              • 12. Re: Hits counter. HELP :'(
                                chandly9 Level 1

                                Yeah thank you ! It works very fine !