首页 > 视觉IT > 编程开发编程开发

初学PHP的18个基础例程

system2021-12-15 15:35:09【编程开发】0人已围观

简介如何创建我们的第一个PHP页面呢?非常简单的!选择我们使用的一个最好的设计工具,当然你也可以 只使用记事本。创建之后记得要保存为扩展名为PHP的文件,然后

如何创建我们的第一个PHP页面呢?非常简单的!选择我们使用的一个最好的设计工具,当然你也可以 只使用记事本。创建之后记得要保存为扩展名为PHP的文件,然后传到我们的服务器上。
在编写PHP程序之前通常我们需要配置我们的环境,也就是说服务器要支持PHP才能行啊
一、PHP的基本结构:
使用Include函数
<Html> 
<Head> 
<title>Your page Subject and domain name</title>
上面内容为我们使用的每个页面的标题,不要动。
每个页的头部:
<Meta NAME="" CONTENT="">
"" your others meta tag
"" your others meta tag
"" your others meta tag
"" your others meta tag
"" your others meta tag
"" your others meta tag
"" your others meta tag
重要的javascripts代码放这
CSS设置放这
上述内容保www.516fc.net存为header.php,使每个页面的头部都是一样的。
<?PHP include("header.php")?> 
</head>
<body> 
你的www.hthrt.com页的所有内容
</body>
</html> 
保存www.bjhsjf.com为footer.php,使每个页面的底部都一样。
<? include("footer.php");?>
填写我们的版权信息
</body>
</html> 
二:如何输出www.yiwenxiang.com.cn文本或者把文本建立连接用PHP
在PHP中如何显示文本呢?使用下面的命令:
<?php echo "Hello in php";?>
如何创建www.dd951.com一个连接呢?
<?php echo "<a href=\"http://www.chuangxin.biz\">www.chuangxin.biz</a>";?> 
如何创建一个有style的连接呢?
<?php echo "<font style=\"color:blue;font-size:10px;font- family:verdana;\">Free Scripts By: <a href=\"http://www.chuangxin.biz\" target=\"_blank\" title=\"Free Scripts By: chuangxin.biz\">http://www.chuangxin.biz</font></a>";?> 
"echo"是用www.515tg.com来显示输出的。
三:如何实现分页:
如果好多内容在www.fuweitek.com你的页面中这时我们就考虑用分页形式来实现显示了。
简单的分页代码:
<html>
<head>
<title>chuangxin.biz</title>
</head>
<body text="#000000">
<center>
<table width="500" border="3">
<tr>
<td width="140" height="400" valign="top">
<p align="center">
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php'>home</a><br />
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php?p=Page1'>page 1</a><br />
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php?p=Page2'>page 2</a><br />
</p></td>
<td width="360" height="400" valign="top">
<p>
<? 
function index()
{
echo "<p align=center>Welcome to this tutorial<br />Here you can find funny 
tricks</p><br /><br /><br /><br /><br /><br />"; }
$choice=$_GET['p'];

switch($choice)
{
case "Page1":

echo "<p align=center>Page1 text, img and so on here</p>";
break;

case "Page2":

echo "<p align=center>Page2 text, img and so on here</p>";
break;

default:
index();
}

?>

</p>
</td>
</tr>
</table> </center>
</body>
</html>
以上文www.kshyf.com件必须保存为index.php
高级www.szhrtz.com分页的代码:
<html>
<head>
<title>chuangxin.biz</title>
</head>
<body text="#000000">
<center>
<table width="500" border="3">
<tr>
<td width="140" height="400" valign="top">
<p align="center">
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php'>home</a><br />
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php?action=contact_us'>page 1</a><br />
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/'index.php?action=link_us'>page 2</a><br />
</p></td>
<td width="360" height="400" valign="top">
<p>
<? 

if (isset($_GET['action'])) $PAGE = $_GET['action']; 

else $PAGE = 'home';
switch ($PAGE) {
//1- index
case 'home':
include ('incl/home.php');
break;

//2-contact form
case 'contact_us':
include ('incl/contact_us.php');
break;
//3-Link us
case 'link_us':
include ('incl/link_us.php');
break;
default:
echo '<p align=center>Error 404! the page you request doesn t exist or as been temporarely unaccessible</p>';
break;
}

?>

</p>
</td>
</tr>
</table> 
</center>
</body>
</html>
提供了www.penqiang.net演示的下载,请自己去试试!
四:页面加载时间的代码:
<?php

echo ("Page Took :");
$load = microtime();
print (number_format($load,2));
echo (" Sec To Load.");

?> 
五:显示从哪个地址转到你当前访问的站的代码:
<?php

echo "You Came From:<br />";
echo $_SERVER['HTTP_REFERER']; 
?> 
六:设置IP地址的转向:屏蔽IP
<?php

if(($REMOTE_ADDR == "22.22.22.22")):// ip address

print "<meta http-equiv='refresh' content='0; url=http://www.sina.com'>";// url to redicted
endif; 
?> 
七:随即显示标题的代码:
<Title><?php include("title.php");?></Title>
要事先做一个title.php文件啊
八;如何用PHP来建立一个HTML 的table
<?php
echo"<html>\n";
echo"<head>\n";
echo"<title>allo</TITLE>\n";
echo"</head>\n";
echo"<body topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"5\" 
bgcolor=\"#ffffff\">\n";
echo"<center>\n";
echo"<table width=\"500\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n";
echo"<tr>\n";
echo"<td>\n";
echo"<p align=\"center\">\n";
echo"
\n";
echo"Text Goes Here\n";
echo"</p>\n";
echo"</td>\n";
echo"</tr>\n";
echo"</table>\n";
echo"</center>";
echo"</body>\n";
echo"</html>";
?> 
九、声明字符串变量:
建立一个页面 ( config.php ) 粘贴下面代码:
<?
$name="Name";
$salutation="Hey!";
$title="chuangxin.biz"; 
$copyrights="©2005 chuangxin.biz"; 
$link_1="Home"; 
?>
创建一个页面( test.php )把下列代码
<? include("config.php");?>
放在<html><head>之前,然后在test.php页里来调用上面的字符串
<p align="center"><? echo("$title");?></p> 
也可以这样来声明:
<?
$surname1="Marco"; $lastname1="Charette"; 
?> 
调用:
<? echo("$surname1 $lastname1");?>
十、显示你的服务器的信息:
<? phpinfo(); ?> 
十一:实现页面的跳转:
创建"jump.php"页面并且设置变量来隐藏连接的地址
<?
if ($id == "1"){$link = "http://chuangxin.biz";}
if ($id == "2"){$link = "http://google.com";} 

header("Location: $link"); // 实现了跳转
exit();
?> 
保存之后,在另外一个页中加入如下代码:
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/"jump.php?id=1">Visit This Link</a>
跳转并且还把访客保存在你的页面内:
<?
if ($id == "1"){$link = "http://chuangxin.biz";}
if ($id == "2"){$link = "http://google.com";} 

echo "<frameset rows=\"0,100%\" border=\"0\">\n";
echo "<frame src=\"jump.php\" name=\"head\" scrolling=\"no\" marginheight=\"0\" frameborder=\"0\" noresize>\n";
echo "<frame src=\"$link\" name=\"body\" marginheight=\"10\" marginwidth=\"10\" frameborder=\"0\" noresize>\n";
echo "</frameset>\n";

header("Location: $link"); 
exit();
?> 
保存为jump.php然后再其他页内加入连接代码:
<a href=http://www.chuangxin.biz/htmldata/2005-03-05/"jump.php?id=1">Visit This Link</a>
十二、保护页面:
<? $Referer = getenv("HTTP_REFERER");
if (!strchr($Referer, "http://chuangxin.biz/page.php")) { 
echo "<script>alert('你不能访问这个页面');
window.location='http://chuangxin.biz';</script>";
exit(); } ?> 
十三:限制访问某个页面
// choose a user name and password between the " " symbol
//===========================
$admin_user_name="admin"; // your admin username
$admin_password="pass"; // your password
$site_com="chuangxin.biz"; // your website name WITHOUT http:// and www 
$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<br />";
}
}

