就我现在的知识,用flash做一个网页计数器,可以有3种方法
1.使用数据库,我就会PHP+MySQL
2.不使用数据库,那么就是PHP+TXT
3.后台都不用,直接使用ActionScript的SharedObject;但是这么做,并不能做到真正意义上的网页计数,只能保存当前用户浏览该flash的计数
方法一:
/* MySQL Code */
CREATE TABLE `counter` (
`count` INT( 20 ) DEFAULT ''''0'''' NOT NULL
);
/* PHP Code */
<?php
$db=mysql_connect("localhost","root");
mysql_select_db("hbbspace");
// 取数
$result=mysql_query("SELECT * FROM counter WHERE 1",$db);
$myrow=mysql_fetch_row($result);
$count = $myrow[0];
// 加 1
$count++;
echo "count=$count"; // 列印
// 更新
$sql="UPDATE counter SET count=''''$count'''' WHERE 1";
$result=mysql_query($sql);
?>
/* Flash Code */
loadDate = function (success) {
if (success) {
var counters = counter_vars.count;
count_txt.htmlText = "你是第<font color=''''#FF0000''''>"+counters+"</font>个访问该swf的人";
}
};
var counter_vars:LoadVars = new LoadVars();
counter_vars.onLoad = loadDate;
counter_vars.load("http://localhost/counter2.php");
this.createTextField("count_txt", 1, 150, 100, 200, 100);
count_txt.html = true;
方法二:
/* PHP Code */
<?php
/* 将计数器归零
这样假如计数器还未被使用,初始值将是1
你当然也可以把初始值设成20000来骗人咯
*/
$count=0;
// 假如存放计数器文件已经存在,读取其中的内容
if ( file_exists($counter_file) )
{
$fp=fopen($counter_file,"r");
// 我们只取了前20位,希望你的站点不要太受欢迎啊
$count=0+fgets($fp,20);
// 由于函数fgets()返回字符串,我们可以通过加0的方法将其自动转换为整数
fclose($fp);
// 对文件操作完毕
}
// 增加一次计数值
$count++;
// 将新的计数值写入文件
$fp=fopen($counter_file,"w");
fputs($fp,$count);
fclose($fp);
# 返回计数值
echo "count=$count<br>";
?>
/* Flash Code */
loadDate = function (success) {
if (success) {
var counters = counter_vars.count;
trace(counters);
count_txt.htmlText = "你是第<font color=''''#FF0000''''>"+counters+"</font>个访问该swf的人";
}
};
var counter_vars:LoadVars = new LoadVars();
counter_vars.onLoad = loadDate;
counter_vars.sendAndLoad("http://localhost/counter.php?counter_file=counter.txt",counter_vars,"post");
this.createTextField("count_txt", 1, 150, 100, 200, 100);
count_txt.html = true;
方法三:
var myLSO = SharedObject.getLocal("foo");
if (myLSO.data.counter == undefined) {
var myCounter = {};
myCounter.count = 1;
myLSO.data.counter = myCounter;
counters = myLSO.data.counter.count;
} else {
myLSO.data.counter.count++;
counters = myLSO.data.counter.count;
}
this.createTextField("count_txt", 1, 150, 100, 200, 100);
count_txt.html = true;
count_txt.htmlText = "你是第<font color=''''#FF0000''''>"+counters+"</font>个访问该swf的人";