40
國國國國國國國國國國 國國國 1 CGI 國國國國國國 (for UNIX Perl)

CGI 程式設計進階 ( for UNIX Perl)

  • Upload
    ely

  • View
    62

  • Download
    0

Embed Size (px)

DESCRIPTION

CGI 程式設計進階 ( for UNIX Perl). 主題. 身份認證 功能製作 檔案上傳功能製作 計數器製作 Perl+MySQL. 身份認證 功能製作. 身份認證 功能製作. 實作1- login.htm. < HEAD> CGI 密碼登錄範例 新增帳號 - PowerPoint PPT Presentation

Citation preview

Page 1: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 1

CGI 程式設計進階(for UNIX Perl)

Page 2: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 2

主題 身份認證功能製作 檔案上傳功能製作 計數器製作 Perl+MySQL

Page 3: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 3

身份認證功能製作

Page 4: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 4

身份認證功能製作<HEAD><meta charset=big5><TITLE>CGI密碼登錄範例 </TITLE></HEAD><BODY bgcolor=white><p align="center"><br>

<font size="+2"> 新增帳號 </font> <FORM METHOD="POST" ACTION= "http://ccy.dd.ncu.edu.tw/~test/passwd.cgi?add">

<p align="center"> 帳號 : <INPUT SIZE=30 NAME="username"><BR> 密碼 : <INPUT type=password SIZE=30 NAME="passwd"><BR> email : <INPUT SIZE=30 NAME="email"><BR>

<INPUT TYPE="submit" VALUE=" 送出資料 "> <INPUT TYPE="reset" VALUE=" 清除資料 "> </FORM>

實作 1- login.htm

Page 5: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 5

身份認證功能製作<HR>

<p align="center"><font size="+2"> 系統登錄 </font> <FORM METHOD="POST" ACTION="http://ccy.dd.ncu.edu.tw/~test/passwd.cgi">

<p align="center"> 帳號 : <INPUT SIZE=30 NAME="username"><BR>

密碼 : <INPUT type=password SIZE=30 NAME="passwd"><BR> <INPUT TYPE="submit" VALUE=" 送出資料 "> <INPUT TYPE="reset" VALUE=" 清除資料 "> </FORM> </BODY>

實作 1- login.htm (cont)

Page 6: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 6

身份認證功能製作#!/usr/bin/perl$passwdfile="/home/test/public_html/userpasswd";

&ReadForm(*FORM);

if ($ENV{QUERY_STRING} eq "add") {

&adduser;

} else {

&checkuser;

}

exit(0); #詳見次頁完整的原始碼

實作 1- passwd.cgi

Page 7: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 7

身份認證功能製作 範例程式畫面

Page 8: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 8

身份認證功能製作 範例程式畫面

Page 9: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 9

檔案上傳功能製作

Page 10: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 10

檔案上傳功能製作 Form 的 參 考 寫 法<FORM ENCTYPE="multipart/form-data" ACTION="upload.cgi" METHOD=POST> File to upload: <INPUT TYPE="file" NAME="filename"> <INPUT TYPE="submit" VALUE="SEND"> </FORM>

Page 11: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 11

檔案上傳功能製作 Perl 的 CGI 程 式 參 考 寫 法 upload.cgi#!/usr/bin/perl use CGI; use Fcntl; $q = new CGI; print $q->header; my $uploaddir = "tmp"; if ($file = $q->param('filename')) { $fname = $file; $fname =~ s/\\/\//g; $fname =~ s/\.\.//g; if (($pos = rindex($fname, '/')) != -1) { $fname = substr($fname, $pos+1); }

Page 12: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 12

檔案上傳功能製作 Perl 的 CGI 程 式 參 考 寫 法 (cont) print "<h2>File: $fname</h2>"; if (length($fname) > 0) { $fname = sprintf "%s/%s", $uploaddir, $fname; open (FN, "> $fname"); print "<pre>"; while (<$file>) { print; print FN $_; } print "</pre>"; close FN; } } print $q->end_html;

Page 13: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 13

計數器製作

Page 14: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 14

計數器製作 -Form 的 參 考 寫 法 實作 2- counter.htm

<html><body><script src="http:/ccy.dd.ncu.edu.tw/~test/counter.cgi"></script></body></html>

Page 15: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 15

計數器製作 - Perl 的 CGI 程 式 參 考 寫 法 實作 2- counter.cgi#! /usr/bin/perl$cgiurl="http://ccy.dd.ncu.edu.tw/~test/counter.cgi"; #online.cgi 的 URL$imgurl="http://ccy.dd.ncu.edu.tw/~test"; #img 目錄的 URL$cntdir="/home/test/public_html";$cntfile="counter.dat";if ($ENV{'QUERY_STRING'} eq "") { $cntfile="counter.dat";}else { $cntfile=$ENV{'QUERY_STRING'}.".dat";}

