5.1 生成图像
php可以操作处理图像。假如你已经安装了gd库,你甚至可以利用php生成图像。
header(content-type: image/gif);
$string=implode($argv, );
$im = imagecreatefromgif(images/button1.gif);
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im)-7.5*strlen($string))/2;
imagestring($im,3,$px,9,$string,$orange);
imagegif($im);
imagedestroy($im);
?>
(译者注:以上代码段缺少注释,请读者参考php manual的图像处理函数部分)
这段代码在其他页面中通过以下标记调用,然后以上的那段button.php3代码取得text值并在另外取得的图像文件中加上该值--在以上的代码中该图像文件是images/button1.gif--最后输出到浏览器。假如你想在表单域中使用图像按钮,但是又不希望在每次按钮上的文字改变后不得不重新生成新的图像,就可以利用这样简单的方法动态生成图像文件。
5.2 cookies
php支持基于http的cookies。在需要时你可以像使用一般变量一样方便的使用cookie。cookies是浏览器保存于客户端的一些信息片段,由此你可以知道是否一台特定pc上的任何人都访问过你的站点,浏览者者在你的站点上的踪迹等等。使用cookies的典型例子就是对浏览者偏好的甄别。cookies由函数setcookie()设定。与输出http标头的函数header()一样,setcookie()必须在任何实际内容杯输出到浏览器之前调用。以下是一个简单例子:
if (empty($visitedbefore))
{
// 假如没有设定cookie,为cookie赋上当前时间值
// 函数中的最后一个参数声明了该cookie保存的时间
// 在这个例子中是1年
// time()函数返回自1970年1月1日以来的以秒数计的时间
setcookie(visitedbefore,time(), time() (60*60*24*365));
}
else
{
// 欢迎浏览者再次光临
echo hello there, welcome back
;
// 读取cookie并判定
if ( (time() - $visitedbefore) >= (60*60*24*7) )
echo why did you take a week to come back. you should be here more often!? ;
}
?>
5.3 基于http验证
基于http验证当php以cgi模式运行时不能实现。我们可以使用函数header()发送http标头强制验证,客户端浏览器则弹出供输入用户名和密码的对话框。这两个变量被储存在$php_auth_user和$php_auth_pw中,你可以使用这两个变量验证合法并答应进入。以下的例子通过用户名称/密码对为tnc/nature的验证一名用户的登录:
if(!isset($php_auth_user))
{
header(www-authenticate: basic realm=my realm);
header(http/1.0 401 unauthorized);
echo text to send if user hits cancel button ;
exit;
}
else
{
if ( !($php_auth_user==tnc && $php_auth_pw==nature) )
{
// 假如是错误的用户名称/密码对,强制再验证
header(www-authenticate: basic realm=my realm);
header(http/1.0 401 unauthorized);
echo error : $php_auth_user/$php_auth_pw is invalid.;
exit;
}
else
{
echo welcome tnc!;
}
?>
事实上再实际引用中不大可能如上面使用代码段明显的用户名称/密码对,而是利用数据库或者加密的密码文件存取它们。
5.4 文件上传
你可以利用php实现文件的功能,注重客户端的浏览器应该是netscape3以上或者ie3以上。以下就是该功能的简单演示:
( upload.html ):
upload your file
enctype=multipart/form-data method=post>
name=max_file_size value=2000000>
name=uploadfile size=24 maxlength=80>
name=sendit>
name=cancelit>
(you may notice a slight
delay while we upload your file.)
下面是处理上传的文件:
( receiver.php3 ):
function do_upload ()
{
global $uploadfile, $uploadfile_size;
global $local_file, $error_msg;
if ( $uploadfile == none )
{
$error_msg = you did not specify a file for uploading.;
return;
}
if ( $uploadfile_size > 2000000 )
{
$error_msg = sorry, your file is too large.;
return;
}
$the_time = time ();
// 你需要对以下目录有写权限
$upload_dir = /local/uploads;
$local_file = $upload_dir/$the_time;
if ( file_exists ( '$local_file' ) )
{
$seq = 1;
while ( file_exists ( $upload_dir/$the_time$seq ) ) { $seq ; }
$local_file = $upload_dir/$the_time$seq;
};
rename ( $uploadfile, $local_file );
display_page ();
}
function display_page ()
{
// 这里是你的页面内容
}
php3 receiving script
if ( $error_msg ) { echo $error_msg
; }
if ( $sendit )
{
do_upload ();
}
elseif ( $cancelit )
{
header ( location: $some_other_script );
exit;
}
else
{
some_other_func ();
}
?>
5.5 常用函数
我们简单来看看一些常用的函数。
数组
array - 生成数组
count - 数组元素个数
sort - 数组排序,另有其他几种排序函数可供使用
