资源描述
实验一 unix命令基础
1.从windows98运行telnet命令;
2.选择主菜单连接下的远程系统
3.在主机名中填入192.168.16.210
端口默认为telnet
终端类型设为DEC-VT100
然后点击连接按钮
1.从windowsxp运行telnet命令;
2.选择终端类型:
set term vt52
3.打开远程主机:
open 192.168.16.210
4.在login:后填入用户名(如:c0101)
如果学号是2003052101,用户名为c0101;如果学号为j200352101,用户名为cj0101;
依此类推。
系统登录成功后将出现系统提示符
[用户名@主机名 当前文件夹名称]$
如:[xx0026@yward xx0026]$
表示你目前是以xx0026用户在主机yward上,当前工作文件夹为xx0026,实际上为/home/xx0026
5.用ls -l命令或ls -l|more命令查看当前文件夹下的文件或文件夹列表
注意文件属性和文件夹属性的区别
-rwxr_xr_x 拥有者 组名
drwxr_xr_x 拥有者 组名
6.用mkdir创建新的文件夹
用法:mkdir 文件夹名
用cd命令改变当前工作文件夹
用法:cd 文件夹相对路径或绝对路径
用rm -r -f命令删除文件或文件夹
用法:rm [-r] [-f] 文件名
7.用ps命令查看进程,并找到自己使用的进程
用法:ps
ps -ef
ps -ef|more
8.查看当前有哪些用户在使用系统
用法:who
who|sort
9.查看当前有工作路径全名
用法:pwd
实验二 PHP简单的表单处理
【实验目的】
掌握HTML基本用法,表单定义,表单元素,ACTION方式,PHP基本语法和接受表单方法
【实验内容】
一、建立一个简单的表单k1.html
要求输入学号和姓名两项,并把输入的内容提交给k1.php
二、k1.php打印输出"Hello! xxxxx!",(其中xxxxx表示在表单中用户输入的内容)
k1.php编码
【问题】
①注意method中get和post有什么区别?
②php变量的定义及变量类型有哪些?
③自己设计一个成绩输入表单,并根据提交的成绩分别显示"优,良,中,及格和不及格"
④
⑤
⑥
实验报告要求
实验报告统一命名为":phpXX_学号_姓名.doc(如实验二就命名为php02_01_张三.doc)",并上传到ftp://192.168.16.210/phpupload/php2(文件名请遵守上面的命名规则,否则无法看到)
Experiment Ⅲ Using text mode to build a counter
[Experimental purposes]
Grasp the simple method using file
[Experimental contents]
1.Storage time of the creation of a text file and named count.txt
(Note:this file must be nobody to have "w" attribute, you can using chmod command to change it)
2.Create a script file counters named count.php
<?
$fp = fopen("count.txt",'r+');
$data = fgets($fp,999);
$data ++;
echo 'data :'.$data;
rewind($fp);
fwrite($fp,$data);
fclose($fp);
?>
Prompt:
① Open the text file count.txt, and read data into a variable
② increasing the variable by 1 and display
③ re-writing the variable into the file, it means that the original contents should be replaced
④ closing file
[Experimental Problem]:
① Why nobody has count.txt must write permissions?
② What ways to open a digital file?
③ If multiple clients at the same time to refresh the page, what results?
④ Why should we close file?
Reporting requirements of the experiment
The experiment report standard nomenclature (for example experiment 2 naming for "php03_ student number _ name .doc is php03_01_zhang .doc)", and uploads to ftp://192.168.16.210/phpupload/php3
( filename please do observe the above naming rule, otherwise is unable to see)
Experiment IV using Graphic mode to build a graphic counter
[Experimental purposes]
Grasp the relevant data type conversion and string processing function
[Experimental contents]
1、updating the Experiment III Character Counter ,Changed to display a graphic counter
(Note: The picture on the relevant / home/xx06/pic folder, please read and quote their own)
ln -s /home/xx06/pic pic
Tip:
①First counter-digital converter for fixed-length text string such as 0000000012 with the following statement directly:
<?
$x=123456;
for($i=0;$i<strlen($x);$i++) echo substr($x,$i,1);
?>
②Using String Functions conversion each and every figure for the following standard format HTML format
<img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/0.gif><img src=gif1/1.gif><img src=gif1/2.gif>......
③Note the location of 0-9 picture
More complex counter
2、Realize lottery ticket 36 select 7 simulation draw a lottery function
Tips:
①1-36 in the number of randomly selected 7 shall not repeat
②Random number seeds and the use of random number function is as follows
srand((double)microtime()*1000000);
$p=rand(1,36);
[Experimental Problem]:
① Why do you want to count the number of fixed-length string?
②How to display a picture?
③
④
Reporting requirements of the experiment
The experiment report standard nomenclature (for example experiment 2 naming for "php03_ student number _ name .doc is php03_01_zhang .doc)", and uploads toftp://192.168.16.210/phpupload/php4
( filename please do observe the above naming rule, otherwise is unable to see)
实验五 PHP函数的定义和使用
【实验目的】 掌握代码重用、PHP函数的定义和应用、变量的作用域、函数的返回值,以及函数的引用传递和值传递
【实验内容】
1、《PHP和MySQL Web开发》教材第5章文字内容和实例
2、补充以下几个函数,主要针对日期和时间的处理
<?
//+ -time
function DateAdd($interval,$number,$date) {
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
echo $year = $date_time_array["year"];
echo "now:".$date."<br>";
switch($interval) {
case "yyyy":$year += $number;break;
case "q":$month += ($number*3);break;
case "m":$month +=$number;break;
case "y":
case "d":
case "w":$day += $number;break;
case "ww":$day +=($number*7);break;
case "h":$hours += $number;break;
case "n":$minutes += $number;break;
case "s":$seconds += $number;break;
}
$timestamp = mktime($hours,$minutes,$seconds,$month,$day,$year);
echo "after:"."$timestamp<br>";
echo strftime( "%Hh%M %A %d %b<br>",$timestamp);
}
//compare time
Function DateDiff($interval,$data1,$date2) {
$timedifference =$date2 - $data1;
switch($interval) {
case "w":$retval =
bcdiv($timedifference,604800);break;
case "d":$retval =
bcdiv($timedifference,86400);break;
case "h":$retval =
bcdiv($timedifference,3600);break;
case "n":$retval = bcdiv($timedifference,60);break;
case "s":$retval = $timedifference;break;
}
return $retval;
}
//H:i:s ->s
function t2s($c) {
$z = explode(":",$c);
$second = $z[2]+$z[1]*60+$z[0]*60*60;
echo $second."<br>";
}
//s->H:i:s
function s2t($second) {
$sec=($second%60);
$k2=floor($second/60);
$hou=floor($k2/60);
$min=floor($k2%60);
echo "$hou:$min:$sec<br>";
}
DateAdd("yyyy",1,time());
t2s("353316:7:53");
s2t(time());
?>
【问题】
①注意require和include有什么区别?
②如果调用未定义的函数、或重复定义函数会有什么样的结果?
③在PHP中变量名的命名规则和函数名的命名规则有什么不同?
④在函数中如何应用全局变量?掌握变量的作用域。
⑤函数参数的引用传递和值传递有何区别?
⑥如何从函数中返回值?有几种方式?
实验报告要求
实验报告统一命名为"php_学号_姓名.doc(如实验二就命名为php2_01_张三.doc)",并上传到ftp://192.168.16.210/phpupload/php5(文件名请遵守上面的命名规则,否则无法看到)
【备注】
PHP中的日期处理
我正打算用PHP编写一种帮助处理系统。我发现我必须知道处理完最后一位客户的问题后已经过去了多长时间?当我过去用ASP时解决这个问题相当简单,ASP有相应的函数DateDiff可以给出两个日期间间隔多少月、多少天和多少秒。当我搜寻完PHP手册后我发现PHP并没有类似的函数。
本文包含以下内容:
1、 得到目前的日期和时间-我们有多少种方式?
2、 改变日期显示的方式-日期和时间的显示形式
3、 转换现在的日期为Unix的时间戳值
4、 改变日期
a. 增加时间
b. 减去时间
c. 找出两日期之间的间隔
5、 为PHP添加DateAdd函数
6、 为PHP添加DateDiff函数
**得到目前的日期和时间
在Unix中,时间的表示方式为计算从1970年1月1日零时起所过去的秒数,这称为UNIX 时间戳(Unix Epoch)。
如果我们有这样一段的代码:
<?
echo time();
?>
将返回值958905820
而此时的时间为2000年5月21日12时43分。
你也许会说这相当不错。当这对我毫无帮助,或者只有一点帮助。在PHP中,对日期处理的函数都必须用到由time()返回的时间戳值。同时,由于PHP在Unix和Windows系统中均使用同样的时间戳值,这就允许你不需要修改代码即可在不同的系统间移植。另外的一个好处是time()函数返回的是一个整数,你可以将其作为整数字段或文本字段存入数据库,而不必使用特别的日期/时间字段。
你已经基本了解了Unix的时间戳值,现在让我们来展示它的实际用途。
改变日期显示的方式-日期和时间的显示形式
PHP提供两个办法来将Unix的时间戳值转换成为有用的数据。第一个是date()函数。这个函数有两个参数-第一个字符串用于设定你所希望返回的格式,第二个为Unix的时间戳值。
格式化字符串通过一些简单的特殊格式化字符来显示你所希望看到的格式的日期和时间。假设你希望日期以这样的格式显示“18h01 Sunday 21 May”。
我们需要对字符串中的每一部分使用一个特殊格式化字符,你可以从PHP手册中日期和时间函数库中找到。这样的特殊格式化字符数量不少,他们所表示的类似于星期几、月的英文名、用2位或4位数表示的年份,是否是上午(AM)或下午(PM)以及其他。对于这个例子我们需要的特殊字符为:
‘H’ -24 小时制的小时
‘i’- 分钟
‘l’- 星期几的英文全名
‘d’- 本月的第几日
‘F’- 月份的英文全名
因此我们的格式化字符串为”Hhi l d F”, PHP代码为:
<?
echo date ("Hhi l d F" ,time());
?>
当我们执行这段代码,我们发现我们所得到的结果为:
180609 Sunday 21 May
这样的结果看起来有些奇怪。让我们再查一下PHP手册,原来’h’所代表的是12 小时制的小时数。这再次证明了一句真理:“计算机只做你所告诉它该做的,而不是你想要它做的”。我们有两个选择。第一个是在h前使用转义字符“”:
echo date ("Hhi l d F", time());
我们得到这样的结果:
18h12 Sunday 21 May
这正是我们所要的。但如果我们在一个十分复杂的句子中需要包含日期和时间,我们是否需要对每个字符使用转义字符?
答案当然是不。我们使用另一个函数strftime()。
strftime()有两个好处。第一个好处我们并不在本文讨论范围内-如果你使用setlocale()函数,你可以通过strftime得到相应语言的月份的名称。另外的一个好处是你可以将特别的日期和时间的格式化字符包含在你的字符串中。这同时也意味着无论你是否要学习date()函数的所有特殊格式化字符,你都必须学习一整套完全不同的格式化字符。
strftime()工作的方式和date()没有什么不同,除了特殊格式化字符的前面必须添加一个百分号%。如果用strftime()函数,前面例子的代码如下:
<?
echo strftime ("%Hh%M %A %d %b" ,time());
?>
结果为:
18h24 Sunday 21 May
这也许看起来将简化繁,但考虑一下如果你所需要的显示的为"Today is Sunday 21 May 2000. The time is somewhere close to 18h24." 我想使用date()函数无疑令人感到厌烦。
在开始的时候,我提及我们有两种方式可以从Unix时间戳值中得到有用的数据。我们刚刚了解了date()和strftime()。另一个getdate()。这个函数只需要Unix 的时间戳值作为参数,而函数的返回值为日期和时间的数组。
下面是一个例子:
<?
$date_time_array = getdate (time());
echo $date_time_array[ "weekday"];
?>
返回的结果为:
Sunday
除了"weekday",该数组的其他部分为:
"seconds" –秒
"minutes" –分
"hours" –小时
“mday" - 本月的第几天
"wday" -本周的第几天(数字)
"mon" -月(数字)
"year" –年
"yday" - r本年的第几天(数字)
"month" -月份全名
我们现在可以得到容易辨认的日期和时间。那么其他呢?
**转换现在的日期为Unix的时间戳值
通常你必须处理一些日期或时间格式的数据。打开M$的一个Access数据库,所有的日期都以YYYY/MM/DD的格式存储,加入目前的日前即为2000/05/27。Mktime()函数可以将一个时间转换成Unix的时间戳值。
函数的格式为:int mktime(int hour, int minute, int second, int month, int day, int year, int [is_dst] );
从左往右你必须提供小时、分、秒、月、天和年。最后一个参数用于指定你是否处于夏令时,此参数是可选的,所以我们将忽略它。
代码如下:
<?
echo mktime (0, 0,0 ,5, 27,2000 );
?>
由于不知道小时、分和秒同时这些参数必须填写,我将其设置为0。设置为0意味着时间为午夜。
<?
$access_date = "2000/05/27";
//explode()函数用一个字符串作为分界来分解另一个字符串。这个例子$access_date通过字符串”/”来分解
$date_elements = explode("/" ,$access_date);
// 此时
// $date_elements[0] = 2000
// $date_elements[1] = 5
// $date_elements[2] = 27
echo mktime (0, 0,0 ,$date_elements [1], $date_elements[ 2],$date_elements [0]);
?>
我们看一个比从Access数据库单纯获得日期更复杂的情况,我们得到一个以下格式的日期和时间:2000/05/27 02:40:21 PM
<?
// 来自Access的字符串
$date_time_string = "2000/05/27 02:40:21 PM";
// 将字符串分解成3部分-日期、时间和上午/下午
$dt_elements = explode(" " ,$date_time_string);
// 分解日期
$date_elements = explode("/" ,$dt_elements[ 0]);
// 分解时间
$time_elements = explode(":" ,$dt_elements[ 1]);
// 如果是下午,我们将时间增加12小时以便得到24小时制的时间
if ($dt_elements [2]== "PM") { $time_elements[ 0]+=12;}
// 输出结果
echo mktime ($time_elements [0], $time_elements[ 1], $time_elements[ 2], $date_elements[1], $date_elements[2], $date_elements[0]);
?>
**修改日期
有时我们需要知道6小时以后是什么时间,35天前的日期或者从你最后一次玩Quake3后已过去多少秒。我们已经知道如何用mktime()函数从单独的日期和时间中获得Unix的时间戳值。如果我们需要的并非目前日期和时间的Unix时间戳值,我们该咋办?下面是一些练习可以帮助说明我们后面所要做的。
正如前面所见,mktime()使用以下参数:小时、分、秒、月、天和年。想想第二节,getdate()函数可以为我们获得这些参数。
<?
// 将目前的时间戳值放入一数组内
$timestamp = time();
echo $timestamp;
echo "p";
$date_time_array = getdate( $timestamp);
// 用mktime()函数重新产生Unix时间戳值
$timestamp = mktime($date_time_array ["hours"], $date_time_array["minutes" ],$date_time_array[ "seconds"],$date_time_array ["mon"], $date_time_array["mday" ],$date_time_array[ "year"]);
echo $timestamp;
?>
看起来有一些令人感到迷惑。我将用一些变量来使上面的程序看起来更容易了解。
<?
// 将目前的时间戳值放入一数组内
$timestamp = time();
echo $timestamp;
echo "p";
$date_time_array = getdate( $timestamp);
$hours = $date_time_array[ "hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array[ "seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
// 用mktime()函数重新产生Unix时间戳值
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day,$year);
echo $timestamp;
?>
现在我们将由getdate()所产生的时间戳值放入相对应的名称变量中,所以代码变得相对容易阅读和理解。现在如果我们需要在目前的时间上加上19个小时,我们用$hours+19代替mktime()函数中的$hours。mktime()将自动为我们将时间转到第二天。
<?
// 将目前的时间戳值放入一数组内
$timestamp = time();
echo strftime( "%Hh%M %A %d %b",$timestamp);
echo "p";
$date_time_array = getdate($timestamp);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
// 用mktime()函数重新产生Unix时间戳值
// 增加19小时
$timestamp = mktime($hours + 19, $minutes,$seconds ,$month, $day,$year);
echo strftime( "%Hh%M %A %d %b",$timestamp);
echo "br~E after adding 19 hours";
?>
运行后得到:
14h58 Saturday 03 Jun
09h58 Sunday 04 Jun
~E after adding 19 hours
减少时间也是同样的-你只需要减少相应变量的值即可。
得到两个不同时间值的差同样也是非常简单。你所需要做的只是将两个时间值转换为Unix的时间戳值,然后两者相减即可。两者之差即为两个时间所相隔的秒数。另外一些算法可以很快地将秒转为天、小时、分和秒。
**为PHP添加DateAdd函数
正如在文章一开始我所说的-写本文的原因是因为我在PHP中找不到类似ASP的DateDiff函数。在介绍完PHP是如何处理日期和时间,让我们将ASP中常用的两个函数移植到PHP。第一个函数是DateAdd。
根据Vbscript的文档,DateAdd(interval,number,date)函数的定义为“返回已添加指定时间间隔的日期。”
Inetrval为表示要添加的时间间隔字符串表达式,例如分或天;number为表示要添加的时间间隔的个数的数值表达式;Date表示日期。
Interval(时间间隔字符串表达式)可以是以下任意值:
yyyy year年
q Quarter季度
m Month月
y Day of year一年的数
d Day天
w Weekday一周的天数
ww Week of year周
h Hour小时
n Minute分
s Second秒
w、y和d的作用是完全一样的,即在目前的日期上加一天,q加3个月,ww加7天。
我们可以将上面的代码保存为dateadd.inc文件,然后运行以下代码:
<?
include('dateadd.inc');
$temptime = time();
echo strftime( "%Y-%m-%d %H:%M",$temptime);
$temptime = DateAdd("n" ,50,$temptime);
echo " p ";
echo strftime( "%Y-%m-%d %H:%M",$temptime);
?>
我们将得到:
2007-05-15 11:14 p 2007-05-15 12:04
为PHP添加DateDiff函数
现在DateAdd已经完成,那么DateDiff呢?
根据文档,DateDiff(interval,date1,date2)函数的定义为“返回两个日期之间的时间间隔”。
Intervals参数的用法与DateAdd函数中的相同。出于避免过于复杂的考虑,我们决定忽略Vbscript中DateDiff函数中其它复杂的参数,即其两个可选的参数变量[firstdayofweek[, firstweekofyear]](它们用于决定星期中第一天是星期天还是星期一和一年中第一周的常数。而且我们只允许intervals有以下五个值:"w"(周)、"d"(天)、"h"(小时)、"n"(分钟)和"s"(秒)。
Let's see what we can come up with: 下面的代码是我们所需要的:
<?
Function DateDiff ($interval, $date1,$date2) {
// 得到两日期之间间隔的秒数
$timedifference = $date2 - $date1;
switch ($interval) {
case "w": $retval = bcdiv($timedifference ,604800); break;
case "d": $retval = bcdiv( $timedifference,86400); break;
case "h": $retval = bcdiv ($timedifference,3600); break;
case "n": $retval = bcdiv( $timedifference,60); break;
case "s": $retval = $timedifference; break;
}
return $retval;}
?>
将上面的代码存为datediff.inc文件,然后运行下面的代码:
<?
include('datediff.inc');
include('dateadd.inc');
$currenttime = time();
echo "Current time: ". strftime("%Hh%M %A %d %b" ,$currenttime)."br";
$newtime = DateAdd ("n",50 ,$currenttime);
echo "Time plus 50 minutes: ". strftime("%Hh%M %A %d %b" ,$newtime)."br";
$temptime = DateDiff ("n",$currenttime ,$newtime);
echo "Interval between two times: ".$temptime;
?>
如果一切顺利,你可以看到以下结果:
Current time: 16h23 Saturday 03 Jun
Time plus 50 minutes: 17h13 Saturday 03 Jun
Interval between two times: 50
如果你在Unix机器上运行PHP,你必须编译PHP支持BC高精度函数。你必须从以下地址
现在你已经得到处理日期和时间的函数,剩下的就是如何将其运用到你的PHP程序中。
实验六 面向对向PHP实验
【实验目的】
1.掌握面向对象的概念,类、属性和操作。
2.PHP中创建类、属性和操作,以及构造函数的作用和继承等
【实验内容】
1、《PHP和MySQL Web开发》教材第6章文字内容和实例
2、补充以下练习内容
练习一
<?
class person {
public $name = "ghosoul";
public $age =24;
public function __construct() {
$this->sex = 'man';
}
private $hight = '178cm';
public function getHigh
展开阅读全文