其他功能
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的图像处理函数部分)
这段代码在其他页面中通过以下标记
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\n";
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 ):
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 ()
{
// 这里是你的页面内容
}
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 - 数组排序,另有其他几种排序函数可供使用
list - 列出数组元素
each - 返回下一个key/value对
current - 返回当前数组元素
next,PRev - 传回当前数组元素前后指针
日期和时间
checkdate - 验证日期/时间格式
date - 生成日期/时间格式
time - 当前时间信息
strftime - 格式化日期/时间
目录、文件系统
chdir - 改变目录
dir - 目录类别
opendir, readdir, closedir - 开启、读取、关闭目录
fopen, fclose - 开启、关闭文件
fgets, fgetss - 逐行读取内容
file - 将整个文件读入一个数组变量中
正则表达式
ereg - 匹配正则表达式
eregi - 大小写非敏感匹配正则表达式
ereg_replace -匹配正则表达式并替换
eregi_replace -大小写非敏感匹配正则表达式并替换
split - 依规则切开字符串并以数组形势存储
字符串
AddSlashes - 加上斜杠后使用字符串
echo - 输出一个或多个字符串
join, implode - 将数组元素合并为字符串
htmlentities, htmlspecialchars - 将HTML特殊字符转换为HTML标记形式
split - 依规则切开字符串并以数组形势存储
5.6 扩展我们的范例主页
我们将使用以上提到的一些函数和思想为我们的范例主页添加更多的动态内容。我们可以在每个页面的顶部加上导航栏,同时使得当前页自动的不被链接显示;同时还可以添加一个用户验证表单以便上传音乐、图像等文件并自动更新页面。
导航栏
实际上就是在footer.inc文件中加上一段代码。假设你的web站点中所有后缀为.php3的文件都会出现在导航栏中,以下就是被存为include/navbar.inc的代码:
/* 输出该导航栏,链接所有除当前页的站内.php3文件 */
# 读取目录
$d = dir("./");
echo "
| \n";
while($entry = $d->read())
{
// 忽略无文件情况
if ( !is_file($entry) )
continue;
/* 将文件名与扩展名分开。由于.是正则表达式特殊字符,应该用\引出 */
list($filenm, $fileext) = split("\.",$entry, 2);
// 忽略非.php3文件情况
if( $fileext != "php3" )
continue;
/* 现在我们已经把.php3文件都选出,下面搜寻文件中的第一行(标题)
类似$title="something";
并将以上标题内容分开,用作链接文字 */
$linknm = "";
$fp=fopen($entry,"r");
while($buffer=fgets($fp, 4096))
{
$buffer = trim($buffer);
// 我们已经把每个文件的标题放在文件的第一行以便搜索
// 但是当你改变变量名称时可能会带来大麻烦
if (ereg("title *= *\"", $buffer))
{
/* 我们已经取得了标题内容并可以在此基础上
进行去除空格等处理。
必须以PHP代码方式处理,比如$title = "blah blah" */
eval($buffer);
// 然后将链接文字显示为标题文字
$linknm = $title;
break;
}
}
fclose($fp);
if ( $entry == basename($PHP_SELF) )
echo "$linknm";
else
echo "$linknm";
echo " | ";
}
$d->close();
echo "
?>
照片收藏夹
我们将引用基于HTTP的验证、文件系统函数和文件上传功能维护放置图像文件的目录。
同时我们需要建立一个可以列出在该目录下所有照片的页面。
文件上传
include("include/common.inc");
// 我们在这里再做一次用户验证
if(!isset($PHP_AUTH_USER))
{
Header("WWW-Authenticate: Basic realm=\"$MySiteName\"");
Header("HTTP/1.0 401 Unauthorized");
echo "Sorry, you are not authorized to upload files\n";
exit;
}
else
{
if ( !($PHP_AUTH_USER==$MyName && $PHP_AUTH_PW==$MyPassWord ) )
{
// 如果是错误的用户名称/密码对,强制再次认证
Header("WWW-Authenticate: Basic realm=\"My Realm\"");
Header("HTTP/1.0 401 Unauthorized");
echo "ERROR : $PHP_AUTH_USER/$PHP_AUTH_PW is invalid.
";
exit;
}
}
if ( $cancelit )
{
// 当浏览者按下"取消"按钮则转向首页面
header ( "Location: front_2.php3" );
exit;
}
function do_upload () {
global $userfile, $userfile_size, $userfile_name, $userfile_type;
global $local_file, $error_msg;
global $HTTP_REFERER;
if ( $userfile == "none" ) {
$error_msg = "You did not specify a file for uploading.";
return;
}
if ( $userfile_size > 2000000 )
{
$error_msg = "Sorry, your file is too large.";
return;
}
// Wherever you have write permission below...
$upload_dir = "photos";
$local_file = "$upload_dir/$userfile_name";
if ( file_exists ( $local_file ) ) {
$error_msg = "Sorry, a file with that name already exists";
return;
};
// 你还可以由此检查文件名称/类型对以确定是何种文件:gif,jpg,mp3…
rename($userfile, $local_file);
echo "The file is uploaded
\n";
echo "Go Back
\n";
}
$title = "Upload File";
include("include/header.inc");
if (empty($userfile) || $userfile=="none")
{
// 输出以下表单
?>
(You may notice a slight delay while we upload your file.)
} else {
if ( $error_msg ) { echo "$error_msg
"; }
if ( $sendit ) {
do_upload ();
}
}
include("include/footer.inc");
?>
照片图库
include("include/common.inc");
$title = "Gallery";
include("include/header.inc");
?>
Here are some of our family photos. This PHP script can really
be made better, by splitting into multiple pages.
$d = dir("photos");
while($entry = $d->read())
{
if (is_file("photos/$entry"))
echo "
}
$d->close();
?>
include("include/footer.inc");
?>
另外,你可以在文件上传的表单中加上一个输入元素去描述该上传的文件。这个元素将被存储在文件中,然后被以上的照片图库的那段代码所读出并显示出来。

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



Alipay Php ...

Artikel membincangkan pengikatan statik lewat (LSB) dalam PHP, yang diperkenalkan dalam Php 5.3, yang membolehkan resolusi runtime kaedah statik memerlukan lebih banyak warisan yang fleksibel. Isu: LSB vs polimorfisme tradisional; Aplikasi Praktikal LSB dan Potensi Perfo

JWT adalah standard terbuka berdasarkan JSON, yang digunakan untuk menghantar maklumat secara selamat antara pihak, terutamanya untuk pengesahan identiti dan pertukaran maklumat. 1. JWT terdiri daripada tiga bahagian: header, muatan dan tandatangan. 2. Prinsip kerja JWT termasuk tiga langkah: menjana JWT, mengesahkan JWT dan muatan parsing. 3. Apabila menggunakan JWT untuk pengesahan di PHP, JWT boleh dijana dan disahkan, dan peranan pengguna dan maklumat kebenaran boleh dimasukkan dalam penggunaan lanjutan. 4. Kesilapan umum termasuk kegagalan pengesahan tandatangan, tamat tempoh, dan muatan besar. Kemahiran penyahpepijatan termasuk menggunakan alat debugging dan pembalakan. 5. Pengoptimuman prestasi dan amalan terbaik termasuk menggunakan algoritma tandatangan yang sesuai, menetapkan tempoh kesahihan dengan munasabah,

Sesi rampasan boleh dicapai melalui langkah -langkah berikut: 1. Dapatkan ID Sesi, 2. Gunakan ID Sesi, 3. Simpan sesi aktif. Kaedah untuk mengelakkan rampasan sesi dalam PHP termasuk: 1. Gunakan fungsi Sesi_Regenerate_ID () untuk menjana semula ID Sesi, 2. Data sesi stor melalui pangkalan data, 3.

Penerapan prinsip pepejal dalam pembangunan PHP termasuk: 1. Prinsip Tanggungjawab Tunggal (SRP): Setiap kelas bertanggungjawab untuk hanya satu fungsi. 2. Prinsip Terbuka dan Tutup (OCP): Perubahan dicapai melalui lanjutan dan bukannya pengubahsuaian. 3. Prinsip Penggantian Lisch (LSP): Subkelas boleh menggantikan kelas asas tanpa menjejaskan ketepatan program. 4. Prinsip Pengasingan Antara Muka (ISP): Gunakan antara muka halus untuk mengelakkan kebergantungan dan kaedah yang tidak digunakan. 5. Prinsip Inversi Ketergantungan (DIP): Modul peringkat tinggi dan rendah bergantung kepada abstraksi dan dilaksanakan melalui suntikan ketergantungan.

Bagaimana untuk menetapkan keizinan UnixSocket secara automatik selepas sistem dimulakan semula. Setiap kali sistem dimulakan semula, kita perlu melaksanakan perintah berikut untuk mengubahsuai keizinan UnixSocket: sudo ...

Bagaimana cara debug mod CLI dalam phpstorm? Semasa membangun dengan PHPStorm, kadang -kadang kita perlu debug PHP dalam mod Interface Line Command (CLI) ...

Mengikat statik (statik: :) Melaksanakan pengikatan statik lewat (LSB) dalam PHP, yang membolehkan kelas panggilan dirujuk dalam konteks statik dan bukannya menentukan kelas. 1) Proses parsing dilakukan pada masa runtime, 2) Cari kelas panggilan dalam hubungan warisan, 3) ia boleh membawa overhead prestasi.
