Results 1 to 4 of 4

Thread: Flash and PHP

  1. #1

    Flash and PHP

    hi, i am trying to create a hit counter in the backround of a flash application. However, i can't seeem to get the code in flash correct to access the php script. I will give you the code and any help i greatly appreciate in advance.


    Flash Code:
    Code:
    _root.checklot=0;
    
    setInterval(check,1)
    function check() {
    	if(_root.checklog == 1){
    		_root.gotoAndStop(2);
    	}else{
    		_root.gotoAndStop(1);
    		clearInterval(check);
    	}
    }
    
    today = Date.getDate();
    visit_so = SharedObject.getLocal("visit");
    
    if(visit_so.data.date != undefined) {
    	visit_so.data.date = today;
    	visit_so.flush();
    	_root.checklot = loadVariablesNum("http://www.kidsforsavingearth.org/php_inc/counter.php?visit=TRUE", 0, "GET");
    }else{
    	if(visit_so.data.date != today) {
    		visit_so.data.date = today;
    		visit_so.flush();
    		_root.checklot = loadVariablesNum("http://www.kidsforsavingearth.org/php_inc/counter.php?visit=FALSE", 0, "GET");
    	}
    }

    PHP Code: (i've checked this and it works fine w/out flash)
    PHP Code:
    <?php
    function get($get) {    
        if (
    $get == 'TRUE') {
            
    $return FALSE;
            
    counter ($return);
        }else{
            
    $return TRUE;
            
    counter ($return);
        }
    }
    function 
    counter($return) {
        if (
    $return) {
            
    increase_counter("return.txt");
        }else{
            
    increase_counter("new.txt");
        }
    }

    function 
    increase_counter ($filename) {
        
    //Reading the file
            
    $file fopen($filename'r');
            
    $line fgets($file999);
            
    fclose($file);
            
    //Increasing the count
            
    $count explode("="$line);
            
    $count[2] = (($count[1])+1);
            
    //Writing to file
            
    $file fopen($filename'w');
            
            
    fwrite($file'hits='.$count[2]);        
            
    fclose($file);
    }
    get($_GET['visit']);
    print 
    "1";

    ?>
    Thanks for any help,
    Dan

  2. #2
    First problem:
    Code:
    today = Date.getDate();
    You need to creeate an instance of the Date object in order to access it's properties
    Fix.
    Code:
    my_date = new Date();
    today = my_date.getDate();
    Second Problem
    Code:
    setInterval(check,1000)
    [...]
    clearInterval(check)
    ClearInterval needs as a parameter the id of the interval - returned by the setInterval function. A interval of 1 - miliseconds... means that function 'check' will repeat 1000 times per second... this is not too fun for the processor . And because of that gotoAndStop(1) inside it, all the script in the first frame gets executed too in the same rithm

    Fix for this
    Code:
    //inside the check function
    clearInterval(my_interval);
    [...]//outside the check function
    my_interval = setInterval(check,1000)
    Check is a property for componets.. I hope it would not create a problem using it.

    Let's consider the first function
    Code:
    function check() {
    if(_root.checklog == 1){
    _root.gotoAndStop(2);
    }else{
    _root.gotoAndStop(1);
    clearInterval(my_interval);
    }
    }
    If the function get's executed once... and loadVariablesNum returned nothing, the execution will get through the else statements.. because _root.checkLog is 0 and not 1... so else not 1 ... do the folowing. Then you delete the interval (trying) just to create another one.

    Last problem
    loadVariablesNum returns NOTHING.

    In the next line checklot will be undefined.
    _root.checklot = loadVariablesNum("http://www.kidsforsavingearth.org/php_inc/counter.php?visit=FALSE", 0, "GET");
    You should output form your php in the last line something like
    PHP Code:
    print "&checklot=1"
    Then loading the variables like loadVariablesNum("yourpath.php",0,"GET") will make your variable printed from php (checklot in this case) available on level 0 -> _level0.checklot
    Easyer would have been to load directly in root with the loadVariables method
    like _root.loadVariables('yourpath.php?getVars=values') ;

    But anyway.. you don't need to continuosly check if that var has changed... you can do something (like the gotoAndPlay) when that happens - meaning when the onData event is called

    General Fix (How I would do it)
    Code:
    _root.onData = function (){
    _root.gotoAndStop(2);
    }
     
    my_date = new Date();
    today = my_date.getDate();
    visit_so = SharedObject.getLocal("visit");
     
    if(visit_so.data.date != undefined) {
    visit_so.data.date = today;
    visit_so.flush();
    _root.loadVariables(http://www.kidsforsavingearth.org/ph...php?visit=TRUE);
    }else{
    if(visit_so.data.date != today) {
    visit_so.data.date = today;
    visit_so.flush();
    _root.loadVariables(http://www.kidsforsavingearth.org/ph...hp?visit=FALSE); }
    }
    Hope it helps

    PS.
    Now that I'm thinking of it.. you don't even need the onData event. Calling the loadVariables will send the data and load the response, but you don't need the response. You don't react to the response - why waiting for it ?
    So.. removing the onData function and putting the gotoAndStop(2) at the end of your script will do the trick. Me thinks
    ...lurking around

  3. #3
    Wow.. this was my longest post
    ...lurking around

  4. #4
    Thanks a lot, this fixed my problems.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012