新手入门:初学动态网页PHP的18个例子

如何创建我们的第一个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设置放这

上述内容保存为header.php,使每个页面的头部都是一样的。

以下为引用的内容:

<?PHP include("header.php")?>

</head>
<body>

你的页的所有内容

以下为引用的内容:
</body>
</html>

保存为footer.php,使每个页面的底部都一样。

<? include("footer.php");?>

填写我们的版权信息

以下为引用的内容:
</body>
</html>

二、如何输出文本或者把文本建立连接用PHP

在PHP中如何显示文本呢?使用下面的命令:

<?php echo "Hello in php";?>

如何创建一个连接呢?

<?php echo "<a href=\"http://www.webjx.com\">www.webjx.com.com</a>";?>

如何创建一个有style的连接呢?

<?php echo "<font style=\"color:blue;font-size:10px;font- family:verdana;\">Free Scripts By: <a href=\"http://www.webjx.com.com\" target=\"_blank\" title=\"Free Scripts By: webjx.com\">http://www.webjx.com</font></a>";?>

"echo"是用来显示输出的。

三、如何实现分页

如果好多内容在你的页面中这时我们就考虑用分页形式来实现显示了。

简单的分页代码:

以下为引用的内容:

<html>
<head>
<title>webjx.com</title>
</head>
<body text="#000000">
<center>
<table width="500" border="3">
<tr>
<td width="140" height="400" valign="top">
<p align="center">
<a href='index.php'>home</a><br />
<a href='index.php?p=Page1'>page 1</a><br />
<a href='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>

以上文件必须保存为index.php

高级分页的代码:

<html>
<head>
<title>webjx.com</title>
</head>
<body text="#000000">
<center>
<table width="500" border="3">
<tr>
<td width="140" height="400" valign="top">
<p align="center">
<a href='index.php'>home</a><br />
<a href='index.php?action=contact_us'>page 1</a><br />
<a href='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>

提供了演示的下载,请自己去试试!

四、页面加载时间的代码

以下为引用的内容:

<?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="webjx.com";
$copyrights="©2005 webjx.com";
$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://webjx.com";}
if ($id == "2"){$link = "http://google.com";}

header("Location: $link"); // 实现了跳转
exit();
?>

保存之后,在另外一个页中加入如下代码:

<a href="jump.php?id=1">Visit This Link</a>

跳转并且还把访客保存在你的页面内:

以下为引用的内容:

<?
if ($id == "1"){$link = "http://webjx.com";}
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="jump.php?id=1">Visit This Link</a>

十二、保护页面

以下为引用的内容:
<? $Referer = getenv("HTTP_REFERER");
if (!strchr($Referer, "http://webjx.com/page.php")) {
echo "<script>alert('你不能访问这个页面');
window.location='http://webjx.com';</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="webjx.com"; // 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://webjx.com");

?>

必须保存为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);
?>