Page 16: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 16

計數器製作 - Perl 的 CGI 程 式 參 考 寫 法 實作 2- counter.cgi(cont)# 讀取 counter $FileName="< ".$cntdir."/".$cntfile; print "$FileName";if (open (FILE,"$FileName")){ @LINES=<FILE>; close(FILE); $LINES[0]=~ s/\"|\s//g ; if (not ($LINES[0] eq "") ){ $people=$LINES[0]; } else {$people="0"; } $people++;}else{ $people=1;}

Page 17: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 17

計數器製作 - Perl 的 CGI 程 式 參 考 寫 法 實作 2- counter.cgi(cont)# 將資訊寫入檔案$FileName="> ".$cntdir."/".$cntfile;open(OUTP, "$FileName") || die " 記錄檔案開啟錯誤 !!\n"; print OUTP "$people";close OUTP;print "Content-type: text/html\n\n";$text=$people;for($i=0;$i<=9;$i++){ $text =~ s/$i/<td><img src=\"$imgurl\/count_$i\.gif\"><\/td>/g;}

Page 18: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 18

計數器製作 - Perl 的 CGI 程 式 參 考 寫 法 實作 2- counter.cgi(cont)$text="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr><td><img src=\"$imgurl\/count_left\.gif\"> </td>“.$text."<td><img src=\"$imgurl\/count_right\.gif\"></td> </tr></table>";print "document.write(\'$text\');";exit;

Page 19: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 19

計數器製作範例畫面

Page 20: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 20

Perl+MySQL 安裝 MySQL

http://www.mysql.com/

Page 21: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 21

Perl+MySQL Install DBI-1.13

[root@fax /tmp]# zcat dbi-1.13.tar.gz | tar xvf –

[root@fax /tmp]# cd DBI-1.13

[root@fax DBI-1.13] perl Makefile.PL

[root@fax DBI-1.13] make

[root@fax DBI-1.13] make test

[root@fax DBI-1.13] make install

Page 22: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 22

Perl+MySQL Install msql-mysql-modules-1.2210

[root@fax /tmp]# zcat msql-mysql-modules-1.2210.tar.gz | tar xvf –

[root@fax /tmp]# cd Msql-Mysql-modules-1.2210

[root@fax Msql-Mysql-modules-1.2210] perl Makefile.PL

[root@fax Msql-Mysql-modules-1.2210]make

[root@fax Msql-Mysql-modules-1.2210]make test

[root@fax Msql-Mysql-modules-1.2210]make install

Page 23: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 23

Perl+MySQL MySQL 操作範例

[s8940@ccy public_html]$ mysql -u s8940 test

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 742 to server version: 3.22.30

Type 'help' for help.

mysql>

Page 24: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 24

Perl+MySQL MySQL 操作範例 (cont)

mysql> help;MySQL commands:

help (\h) Display this text

