# PHP into Flash

**URL:** https://forum.kirupa.com/t/php-into-flash/22084
**Category:** programming
**Created:** [May 30, 2003, 12:18am UTC](https://forum.kirupa.com/t/php-into-flash/22084 "2003-05-30T00:18:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mdipi](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mdipi/32/49_2.png) [@mdipi](https://forum.kirupa.com/u/mdipi)
#### Post date: [May 30, 2003, 12:18am UTC](https://forum.kirupa.com/t/php-into-flash/22084/1 "2003-05-30T00:18:19Z")

</div>

Hey guys. I got a script for a PHP counter but i want to display it in flash. so on the last line of my PHP script i added:

```php

$variable="variable=$variable";
echo $variable

```

then in flash i am using

```auto

counterText=loadVariables("counter.php");

```

is that how i would go about doing it? I was talking to David and he helped me this far, but we both wanted to doubble check to make sure thats how you would do it.

Any help is appreciated-  
-Mike

PS: Here is the counter script:

```php

<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0]++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>

```

This is before adding what i said above btw

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 2:53am UTC](https://forum.kirupa.com/t/php-into-flash/22084/2 "2003-05-30T02:53:42Z")

</div>

if you follow what i posted here it should work 🙂

[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=18524&highlight=counter](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=18524&highlight=counter)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 6:04pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/3 "2003-05-30T18:04:16Z")

</div>

i knew i saw that thread some where/ when i searched it didnt find it. thaks!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 6:47pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/4 "2003-05-30T18:47:50Z")

</div>

Ahmed: i am using this code in the first frame of my movie:

```auto

count=loadVariables("counter.php", 0);

```

but the thing is that it wont show up in my text box of count. i can provide the FLA if thats what you need, but i am using the code above so i dont htinkits the PHP

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 8:30pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/5 "2003-05-30T20:30:00Z")

</div>

use this actionscript:

```auto
myTextbox.variable = myCounter
lv = new LoadVars()
lv.onLoad = function(success) {
if(success) {
myCounter = this.myCounter
}
}
lv.load("counter.php");

```

… that should load the variable ‘myCounter’ and display it in the textbox 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 8:36pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/6 "2003-05-30T20:36:53Z")

</div>

ok ahmed i will try it, thanks! but just a question, shouldnt loadVariables have worked?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 8:39pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/7 "2003-05-30T20:39:53Z")

</div>

the way you were trying to do it, it won’t 🙂

this could’ve worked:

```auto
myTextbox.variable = myCounter
loadVariables("counter.php", this)
// when loadVariables is done loading, the variable myCounter in the php script will be
// included in the flash movie, and then loaded into the textbox

```

🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 10:34pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/8 "2003-05-30T22:34:20Z")

</div>

ahmed, the first code didnt werk. i will try this one.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 10:41pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/9 "2003-05-30T22:41:52Z")

</div>

ahmed, unfortuanatly that doesnt work either. i honestly dont know why, i think i am doing everything right…but i just dont know…if you want here is the fla to the project.

[www.mdipi.com/php/miniml.fla](http://www.mdipi.com/php/miniml.fla)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 11:08pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/10 "2003-05-30T23:08:54Z")

</div>

where’s your script located?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 30, 2003, 11:11pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/11 "2003-05-30T23:11:54Z")

</div>

