你是说,下一次用这个文件时总是一开始就把文件大小截为零,然后从头开始写,但问题是在下一次调用前,应该内容还是存在的吧?我只是调用一次啊,怎么最后内容都空的呢?我把代码全部帖上<?php
class ipdata
{
var $ip;
var $amount;
var $next;
};class ipdata_functions
{
var $head;
var $p;
var $tail;
function __destruct()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->next=NULL;
$this->p->amount=0;
$this->p=$this->head;
$file=fopen("ip.txt","w");
//fseek($file,0);
while($this->p!=NULL)
{
fwrite($file,$this->p->ip);
fwrite($file," ");
fwrite($file,$this->p->amount);
fwrite($file," ");
$this->p=$this->p->next;
}
fclose($file);
}
}
function __construct()
{
$this->head=new ipdata();
$this->head->next=NULL;
$this->tail=new ipdata();
$this->tail->next=NULL;
if (!file_exists("ip.dat"))
{
$file=fopen("ip.dat","r+");
fseek($file,0);
$ch=fgetc($file);
while(!feof($file))
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
while($ch!=" ")
{
$this->p->ip.=$ch;
}
$ch=fgetc($file);
$this->p->amount=fgetc($file);
if($this->head==NULL) $this->head=$this->p;
else
{
$this->tail->next=$this->p;
$this->tail=$this->p;
}
$ch=fgetc($file);
}
}
}
function checkandrecord($clientIP)
{
if ($this->head==NULL)
echo "到目前为止还没有投票哦!";
else
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p=$this->head;
while($this->p!=NULL && strcmp($this->p->ip,$clientIP))
{
$this->p=$this->p->next;
}
if($this->p==NULL)
{
$this->p->ip=$clientIP;
$this->p->amount=1;
$this->next=NULL;
//找出尾指针
$this->tail=$this->head;
while($this->tail!=NULL)
{
$this->tail=$this->tail->next;
}
//链接到尾指针去
$this->tail->next=$this;
}
else
{
if ($this->p->amount>=3)
echo "感谢你的投票,你已经投票过了,请不要反复投票!";
else
{
$this->p->amount++;
}
}
}
} function creat()
{
$a[0]=321412;
$a[1]=454354;
$a[2]=433676;
//$p=new ipdata(); $this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p->ip=$a[0];
$this->p->amount=1;
$this->head=$this->p; $this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[1];
$this->p->amount=2;
$this->head->next=$this->p;
$this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[2];
$this->p->amount=3;
$this->head->next->next=$this->p;
}
function show()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p=$this->head;
while($this->p!=NULL)
{
echo $this->p->ip;
echo " ";
echo $this->p->amount;
echo "<br>";
$this->p=$this->p->next;
}
}
}
}$temp =new ipdata_functions();
$temp->creat();
$temp->show();
$temp=NULL;?>
class ipdata
{
var $ip;
var $amount;
var $next;
};class ipdata_functions
{
var $head;
var $p;
var $tail;
function __destruct()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->next=NULL;
$this->p->amount=0;
$this->p=$this->head;
$file=fopen("ip.txt","w");
//fseek($file,0);
while($this->p!=NULL)
{
fwrite($file,$this->p->ip);
fwrite($file," ");
fwrite($file,$this->p->amount);
fwrite($file," ");
$this->p=$this->p->next;
}
fclose($file);
}
}
function __construct()
{
$this->head=new ipdata();
$this->head->next=NULL;
$this->tail=new ipdata();
$this->tail->next=NULL;
if (!file_exists("ip.dat"))
{
$file=fopen("ip.dat","r+");
fseek($file,0);
$ch=fgetc($file);
while(!feof($file))
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
while($ch!=" ")
{
$this->p->ip.=$ch;
}
$ch=fgetc($file);
$this->p->amount=fgetc($file);
if($this->head==NULL) $this->head=$this->p;
else
{
$this->tail->next=$this->p;
$this->tail=$this->p;
}
$ch=fgetc($file);
}
}
}
function checkandrecord($clientIP)
{
if ($this->head==NULL)
echo "到目前为止还没有投票哦!";
else
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p=$this->head;
while($this->p!=NULL && strcmp($this->p->ip,$clientIP))
{
$this->p=$this->p->next;
}
if($this->p==NULL)
{
$this->p->ip=$clientIP;
$this->p->amount=1;
$this->next=NULL;
//找出尾指针
$this->tail=$this->head;
while($this->tail!=NULL)
{
$this->tail=$this->tail->next;
}
//链接到尾指针去
$this->tail->next=$this;
}
else
{
if ($this->p->amount>=3)
echo "感谢你的投票,你已经投票过了,请不要反复投票!";
else
{
$this->p->amount++;
}
}
}
} function creat()
{
$a[0]=321412;
$a[1]=454354;
$a[2]=433676;
//$p=new ipdata(); $this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p->ip=$a[0];
$this->p->amount=1;
$this->head=$this->p; $this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[1];
$this->p->amount=2;
$this->head->next=$this->p;
$this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[2];
$this->p->amount=3;
$this->head->next->next=$this->p;
}
function show()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;
$this->p=$this->head;
while($this->p!=NULL)
{
echo $this->p->ip;
echo " ";
echo $this->p->amount;
echo "<br>";
$this->p=$this->p->next;
}
}
}
}$temp =new ipdata_functions();
$temp->creat();
$temp->show();
$temp=NULL;?>
解决方案 »
- 出现Warning: Call-time pass-by-reference has been deprecated in 这种警告怎会解决?(急)
- PHP 查询 MySQL 数据,返回值是乱码!
- 请问Php如何将一些特殊字符保存到数据库,数据库是Mysql,数据由页面提交页面
- php数组跟js数组的转化
- 大家去拍砖!我的第一次模板的应用
- 学用php调用oracle,请各位师兄帮帮忙,,
- window.showModalDialog打开和窗口问题
- 帮我解决一下?
- 为什么不认识PHP变量?提示“Notice: Undefined variable: .......”,在线等
- 谁知道武汉的比较大一点网络公司(要做网页的),我想去实习!西西!谢了!
- 这个mysql更新语句怎么写,请高手进来帮帮我?
- 转换编码后出现问题
$this->p->amount=0;
$this->p->next=NULL;$this->p->ip=$a[0];
$this->p->amount=1;
$this->head=$this->p;$this->p=new ipdata_functions(); //应该是ipdata();
$this->p->next=NULL;
$this->p->ip=$a[1];
$this->p->amount=2;
$this->head->next=$this->p;$this->p=new ipdata_functions(); //应该是ipdata()
$this->p->next=NULL;
$this->p->ip=$a[2];
$this->p->amount=3;
$this->head->next->next=$this->p;
正因为不小心把上面的写错了,就建立了好几个ipdata_functions对象,因此最后执行了多次的构造与析构函数.我晕,我还以为是php的问题,原来是自己的问题.太不小心了.