32 Replies Latest reply on Dec 5, 2007 12:38 PM by Greg Dove

    Making a visitor SWF counter

    StavBN
      I want to make a counter that will count the number of times that the swf viewed.
      For example, frame 5 is played only on time, so I want to do somthing that will add 1 to the counter.
      And I need the counter to be in a separate text file that will give me fast access.
      I hope you understand me.
      Thanks for the helpers!
        • 1. Re: Making a visitor SWF counter
          kglad Adobe Community Professional & MVP
          you'll need to use a server-side script to handle reading your counter file and updating it. you can use the loadvars() class in flash to call that server-side script.
          • 2. Re: Making a visitor SWF counter
            StavBN Level 1
            And what will be the server-side script?
            And I also need help with the text file.
            • 3. Re: Making a visitor SWF counter
              Greg Dove Level 4
              Do you understand that the server-side script itself is completely different to actionscript?

              The server-side script is in php or asp or cfm or some other scripting language, not actionscript. And you need to be running (for example, php, .net or coldfusion) on your server. Your server-side script would/could increment and save your counter data in a text file or a database on your server.

              The actionscript part is only the part that talks to the server-side script. That would (in as2) be most likely using the LoadVars class. You need to have the server side script coded and live on your server first.
              You may get help to do the server-side scripting here (its often the case with regular things like sending an email via php etc)... but don't be disappointed if you don't. Its not the primary focus of this forum which is the actionscript part (which you will get help with for sure if your server side script is already done and working).
              • 4. Re: Making a visitor SWF counter
                StavBN Level 1
                Yes, I know that I will need a PHP.
                for example, I already build a E-mail sender with flash and a PHP file next to the SWF.
                I just having problems with this counter.
                Maybe a PHP that will write into a txt file.
                Can you please help me build the counter (with php or what ever you need) in that forum?
                • 5. Re: Making a visitor SWF counter
                  StavBN Level 1
                  By the way, I got a server to upload to, that's how I build the E-mail sender.
                  • 6. Re: Making a visitor SWF counter
                    Greg Dove Level 4
                    Have you already tried to learn the php part yourself? Maybe on some php tutorial sites?

                    A simple google search

                    google: http://www.google.fr/search?hl=en&q=php+manipulation+of+text+files&meta=

                    gives a big choice. These are small things you can start to learn separately so start to learn the parts separately. Then join it together as you learn each of the different things you need to do. And be very clear what you want to do before you start.

                    This is the internet... there are search engines that help you find stuff and there's lots of existing free education out there. Take advantage of it first. :-)
                    • 7. Re: Making a visitor SWF counter
                      StavBN Level 1
                      If you will show me how to do it, it will be the easy way for me to learn.
                      Do you have a messenger maybe?
                      • 8. Re: Making a visitor SWF counter
                        Greg Dove Level 4
                        You're assuming I already know how to do it.
                        I don't. I've never done it before.
                        But I know I could learn to do it if I needed to because the resources are accessible for all and its not so complicated. And I'm sure you could learn it too if you tried.
                        The best way to learn and retain something new is through involvement not observation.
                        Give it a try and then come back here (with specific issues) if you have problems with things.

                        Perhaps others here may be prepared to offer you help for your request now. But I am not. I'm sure it's not your intention, but the impression I get is that you are seeking someone to do your work for you, rather than asking for help on specific issues after having made some effort yourself. You can easily prevent people forming that impression by demonstrating things you've tried, what's working and what's not.
                        • 9. Re: Making a visitor SWF counter
                          StavBN Level 1
                          Man, you got the wrong impression and I hope to change it.
                          So I build it, it should be working , but it's not.

                          The files:
                          count.php
                          counter.txt

                          The content count.php:
                          <?php

                          $myFile = "counter.txt";
                          $fh = fopen($myFile, "r+");
                          $num = fread($fh, filesize($myFile));
                          $num = $num+1;
                          fwrite($fh, $num);
                          fclose($fh);

                          ?>

                          The content counter.txt:
                          0


                          In the first flame inside the flash I put:
                          this.loadVariables("count.php");

                          I ran it several times but the number inside the txt is not growing.
                          What is wrong with my script?
                          • 10. Re: Making a visitor SWF counter
                            Greg Dove Level 4
                            Well, I'm pleased I had the wrong impression. And its much easier to answer your question now.

                            I looked at one tutorial and didn't see a r+ mode. Perhaps thats new or the tutorial was wrong (it happens)

                            So based on what I saw I would open in read mode and then wrote back in write mode.

                            <?php


                            $myFile = "counter.txt";

                            $read = fopen($myFile, 'r');
                            $num = fread($read, filesize($myFile));
                            fclose($read);

                            $num++;
                            $write=fopen($myFile, 'w');

                            fwrite($write, $num);
                            fclose($write);


                            ?>

                            Try that. Others here know more about php than I do so perhaps they'll give you better advice.
                            • 11. Re: Making a visitor SWF counter
                              StavBN Level 1
                              Not working too.
                              Thanks for trying to help.
                              Maybe anyone else know what the problem is?
                              • 12. Re: Making a visitor SWF counter
                                Greg Dove Level 4
                                No its working for me.... I just tried it. There's something else not right then. counter.txt needs to have a number 0 with no spaces or other characters.
                                your path to the php script needs to be correct

                                are you testing from localhost inside the flash IDE?

                                if so
                                use something more like
                                this.loadVariables(" http://localhost/count.php");
                                • 13. Re: Making a visitor SWF counter
                                  StavBN Level 1
                                  "counter.txt" is only contains 0, with nothing more.
                                  And I also tried to upload it to my server, but still with no success.
                                  Can you please upload the files in a rar or somthing?
                                  • 14. Re: Making a visitor SWF counter
                                    Greg Dove Level 4
                                    There's really nothing to rar. Its all here...

                                    Try it like this

                                    counter.php:

                                    <?php


                                    $myFile = "counter.txt";

                                    $read = fopen($myFile, 'r');
                                    $num = fread($read, filesize($myFile));
                                    fclose($read);

                                    $num++;
                                    $write=fopen($myFile, 'w');

                                    fwrite($write, $num);
                                    fclose($write);
                                    echo "result=".$num;

                                    ?>

                                    counter.txt (same directory as counter.php)
                                    contains:
                                    0


                                    and timeline code in flash:

                                    var result_lv:LoadVars = new LoadVars();
                                    result_lv.onLoad = function(success:Boolean) {
                                    if (success) {
                                    trace(result_lv.result);
                                    } else {
                                    trace( "Error connecting to server.");
                                    }
                                    };
                                    var send_lv:LoadVars = new LoadVars();
                                    send_lv.random = getTimer();
                                    send_lv.sendAndLoad(" http://localhost/testdir/counter.php", result_lv, "GET");

                                    [just change your url above.]
                                    • 15. Re: Making a visitor SWF counter
                                      Greg Dove Level 4
                                      That lets you see a copy of the counter back in flash - in the trace output - so you don't have to check the file on the server each time.

                                      I also put in a get var to send to avoid potential browser cache issues
                                      • 16. Making a visitor SWF counter
                                        StavBN Level 1
                                        Still not working.
                                        I uploaded it to:
                                        http://www.sivn.net/stav/test.html
                                        And all the other files are in the same directory.
                                        I'am so frustrated, the counter.txt content is still "0".
                                        • 17. Re: Making a visitor SWF counter
                                          StavBN Level 1
                                          Will running it withcode in flash that you gave me, that code:

                                          var result_lv:LoadVars = new LoadVars();
                                          result_lv.onLoad = function(success:Boolean) {
                                          if (success) {
                                          trace(result_lv.result);
                                          } else {
                                          trace("Error connecting to server.");
                                          }
                                          };
                                          var send_lv:LoadVars = new LoadVars();
                                          send_lv.random = getTimer();
                                          send_lv.sendAndLoad("counter.php", result_lv, "GET");

                                          I'am getting in the trace output that:
                                          Error connecting to server.
                                          Error opening URL 'file:///C|/Documents%20and%20Settings/Stav/Desktop/counter.php?random=16'
                                          • 18. Re: Making a visitor SWF counter
                                            Greg Dove Level 4
                                            For your first post...
                                            Your swf is at
                                            http://www.sivn.net/stav/test.swf

                                            But your code is (I think) requesting
                                            http://sivn.net/stav/counter.php

                                            (notice the missing www - that means its not requesting the same subdomain - and flash is asking your server for permission to do that - crossdomain.xml - and not getting it)

                                            Then you have another problem. Again its not related to the code above.

                                            its this:
                                            http://www.sivn.net/stav/counter.php

                                            What this is saying is that your php permissions don't allow you to write to that file counter.txt. So that's something you'll need to check out.
                                            .htaccess is relevant I think but it also may be a php.ini setting. I don't know and you should do some research to get good advice for that. You don't want to do the wrong thing with that kind of stuff is my advice there, and I can't advise you.

                                            For your second post:
                                            No it won't work locally unless you have a php server installed locally and you would normally access this via http://localhost
                                            For windows, WAMPserver is a good option.
                                            • 19. Re: Making a visitor SWF counter
                                              StavBN Level 1
                                              You said that there is two problems (for the first post).
                                              So I fixed the first and I did:

                                              var result_lv:LoadVars = new LoadVars();
                                              result_lv.onLoad = function(success:Boolean) {
                                              if (success) {
                                              trace(result_lv.result);
                                              } else {
                                              trace("Error connecting to server.");
                                              }
                                              };
                                              var send_lv:LoadVars = new LoadVars();
                                              send_lv.random = getTimer();
                                              send_lv.sendAndLoad(" http://www.sivn.net/stav/counter.php", result_lv, "GET");

                                              And the second problem you say I didn't understand whay you mean.

                                              For the second post, I tried:
                                              send_lv.sendAndLoad(" http://localhost/counter.php", result_lv, "GET");

                                              I\am getting almost the same thing like last time in the trace output that:
                                              Error opening URL ' http://localhost/counter.php?random=14'
                                              Error connecting to server.
                                              • 20. Re: Making a visitor SWF counter
                                                Greg Dove Level 4
                                                For the 2nd thing:
                                                You can't do the second thing on your local computer unless you have a webserver (e.g. Apache) and php installed on your local computer. That's what I mean. You can't run php unless you have it If you use ' http://localhost/counter.php?random=14' then you're trying to access php through a webserver installed on your own computer. If you don't have it, it won't work. And you can't test it on your own computer if you don't have it.

                                                Back to the first thing:
                                                If you made the changes on your server then the other thing you need to do is still there, which is what I mentioned before.
                                                I can't help you with that. You should do some research or ask someone who knows php better.
                                                click this link and you will see.
                                                http://sivn.net/stav/counter.php

                                                Its not able to write the $num value to the file. It can read it but not write it. That's a file perfmission thing on the server. Probably related to a file called .htaccess in that directory. But you will need to figure that part out for yourself, or with someone else's help.


                                                • 21. Re: Making a visitor SWF counter
                                                  StavBN Level 1
                                                  I understand you know, I will try to figure it by myself, And maybe ask the server owner.
                                                  Thank you again.
                                                  • 22. Re: Making a visitor SWF counter
                                                    Greg Dove Level 4
                                                    ok. good luck.
                                                    • 23. Re: Making a visitor SWF counter
                                                      StavBN Level 1
                                                      I just want to ask from you a favor (if it won't too hard for you).
                                                      It's not related to the server-side.
                                                      Can you explain me the AS you build here:

                                                      var result_lv:LoadVars = new LoadVars();
                                                      result_lv.onLoad = function(success:Boolean) {
                                                      if (success) {
                                                      trace(result_lv.result);
                                                      } else {
                                                      trace("Error connecting to server.");
                                                      }
                                                      };
                                                      var send_lv:LoadVars = new LoadVars();
                                                      send_lv.random = getTimer();
                                                      send_lv.sendAndLoad(" http://www.sinv.net/stav/counter.php", result_lv, "GET");

                                                      Because I don't understand how "send_lv" and "result_lv" are related...
                                                      • 24. Re: Making a visitor SWF counter
                                                        Greg Dove Level 4
                                                        send_lv contains the data to be sent to the server. You don't need to send any, you really just want to call the script here... but I'm using it to append a random (actually not so random, a timer value but its OK) GET variable to the counter script. This will help avoid any potential cacheing issues from the browser side, because it will make the url unique. The php script does not use the value which is available in the $_GET array. But its also a clue for how to do it if you wanted to pass a value for different 'counters' based on different parts of your swf that were viewed.

                                                        The result_lv part is just because you need to have something to receive the return results of a sendAndLoad... and it receives the return value from the script. When the script is working properly ... it will return the latest count number to flash. It does that in the php script with the
                                                        echo "result=".$num;
                                                        part.
                                                        You do need to return something from the script otherwise you'll get an error. You don't need to return the number - but that might be useful for testing.
                                                        Instead you could just do
                                                        echo "result=OK";

                                                        • 26. Re: Making a visitor SWF counter
                                                          Greg Dove Level 4
                                                          "GET" always appends the variables to the url.

                                                          ?var=value&var2=value2&var3=value3 etc Just like in the browser address bar up there ^

                                                          random is the name of the variable I put inside send_lv which just containers a timer number. LoadVars appends it to the url for you when sending with the "GET" method. I only put one variable...called random. So that's all it sends. It does nothing in the php script because the php script does not look at the variables it received in the php $_GET array (which are just the variables it finds appended to the url).
                                                          • 27. Making a visitor SWF counter
                                                            StavBN Level 1
                                                            I understand now!
                                                            So only if I use "POST" the php will use the vars into "result_lv"?
                                                            • 28. Re: Making a visitor SWF counter
                                                              Greg Dove Level 4
                                                              No. POST sends them inside the request body and not appended to the url. Its exactly like a html POST form submission from a html page. result_lv just gets whatever the server sends back, whether send_lv uses GET or POST. You want to use GET here because the only purpose is to create a unique url that won't cache.

                                                              Here's an easy way to think of it.
                                                              When you click on a link in this page, you are sending a GET request to the cfm script at adobe. The browser page also receives the server response... its text, but its formated as html. In flash its the same except a) the thing that sends it is not the same as the thing that receives it and b) its text as well, but not html in this case. Its a string that says "result=1" or "result=2" etc. Flash takes that can converts it to property/variable inside result_lv called "result" with the value "1". or "2"
                                                              so trace(result_lv.result);
                                                              should trace a number (the latest count value).

                                                              • 29. Making a visitor SWF counter
                                                                StavBN Level 1
                                                                So GET is to make a unique url.
                                                                And POST is to send the variables to the php so the php can use them.
                                                                Right?

                                                                And if I'am doing:
                                                                send_lv.sendAndLoad(" http://www.sivn.net/stav/counter.php", result_lv, "POST");
                                                                The variables inside "send_lv" is sending to the php and the coming back variables is getting inside "result_lv"?
                                                                • 30. Re: Making a visitor SWF counter
                                                                  Greg Dove Level 4
                                                                  In this case GET is to make a unique url but that's just taking advantage of the way it works. It is also sending a variable(s) to php in the url (POST sends variables that too, just in a different way, without changing the url).

                                                                  And yes, normally you would have php do something with the variables you send from flash. I'm not with this script... the variable 'random' is being sent and is completely ignored by the php script. Its only purpose is to make a unique url.

                                                                  The php script is returning a value called 'result' which is put inside result_lv.
                                                                  • 31. Re: Making a visitor SWF counter
                                                                    StavBN Level 1
                                                                    Now I completely understand you and how the sendAndLoad(); is working.
                                                                    Thanks.
                                                                    • 32. Re: Making a visitor SWF counter
                                                                      Greg Dove Level 4
                                                                      Great! Good luck with your counter.