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<BR>";
// 读取cookie并判别
if ( (time() - $VisitedBefore) >= "(60*60*24*7)" )
echo "Why did you take a week to come back. You should be here more often!? ";
}
?>
文件上传
<?
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.<P>";
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<BR>\n";
echo "<A HREF=\"$HTTP_REFERER\">Go Back</A><BR>\n";
}
$title = "Upload File";
include("include/header.inc");
if (empty($userfile) || $userfile=="none")
{
// 输入以下表单
?>
<FORM ACTION="<? echo "$PHP_SELF"; ?>" ENCTYPE="multipart/form-data" METHOD=POST>
<INPUT TYPE="HIDDEN" NAME="MAX_FILE_SIZE" VALUE="2000000">
<INPUT TYPE="FILE" NAME="userfile" SIZE="24" MAXLENGTH="80">
<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload File!" NAME="sendit">
<INPUT TYPE="SUBMIT" VALUE="Cancel" NAME="cancelit"><BR>
</FORM>
<I><FONT SIZE="2">(You may notice a slight delay while we upload your file.)</FONT></I>
<?
} else {
if ( $error_msg ) { echo "<B>$error_msg</B><BR><BR>"; }
if ( $sendit ) {
do_upload ();
}
}
include("include/footer.inc");
?>
照片图库
<?
include("include/common.inc");
$title = "Gallery";
include("include/header.inc");
?>
<P>
Here are some of our family photos. This PHP script can really
be made better, by splitting into multiple pages.
</P>
<?
$d = dir("photos");
while($entry = $d->read())
{
if (is_file("photos/$entry"))
echo "<IMG SRC=\"photos/$entry\">\n";
}
$d->close();
?>
<?
include("include/footer.inc");
?>