shaerzzr 发表于 2015-8-1 07:30:41

用delphi+Apache 开发动态网站(二)

-------创建数据库应用
  时间过的真快,距离上次写Delphi+Apache 开发动态网站已经快一年了,Borland 的Delphi 7都发布快半年了,一直没有时间静下写文章,今天就写一下怎么创建数据库应用,其实网站的数据库和普通的客户端应用差不多,唯一的不同就是那些可视化控件不能再用了,虽然delphi 6 和7里面提供的websnap 功能比较强大,但是在实际应用中定制性太差,可控制的东西复杂,因此本文还是以web broker 为例。
  一般的网页都是以上、中、下来设计的,即上面是基本固定的LOGO 和联接部分,中间是内容部分,下面是版权等基本信息,因此我们可以把网页分成三部分,即上、中、下部分,由于上、下部分相对固定,我们就建立两个PageProducer1 元件与之相对应,中间的部分再根据情况动态设定。
  我们以delphi 7 和 apache 2.0.43 和 Interbase为例,delphi7 不支持 Apache 2.0.43 ,具体解决办法请看我的文章使Delphi 6 支持Apache 2.0。首先建立web server 应用程序。如图一,

  这样就建立起一个Apache 2.x 的 DSO 模块,放置dbexpress 数据库联接和访问控件和PageProducer 元件,如图二,

  并保证 Dbexpress 控件可以正常访问数据库,具体访问方式超出本文章范围,这里不再详细讨论。在Apache2 的主目录(一般为htdocs目录)建立一下两个文件,title.html,foot.html.
  title.html:
  
  
  
  
  
  
  
  
  
  
  
  用户名
  密码
  
  
   注册新用户
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  当前人数:
  
  
  
  
  
  
  
  
    首页  |
    软件下载  |
    技术资料  |
    技术文章  |
    网络资源  |
    技术论坛  |
    给我留言  
  
  
  
  
  foot.html:
  
           
           
           
  谢谢光临         
           
           
  给我留言         
  返回主页         
  给我写信         
           
           
           
  1999-2001 delphi 窑洞            
  copyright            
              
  窑洞洞主:xalion     合作伙伴:阿土         
  
  
  
  
  好,开始我们的程序之旅:
  系统常量:
  const
  chweek:array of string=('日','一','二','三','四','五','六');
  defpathname:string='d:/Apache Group/Apache2/htdocs';
  defsoftpath:string='ftp://202.117.213.5/file/';
  defbookpath:string='ftp://202.117.213.5/book/';
  gridhead:string=' ';
  gridheadline:string='';
  gridline:string=' ';
  gridtdhead:string=' ';
  gridtdheadnowrap:string=' ';
  gridtdend:string=' ';
  webModule 的建立事件:
  procedure Twm.WebModuleCreate(Sender: TObject);
  var
  fconfig:Tinifile;
  bbsopen:string;
  fname:pchar;
  path:string;
  begin
  getmem(fname,256);
  GetModuleFileName( 0,fname,256);
  path:=ExtractFilePath(fname);
  Freemem(fname);
  看是否有设置文件
  fconfig:=Tinifile.create(path+'51delphi.ini');    //in folder
  pathname:=fconfig.readstring('PATH','HTMLROOT',defpathname);
  softpath:=fconfig.readstring('PATH','SOFTPATH',defsoftpath);
  bookpath:=fconfig.readstring('PATH','BOOKPATH',defbookpath);
  // defpathname);
  fconfig.Free;
  title.HTMLFile:=pathname+'/title.html';
  foot.HTMLFile:=pathname+'/foot.html';
  center.HTMLFile:=pathname+'/center.html';
  bookpage.HTMLFile:=pathname+'/book.html';
  
  end;
  
  在title 的onhtmltag 事件写:
  var
  yy,mm,dd:word;
  s,img,rc:string;
  i:integer;
  sip:string;
  allnum:integer;
  begin
  // 加入IP 计数
  iftagstring='getalluser' then
  begin
  sip:=request.RemoteAddr;//浏览器地址
  with cx do
  begin
  sql.Clear;
  SQL.Add('select max(id)+1 AS NUMfrom ip '); //取最大IP 值
  OPen;
  allnum:=fields.asinteger;
  sql.Clear;
  SQL.Add('select max(iptime) as iptime from ip where ip='''+sip+'''');// 是否访问过本站
  OPen;
  if Isempty then
  //没有访问过本站
  begin
  SQL.Clear;
  SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE )VALUES (:1,:2, CURRENT_TIMESTAMP, :3)');
  Params.AsInteger:=allnum;
  Params.AsString:=sip;
  Params.asstring:=request.UserAgent;
  try
  ExecSQL;   //写入访问时间
  except
  exit;
  end;
  end
  else
  begin
  //访问过本站
  if (now-fields.asdatetime)>(1/12) then
  //是否两小时内访问过
  begin
  SQL.Clear;
  SQL.Add('INSERT INTO IP (ID,IP ,IPTIME ,IPTYPE )VALUES (:1,:2,CURRENT_TIMESTAMP, :3)');
  Params.AsInteger:=allnum;
  Params.asstring:=sip;
  Params.asstring:=request.UserAgent;
  try
  ExecSQL;
  except
  exit;
  end;
  end;
  end;
  end;
  ReplaceText:=''; //仅仅是计数,不返回结果
  end;
  if tagstring='curuser' then //显示当前五分钟内在线用户
  begin
  with cx do
  begin
  SQL.Clear;
  SQL.Add(' select count(*) as num from IP');
  SQL.Add(' where(CURRENT_TIMESTAMP-iptime)
页: [1]
查看完整版本: 用delphi+Apache 开发动态网站(二)