ok… you need to have the field’s instance name set as ‘myTextbox’… both methods should work 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 31, 2003, 12:19am UTC](https://forum.kirupa.com/t/php-into-flash/22084/12 "2003-05-31T00:19:35Z")

</div>

doh!  
lol thanks a ton! lol how stupidof a mistake was it?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 31, 2003, 12:22am UTC](https://forum.kirupa.com/t/php-into-flash/22084/13 "2003-05-31T00:22:25Z")

</div>

omgosh! still nothing! i odnt get it!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 31, 2003, 6:21am UTC](https://forum.kirupa.com/t/php-into-flash/22084/14 "2003-05-31T06:21:16Z")

</div>

ok let’s see now…

```php

<?

$filename = "TestCounter.txt";

$file = fopen( $filename,"r"); 
$OldCount = fread($file, 25); 
fclose( $file ); 

$OldCount = split ("=", $OldCount, 25);

$NewCount = $OldCount[1] + '1';

$New = "&count=$NewCount";

$file = fopen( $filename,"w+");
if (flock($file, 2)) { 
fwrite($file, $New, 100); }
fclose( $file ); 

print "&count=$NewCount";

?>

```

if (flock($file, 2)) { \<— this line blocks the file or something! I saw it in a tute somewhere! Anyway it keeps the count from resetting when a bunch of ppl visit the site at the same time n’ all.

Are you doing a Live counter or just a counter?

You can see my live one here: \<a href=“[http://www.vmkdsn.com/sykopage/counter/TestCounter.htm](http://www.vmkdsn.com/sykopage/counter/TestCounter.htm)”\>Syko’s Live Counter\</a\>

\<b\>NB! Be sure to change the permissions of the txt file to 777\</b\>

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 31, 2003, 2:08pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/15 "2003-05-31T14:08:19Z")

</div>

Well syko its just that i want it too count when ppl visit lol. i am not to sure what you mean by live. though. but i will change the permission right now…(oops)  
edit one:  
i changed them and still nothing. i will try your code though  
edit two:  
tryed syko’s php and still nothing. i has to be with the flash. my code is in the first frame and is as follows:

```auto

myTextbox.variable = myCounter
loadVariables("counter.php", this)

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 31, 2003, 5:10pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/16 "2003-05-31T17:10:40Z")

</div>

check my .fla file here: [http://www.vmkdsn.com/sykopage/counter/TestCounter.fla](http://www.vmkdsn.com/sykopage/counter/TestCounter.fla)  
I have the tetbox named the same as the variable in the txt file! I alwasy have a problem with that!

And by Live Counter I meant that the counter refreshes itself all the time so you don’t have to refrsh the page to refresh it!  
[go [http://www.vmkdsn.com/sykopage/counter/TestCounter.htm](http://www.vmkdsn.com/sykopage/counter/TestCounter.htm), opne up a new window and go to the same URL and see it increase! ] ( it will refresh after a period of time)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2003, 12:45am UTC](https://forum.kirupa.com/t/php-into-flash/22084/17 "2003-06-10T00:45:14Z")

</div>

syko, i tore apart some of the code you gave me and got it to display 1, but it never increments…like with this code in your sample fla

```auto

loadVariablesNum("TestCounter.txt?cnum="+random(99999), 0);

```

that loads the text file just fine, but the php is never executed. lol so where do i put the php in all of this?

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2003, 12:55am UTC](https://forum.kirupa.com/t/php-into-flash/22084/18 "2003-06-10T00:55:40Z")

</div>

i placed the php but still no werk! the fla is attached.

[www.mdipi.com/TextCounter.txt](http://www.mdipi.com/TextCounter.txt)  
thats the txt file

and the PHP code is exactly as you posted.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2003, 4:30pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/19 "2003-06-10T16:30:35Z")

</div>

wops! Sry for the delay!

I wish I could view that .fla but my hard-drive is all screwqed up right now.  
the varname of the textbox is “count” right?

Your going trough the same thing as I did. At first it didn’t show me anything, then it was just “1” and then voila.  
Try executing the php directly! ( www.something.sth/yourphp.php ) and see if it gives ya any errors.

And also if you have an action loading the textfile [size=1]loadvariablesnum(“txtfile.txt”);[/size] then delete it or put “//” symbols in front of it and see if it still shows the “1” there.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [June 10, 2003, 4:55pm UTC](https://forum.kirupa.com/t/php-into-flash/22084/20 "2003-06-10T16:55:09Z")

</div>

Not sure if your question is answered or not… but here is a code that I would use for a simple PHP hit counter…

here is a link to the working copy. Hit refresh a couple of times…

[http://www.bluefinatlantic.com/egTest/count.php](http://www.bluefinatlantic.com/egTest/count.php)

```php

<?
$file = "count.txt"; //define file

$fp = fopen($file, "a+"); //open file for read and writing

$data = fread($fp, 80000); //read file to retrieve data

ftruncate($fp, 0); //Cuts file length to zero - Erases previous value

$new = $data + 1; //Adds to counter

fwrite($fp, $new); //Writes the new number into the textfile

fclose($fp); //Closes the file

print "count=".$new; //Prints out the variable so Flash can read it.
?>

```

This only saves a single number to the text file, so there is no need to manipulate strings or anything…

Then your Flash code would be…

```auto

loadVariablesNum("http://www.somesite.com/phpFile.php", 0, "POST");

```

and on your main stage have a textbox that has the **variable** name of “count” (without the quotes).

if you want a sample file I can make one up…

[Next page](https://forum.kirupa.com/t/php-into-flash/22084.md?page=2)