?>
保存为log.php,把下列代码加到head页内,那么将会保护所有的页面。
<? require("log.php");?> 
十四、制作一个自动转向:
<?php

header("Refresh:0; url=http://chuangxin.biz");

?> 
必须保存为php文件才可以。
十五。制作用户列表:
<?
$customer[0] = 'webjx';
$customer[1] = 'web';
$customer[2] = 'mutou';
$customer[3] = 'chuxia';
$customer[4] = 'shenhua';
echo "第3个用户是: $customer[2]";

echo "<br /><br />所有的用户:<br /><br />";

$number = 5;
$x = 0;
while ($x < $number) {
$customernumber = $x + 0;
echo "Costumer Name $customernumber is $customer[$x]<br />";
++$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]. " . "<br />"; 
?> 

将会显示:
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);
?> 


PHP源码-利用 QQWry.Dat 实现 IP 地址高效检索
根据 LumaQQ 开发者文档中的纯真 IP 数据库格式详解,我编写了一个 PHP 的查询 IP 所在地区信息的类。 在编写过程中发现纯真 IP 数据库格式详解中关于记录区的描述不是很全面,不过出入也不是很大,所以我没必要再写一份纯真 IP 数据库的格式说明了,大家感兴趣的话,读一读下面的代码应该就能看出来了。代码中加了很详细的注释,应该很容易读懂的。
在创建这个类的一个实例后,实例中就保存了打开的文件指针和一些查询需要的信息,每次查询时不需要重新打开文件,直到页面执行结束后,打开的文件才会自动关闭。这样。在一个页面内进行多次查询时,效率是很高的。并且此类不仅可以直接查询 IP,还可以自动将域名解析为 IP 进行查询。

下面是程序代码:
<?php
/**
* @author 马秉尧
*/

