Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->PHP4/PHP5 ->正文

初学PHP的18个基础例程

来源: 作者:webmaster 时间:2007-04-16 点击: [收藏] [投稿]

$login="你已经登陆"; // succesful message when user are logged in ( NOT NECESSARY )
//===========================

// DO NOT EDIT NOTHING BELOW!

if ((!isset($PHP_AUTH_USER)) || (!isset($PHP_AUTH_PW))) {

/* No values: send headers causing dialog box to appear */

header('WWW-Authenticate: Basic realm="$site_com"');

header('HTTP/1.0 401 Unauthorized');

echo '<h1>访问的页面被限制.</h1>请检查你的用户名或密码是否正确,正常登陆本站才能查看 本站的内容';
exit;

} else if ((isset($PHP_AUTH_USER)) && (isset($PHP_AUTH_PW))){
/* Values contain some values, so check to see if they're correct */

if (($PHP_AUTH_USER != "$admin_user_name") || ($PHP_AUTH_PW != "$admin_password")) { /* If either the username entered is incorrect, or the password entered is incorrect, send the headers causing dialog box to appear */

header('WWW-Authenticate: Basic realm="$site_com"');
header('HTTP/1.0 401 Unauthorized');
echo '<h1>访问的页面被限制.</h1>请检查你的用户名或密码是否正确,正常登陆本站才能查看 本站的内容';
exit;
} else if (($PHP_AUTH_USER == "$admin_user_name") || ($PHP_AUTH_PW == "$admin_password")) { echo "$login
";
}
}

?>

  保存为log.php,把下列代码加到head页内,那么将会保护所有的页面。

<? require("log.php");?>

  十四、制作一个自动转向:

<?php

header("Refresh:0; url=http://webjx.com");

?>

  必须保存为php文件才可以。

  十五。制作用户列表:

<?
$customer[0] = 'webjx';
$customer[1] = 'web';
$customer[2] = 'mutou';
$customer[3] = 'chuxia';
$customer[4] = 'shenhua';
echo "第3个用户是: $customer[2]";

echo "

所有的用户:

";

$number = 5;
$x = 0;
while ($x < $number) {
$customernumber = $x + 0;
echo "Costumer Name $customernumber is $customer[$x]
";
++$x;
}
?>
将会显示:
The third customer is mutou

所有的用户:

Costumer Name 0 is webjx
Costumer Name 1 is web
Costumer Name 2 is mutou
Costumer Name 3 is chuxia
Costumer Name 4 is shenhua

  另一种读取数组的方法:

<?
echo "3 of my customer are: $customer[0], " . " $customer[1] and " . " $customer[2]. " . "
";
?>

将会显示:
3 of my customer are: webjx, web and mutou.

  十六.统计某个目录下文件的数量:

<?
echo("There is ");
$dir = "/path/to/the/folder/in/your/computer ";
$count = 0;
$handle=opendir($dir);
while (($file = readdir($handle))!== false){ if ($file != "." && $file != "..") { $count++; }}
echo $count;

echo(" Pages For You To Search In This Directory.");
?>

  十七.显示当前日期或最新更新的日期:

<?php echo date("F d Y"); ?>

显示更新日期:

<?php echo "Last Modified: " . date ("m/d/y.", getlastmod());?>

  十八.重复一个字符多次的代码:



<?
echo str_repeat("-", 30);
?>



 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:HTTP参考   下一篇:手工注射php学习

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号