分类广告


推荐文章

  • 没有找到任何内容!
您当前的位置:中国站长下载网络编程PHP专区 → 文章内容

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

  • 作者:不详    来源:网络转载    发布时间:2007-12-13 9:04:14
  • 字体大小:

如何创建我们的第一个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,使每个页面的底部都一样。 zhuye123.com

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

Www~Chinaz~com

填写我们的版权信息

中国

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

Www.zhuye123.com

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

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

Www_Chinaz_com

<?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"是用来显示输出的。 Www~Chinaz~com

三、如何实现分页

Www_Chinaz_com

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

Www.zhuye123.com

简单的分页代码:

Chinaz_com

以下为引用的内容:

<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'];

Chinaz

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;

Www.zhuye123.com

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>
<?

Chinaz_com

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

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

Chinaz

//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>

zhuye123.com

中国站长.站

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

中国

四、页面加载时间的代码 中国站长_站,为中文网站提供动力

以下为引用的内容:

<?php Www^

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

站.长站

?>

Www_Chinaz_com

[中国站长站]

五、显示从哪个地址转到你当前访问的站的代码

中.国站长站

以下为引用的内容:

<?php [中国站长站]

echo "You Came From:<br />";
echo $_SERVER['HTTP_REFERER'];
?>

中国.站长站

六、设置IP地址的转向:屏蔽IP

Www.zhuye123.com

以下为引用的内容:

<?php

中国站长_站,为中文网站提供动力

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

Chinaz~com

print "<meta http-equiv='refresh' content='0; url=http://www.sina.com'>";// url to redicted
endif;
?> Www_Chinaz_com

zhuye123.com

七、随即显示标题的代码

<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";
?>
zhuye123.com

创建一个页面( test.php )把下列代码

站.长站

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

Www_Chinaz_com

放在<html><head>之前,然后在test.php页里来调用上面的字符串

<p align="center"><? echo("$title");?></p>

中.国.

也可以这样来声明:

Www_Chinaz_com

以下为引用的内容:
<?
$surname1="Marco"; $lastname1="Charette";
?>
中.国站长站

调用: 中.国站长站

<? echo("$surname1 $lastname1");?>

中国站长_站,为中文网站提供动力

十、显示你的服务器的信息: 中.国.站长站

<? phpinfo(); ?>

中国站长_站,为中文网站提供动力

十一、实现页面的跳转: 中国站长_站,为中文网站提供动力

创建"jump.php"页面并且设置变量来隐藏连接的地址

Chinaz_com

以下为引用的内容:

<?
if ($id == "1"){$link = "http://webjx.com";}
if ($id == "2"){$link = "http://google.com";} Chinaz~com

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

Www~Chinaz~com

保存之后,在另外一个页中加入如下代码: 中国站长_站,为中文网站提供动力

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

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

zhuye123.com

以下为引用的内容:

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

header("Location: $link");
exit();
?> Chinaz~com

Chinaz~com

保存为jump.php然后再其他页内加入连接代码:

Www^

<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))) {

Www_Chinaz_com

/* 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 */ zhuye123.com

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");

中国站长.站

?> Chinaz~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 中国站.长站

所有的用户:

Www^

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 站.长站

站.长站

另一种读取数组的方法: zhuye123.com

以下为引用的内容:
<?
echo "3 of my customer are: $customer[0], " . " $customer[1] and " . " $customer[2]. " . "<br />";
?>
中国.站长站

将会显示:

中.国站长站

3 of my customer are: webjx, web and mutou. zhuye123.com

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

zhuye123.com

以下为引用的内容:

<?
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;

Www.zhuye123.com

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

十七、显示当前日期或最新更新的日期 Www~Chinaz~com

<?php echo date("F d Y"); ?> Www.zhuye123.com

显示更新日期:

中国站长_站,为中文网站提供动力

<?php echo "Last Modified: " . date ("m/d/y.", getlastmod());?> 中国站长_站,为中文网站提供动力

十八、重复一个字符多次的代码 中国站.长站

以下为引用的内容:

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

中国站长_站,为中文网站提供动力

Chinaz