class IpLocation {

/**
* QQWry.Dat文件指针
* @var resource
*/

var $fp;

/**
* 第一条IP记录的偏移地址
*
* @var int
*/

var $firstip;

/**
* 最后一条IP记录的偏移地址
*
* @var int
*/

var $lastip;

/**
* IP记录的总条数(不包含版本信息记录)
*
* @var int
*/

var $totalip;

/**
* 返回读取的长整型数
*
* @access private
* @return int
*/

function getlong() {

//将读取的little-endian编码的4个字节转化为长整型数

$result = unpack('Vlong', fread($this->fp, 4));

return $result['long'];

}

/**
* 返回读取的3个字节的长整型数
*
* @access private
* @return int
*/

function getlong3() {

//将读取的little-endian编码的3个字节转化为长整型数

$result = unpack('Vlong', fread($this->fp, 3).chr(0));

return $result['long'];

}

/**
* 返回压缩后可进行比较的IP地址
*
* @access private
* @param string $ip
* @return string
*/

function packip($ip) {

// 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,

// 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串

return pack('N', intval(ip2long($ip)));

}

/**
* 返回读取的字符串
*
* @access private
* @param string $data
* @return string
*/

function getstring($data = "") {

$char = fread($this->fp, 1);

while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束

$data .= $char; // 将读取的字符连接到给定字符串之后

$char = fread($this->fp, 1);

}

return $data;

}

/**
* 返回地区信息
*
* @access private
* @return string
*/

function getarea() {

$byte = fread($this->fp, 1); // 标志字节

switch (ord($byte)) {

case 0: // 没有区域信息

$area = "";

break;

case 1:

case 2: // 标志字节为1或2,表示区域信息被重定向

fseek($this->fp, $this->getlong3());

$area = $this->getstring();

break;

default: // 否则,表示区域信息没有被重定向

$area = $this->getstring($byte);

break;

}

return $area;

}

/**
* 根据所给 IP 地址或域名返回所在地区信息
*
* @access public
* @param string $ip
* @return array
*/

function getlocation($ip) {

if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空

$location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址

$ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址

// 不合法的IP地址会被转化为255.255.255.255

// 对分搜索

$l = 0; // 搜索的下边界

$u = $this->totalip; // 搜索的上边界

$findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)

while ($l <= $u) { // 当上边界小于下边界时,查找失败

$i = floor(($l + $u) / 2); // 计算近似中间记录

fseek($this->fp, $this->firstip + $i * 7);

$beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址

// strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式

// 以便用于比较,后面相同。

if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时

$u = $i - 1; // 将搜索的上边界修改为中间记录减一

}

else {

fseek($this->fp, $this->getlong3());

$endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址

if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时

$l = $i + 1; // 将搜索的下边界修改为中间记录加一

}

else { // 用户的IP在中间记录的IP范围内时

$findip = $this->firstip + $i * 7;

break; // 则表示找到结果,退出循环

}

}

}



//获取查找到的IP地理位置信息

fseek($this->fp, $findip);

$location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址

$offset = $this->getlong3();

fseek($this->fp, $offset);

$location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址

$byte = fread($this->fp, 1); // 标志字节

switch (ord($byte)) {

case 1: // 标志字节为1,表示国家和区域信息都被同时重定向

$countryOffset = $this->getlong3(); // 重定向地址

fseek($this->fp, $countryOffset);

$byte = fread($this->fp, 1); // 标志字节

switch (ord($byte)) {

case 2: // 标志字节为2,表示国家信息又被重定向

fseek($this->fp, $this->getlong3());

$location['country'] = $this->getstring();

fseek($this->fp, $countryOffset + 4);

$location['area'] = $this->getarea();

break;

default: // 否则,表示国家信息没有被重定向

$location['country'] = $this->getstring($byte);

$location['area'] = $this->getarea();

break;

}

break;

case 2: // 标志字节为2,表示国家信息被重定向

fseek($this->fp, $this->getlong3());

$location['country'] = $this->getstring();

fseek($this->fp, $offset + 8);

$location['area'] = $this->getarea();

break;

default: // 否则,表示国家信息没有被重定向

$location['country'] = $this->getstring($byte);

$location['area'] = $this->getarea();

break;

}

if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息

$location['country'] = "未知";

}

if ($location['area'] == " CZ88.NET") {

$location['area'] = "";

}

return $location;

}

/**
* 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
*
* @param string $filename
* @return IpLocation
*/

function IpLocation($filename = "QQWry.Dat") {

if (($this->fp = @fopen($filename, 'rb')) !== false) {

$this->firstip = $this->getlong();

$this->lastip = $this->getlong();

$this->totalip = ($this->lastip - $this->firstip) / 7;

//注册析构函数,使其在程序执行结束时执行

register_shutdown_function(array(&$this, '_IpLocation'));

}

}

/**
* 析构函数,用于在页面执行结束后自动关闭打开的文件。
*
*/

function _IpLocation() {

fclose($this->fp);

}

}

?> 

很赞哦!

上一篇:电脑主板BIOS设置详解

下一篇:返回列表

文章评论

站点信息

  • 建站时间:2019-05-01
  • 网站程序:帝国cms
  • 主题模板《今夕何夕》
  • 文章统计:299条
  • 文章评论:490条
  • 统计数据
  • 微信公众号:扫描二维码,关注我们