? (\h) Synonym for `help'

clear (\c) Clear command

connect (\r) Reconnect to the server. Optional arguments are db and host

exit (\q) Exit mysql. Same as quit

go (\g) Send command to mysql serverquit (\q) Quit mysql

status (\s) Get status information from the server

use (\u) Use another database. Takes database name as argument

Connection id: 742 (Can be used with mysqladmin kill)

Page 25: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 25

Perl+MySQL MySQL 操作範例 (cont)

mysql> show tables;+----------------+

| Tables in test |

+----------------+

| s8940 |

| testac |

+----------------+

2 rows in set (0.00 sec)

Page 26: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 26

Perl+MySQL MySQL 操作範例 (cont)

mysql> CREATE TABLE s8941

-> (CreateDate DATETIME ,

-> StudentName char(50) ,

-> StudentID char(10) ,

-> Address char(100) ,

-> Birthday DATE );

Query OK, 0 rows affected (0.01 sec)

mysql> show tables;

+----------------+

| Tables in test |

+----------------+

| s8940 |

| s8941 |

| testac |

+----------------+

3 rows in set (0.01 sec)

Page 27: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 27

Perl+MySQL MySQL 操作範例 (cont)

mysql> insert into s8941 values('2000/07/18 15:12:01',' 陳慶彥 ','87001',

-> ' 中壢市五權里上山座屋 38 號 ','1988/01/01');Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋 38 號 | 1988-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

1 row in set (0.01 sec)

Page 28: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 28

Perl+MySQL MySQL 操作範例 (cont)

mysql> insert into s8941 values('2000/07/18 15:19:01',' 陳二 ','87002',

-> ' 中壢市五權里上山座屋 38 號 ','1977/01/01');Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋 38 號 | 1988-01-01 |

| 2000-07-18 15:19:01 | 陳二 | 87002 | 中壢市五權里上山座屋 38 號 | 1977-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

Page 29: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 29

Perl+MySQL MySQL 操作範例 (cont)

mysql> update s8941 set Address=' 金門縣金沙鎮光前村陽翟 1 號 ' where StudentID='87002';

Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋 38 號 | 1988-01-01 |

| 2000-07-18 15:19:01 | 陳二 | 87002 | 金門縣金沙鎮光前村陽翟 1 號 | 1977-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

Page 30: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 30

Perl+MySQL MySQL 操作範例 (cont)

mysql> delete from s8941 where StudentID='87002';Query OK, 1 row affected (0.00 sec)

mysql> select * from s8941;+-------------------------+----------------+-------------+-----------------------------------+--------------+

| CreateDate | StudentName | StudentID | Address | Birthday |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

| 2000-07-18 15:12:01 | 陳慶彥 | 87001 | 中壢市五權里上山座屋 38 號 | 1988-01-01 |

+-------------------------+-----------------+-----------+------------------------------------+--------------+

mysql> quit

Page 31: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 31

Perl+MySQL

Perl 連結 MySQL 執行資料的存取use DBI;use strict;$dbh = DBI->connect(“DBI:mysql: 資料庫” ,“ 帳號” ,” 密碼“ );$sth = $dbh->prpare($statement);$sth->execute;$sth->finish;$dbh->disconnect;

Page 32: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 32

Perl+MySQL實作 3 – create_table.cgi

#!/usr/bin/perl

use DBI;

use strict;

my $dbh;

if ($dbh=DBI->connect("DBI:mysql:test","s8940","") ){

print " 連結資料庫 :test OK!\n";

}else { print " 無法連結資料庫 :test!"; exit(0); }

Page 33: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 33

Perl+MySQL實作 3 – create_table.cgi (cont)

my $sth=$dbh->prepare(" DROP TABLE s8940 ");

if ($sth->execute ){

$sth->finish;

print " 表格 :s8940 存在資料庫 :test 之中 , 刪除表格 :s8940 成功 \!\n";

}else {

print " 表格 :s8940 不存在資料庫 :test 之中 , 刪除表格 :s8940 失敗 \!\n"; }

Page 34: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 34

Perl+MySQL實作 3– create_table.cgi (cont)

my $sth=$dbh->prepare("CREATE TABLE s8940

( CreateDate DATETIME ,

StudentName char(50) ,

StudentID char(10) ,

Address char(100) ,

Birthday DATE)

");

Page 35: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 35

Perl+MySQL 實作 3 – create_table.cgi (cont)

if ($sth->execute){

print " 表格 :s8940 在資料庫 :test 上 create OK!\n";

}

else{

print " 表格 :s8940 在資料庫 :test 上 create 失敗 !: $dbh->errstr\n";

}

$sth->finish;

Page 36: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 36

Perl+MySQL 實作 3 – create_table.cgi (cont)

my $sth=$dbh->prepare("INSERT INTO s8940 VALUES

(‘2000/07/18 15:12:01’,‘ 陳慶彥’ ,‘87001’,‘ 中壢市五權里上山 座屋 38 號 ','1988/01/01') ");

if ($sth->execute){

print " 表格 :s8940 新增資料 OK!\n";

}else{ print " 表格 :s8940 新增資料失敗 !: $dbh->errstr\n"; }

$sth->finish; $dbh->disconnect; exit(0);

Page 37: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 37

Perl+MySQL 實作 4 – select_table.cgi

#!/usr/bin/perl

use DBI;

use strict;

my $dbh;

if ($dbh=DBI->connect("DBI:mysql:test","s8940","") ){

print " 連結資料庫 :test OK!\n"; }

else { print " 無法連結資料庫 :test!"; exit(0); }

Page 38: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 38

Perl+MySQL 實作 4 – select_table.cgi (cont)

my $sth = $dbh->prepare("SELECT * FROM s8940");

if ($sth->execute ){

my $table = $sth->fetchall_arrayref;

my($i, $j);

for $i ( 0 .. $#{$table} ) {

for $j ( 0 .. $#{$table->[$i]} ) { print "$table->[$i][$j]\t"; }

print "\n";

}

Page 39: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 39

Perl+MySQL 實作 4 – select_table.cgi (cont)

}

else {

print " 表格 :s8940 查詢資料失敗 \!\n";

}

$sth->finish;

$dbh->disconnect;

exit;

Page 40: CGI 程式設計進階 ( for UNIX Perl)

國立中央大學電算中心 陳慶彥 40

測驗 -檔名 (examsql.cgi) 請利用 Perl+MySQL 設計一個可供使用者登入資料與顯示資料的 CGI 程式