本书展示一个完整网站的设计和实现过程,详细地介绍动态网页设计和制作的技术和相关理论,全书共分为8章,主要内容包括:动态网站设计概述、动态网站编程环境、网站主页设计与PHP基础、网站计数器设计与PHP文件访问、会员注册和管理设计与数据获取、网上社区设计与PHP数据库访问、网上购书与PHP面向对象技术、网站优化与PHP的高级功能等,本书内容系统全面,案例典型实用,讲述直观详尽,非常适合动态网页设计与制作的初学者使用,还可作为高等院校教材和“实用型”人才培训教材。 目录 第1章 动态网站设计概述 1.1 动态网站的特点 1.2 动态网站的运行机制 1.2.1 域名 1.2.2 网页 1.2.3 浏览器 1.2.4 服务器 1.3 动态网站的规划 1.3.1 确定网站的类型 1.3.2 确定网站的主题 1.3.3 确定网站的整体风格 1.3.4 确定网站的内容 1.3.5 规划界面 1.3.6 规划站点的目录结构和链接结构 1.3.7 编写网站策划书 1.4 动态网站开发前的准备 1.4.1 申请域名 1.4.2 接入Internet 1.4.3 选择软硬件平台 1.4.4 选择网站建设服务商 第2章 动态网站编程环境 2.1 动态网页编程环境的构成要素 2.1.1 操作系统 2.1.2 服务器端程序 2.1.3 程序语言 2.1.4 数据库 2.1.5 基于PHP常见动态网站开发环境 2.2 安装和配置Apache 2.2.1 安装Apache前的准备 2.2.2 安装Apache 2.2 13测试Apache 2.2.4 配置Apache 2.2.5 在Windows上管理Apache 2.3 安装和配置PHP 2.3.1 安装PHP前的准备 2.3.2 安装PHP 2.3.3 配置PHP 2.3.4 测试PHP 2.4 安装和配置MySQL 2.4.1 安装MySQL,前的准备 2.4.2 安装MySQL 2.4.3 配置MySQL 2.4.4 在Windows上手动启动和停止MySQL 2.4.5 安装phpMyAdmin 2.5 AppServ组件安装 2.5.1 安装AppSery前的准备 2.5.2 安装。AppSery 2.5.3 测试AppSery 2.5.4 配置Apache 2.5.5 修改MySQL服务器的密码 2.6 PHP的集成开发环境 2.6.1 Dream weaver开发工具 2.6.2 Eclipse开发工具 2.6.3 其他开发工具 第3章 网站主页设计与PHP基础 3.1 网站主页的设计 3.1.1 主页中的页面元素, 3.1.2 规划主页的结构 3.1.3 布局页面版式 3.1.4 用Fireworks创建网页模型 3.2 网站主页的实现 3.2.1 在Dream weaver中创建网站 3.2.2 布局页面元素的样式 3.2.3 实现主页的结构 3.2.4 编辑主页的栏目内容 3.3 网站主页中的链接策略 3.4 PHP语言基础 3.4.1 PHP的程序结构 3.4.2 PHP的句法结构 3.4.3 PHP的数据类型 3.4.4 PHP的控制语句 第4章 网站计数器设计与PHP文件访问 4.1 网站计数器的设计 4.1.1 系统架构 4.1.2 系统设计 4.2 网站计数器的实现 4.2.1 文本输出的网页计数器 4.2.2 图片输出的网页计数器 4.2.3 调试代码 4.3 PHP的函数 4.3.1 PHP函数概述 4.3.2 白定义函数 4.3.3 PHP内置函数 4.4 PHP访问文件 4.4.1 PHP支持的文件系统 4.4..2 PHP访问文件的方法 4.4.3.PHP访问目录 4.5 文件管理器 第5章 会员注册和管理设计与数据获取 5.1 会员注册和管理概述 5.2 会员注册和管理设计 5.2.1 系统架构 5.2.2 系统设计 5.3 注册模块的实现 5.3.1 页面样式表 5.3.2 网页的头部、尾部和数据库连接文件 5.3.3 注册的主页和处理程序 5.3.4 用户申请购书卡页和处理程序 5.3.5 注册用户资料页与处理程序 5.3.6 调试代码 5.4 会员管理模块的实现 5.4.1 页面样式表 5.4.2 网页的头部、尾部和连接数据库 5.4.3 会员管理的主页 5.4.4 用户分级登录页和处理程序 5.4.5 用户购书卡专区页和处理程序 5.4.6 会员修改资料页与处理程序 5.4.7 会员找回密码页与处理程序 5.4..8 购书卡管理页与处理程序 5.4.9 调试代码 5.5 表单数据处理 5.5.1 PHP与表单 5.5.2 表单数据的采集 5.5.3 表单数据处理描述 5.6 cookie与会话管理 5.6.1 数据传递概述 5.6.2 cookie 5.6.3 会话管理 第6章 网上社区设计与PHP数据库访问 6.1 网上社区概述 6.2 留言板的设计 6.2.1 留言板的架构 6.2.2 留言板的设计描述 6.3 留言板的实现 6.3.1 网页的头部、尾部和系统配置文件 6.3.2 留言板的主页——欢迎页 6.3.3 写留言页和回复留言 6.3.4 浏览留言主题页 6.3.5 浏览主题留言页 6.3.6 管理员登录页 6.3.7 屏蔽和删除留言页 6.3.8 调试代码 6.4 聊天室的设计 6.4.1 聊天室的架构 6.4.2 聊天室的设计描述 6.5 聊天室的实现 6.5.1 网页的头部、尾部和系统配置文件 6.5.2 聊天室的主页——登录页 6.5.3 聊天室页 6.5.4 浏览聊天信息页 6.5.5 离开页 6.5.6 注销页 6.5.7 调试代码 6.6 PHP访问数据库 6.6.1 PHP访问数据库的机制 6.6.2 连接数据库服务器 6.6.3 PHP数据库管理函数 6.6.4 PHP查询数据函数 6.6.5 其他数据管理工具 第7章 网上购书与PHP面向对象技术 7.1 网上购书系统分析 7.1.1 系统工作流程分析 7.1.2 系统功能模块分析 7.2 网上购书系统设计 7.2.1 模块的逻辑结构设计 7.2.2 数据库设计 7.2.3 数据操作类设计 7.2.4 界面设计 7.3 网上购书系统实现 7.3.1 数据操作类 7.3.2 图书查询 ’7.3.3 图书显示处理 7.3.4 购物车 7.3.5 订单处理 7.3.6 调试代码 7.4 PHP面向对象技术 7.4.1 基本概念 7.4.2 定义类 7.4.3 创建对象 7.4.4 使用对象的属性和方法 7.4..5 对象的操作 第8章 网站优化与PHP的高级功能 8.1 用户注册安全管理 8.1.1 用户注册安全管理的策略 8.1.2 优化用户注册页面 8.1.3 PHP的GD函数库 8.1.4 图片缩略图 8.2 图像文件上传处理 8.2.1 网站图像存储策略 8.2.2 上传图书封面图像 8.2.3 PHP文件上传处理 8.3 电子邮件处理 8.3.1 PHP电子邮件处理概述 8.3.2 向客户发送邮件 8.3.3 PHP发送电子邮件的方式 8.4 正则表达式 8.4.1 正则表达式简介 8.4.2 常用表单项验证 8.4.3 正则表达式的法则 参考文献
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值