国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

新手入門:初學動態網頁PHP的18個例子

2024-05-04 23:03:04
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  •   本站原創內容,轉載請注明出處網頁教學網

      如何創建我們的第一個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.survivalescaperooms.com/">www.survivalescaperooms.com.com</a>";?>

      如何創建一個有style的連接呢?

    <?php echo "<font style=/"color:blue;font-size:10px;font- family:verdana;/">free scripts by: <a href=/"http://www.survivalescaperooms.com.com/" target=/"_blank/" title=/"free scripts by: VeVb.com/">http://www.survivalescaperooms.com</font></a>";?>

      "echo"是用來顯示輸出的。

      三:如何實現分頁:

      如果好多內容在你的頁面中這時我們就考慮用分頁形式來實現顯示了。

      簡單的分頁代碼:

    <html>
    <head>
    <title>VeVb.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>VeVb.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="VeVb.com";
    $copyrights="©2005 VeVb.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://VeVb.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://VeVb.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://VeVb.com/page.php")) {
    echo "<script>alert('你不能訪問這個頁面');
    window.location='http://VeVb.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="VeVb.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://VeVb.com");

    ?>

      必須保存為php文件才可以。

      十五。制作用戶列表:

    <?
    $customer[0] = 'VeVb';
    $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 VeVb
    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: VeVb, 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);
    ?>

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 临湘市| 永善县| 辰溪县| 当涂县| 崇文区| 天柱县| 淅川县| 积石山| 象山县| 奉贤区| 闽侯县| 亳州市| 奉贤区| 金塔县| 新平| 卢龙县| 麻城市| 象山县| 措美县| 启东市| 栾川县| 绵竹市| 琼海市| 北川| 乾安县| 庆元县| 和林格尔县| 浦东新区| 和龙市| 博客| 社旗县| 奎屯市| 甘孜县| 宾阳县| 延庆县| 怀安县| 井冈山市| 巴塘县| 黔江区| 汽车| 娄底市|