PHP學習18例

來源:互聯網
上載者:User
  一、PHP的基本結構:
  使用Include函數


Your page Subject and domain name
  上面內容為我們使用的每個頁面的標題,不要動。
  每個頁的頭部:

"" 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,使每個頁面的頭部都是一樣的。



  你的頁的所有內容


  儲存為footer.php,使每個頁面的底部都一樣。

  填寫我們的著作權資訊


  二:如何輸出文本或者把文本建立串連用PHP
  在PHP中如何顯示文本呢?使用下面的命令:

  如何建立一個串連呢?

  如何建立一個有style的串連呢?
";?>
  "echo"是用來顯示輸出的。
  三:如何?分頁:
  如果好多內容在你的頁面中這時我們就考慮用分頁形式來實現顯示了。
  簡單的分頁代碼:


webjx.com










home

page 1

page 2



function index()
{
echo "

Welcome to this tutorial
Here you can find funny
tricks







"; }
$choice=$_GET['p'];

switch($choice)
{
case "Page1":

echo "

Page1 text, img and so on here

";
break;

case "Page2":

echo "

Page2 text, img and so on here

";
break;

default:
index();
}

?>





  以上檔案必須儲存為index.php
  進階分頁的代碼:


webjx.com










home

page 1

page 2



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 '

Error 404! the page you request doesn t exist or as been temporarely unaccessible

';
break;
}

?>






  提供了示範的下載,請自己去試試!
  四:頁面載入時間的代碼:

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

?>
  五:顯示從哪個地址轉到你當前訪問的站的代碼:

echo "You Came From:
";
echo $_SERVER['HTTP_REFERER'];
?>
  六:設定IP地址的轉向:屏蔽IP

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

print "";// url to redicted
endif;
?>
  七:隨即顯示標題的代碼:
<?php include("title.php");?>
  要事先做一個title.php檔案啊
  八;如何用PHP來建立一個HTML 的table
echo"\n";
echo"\n";
echo"allo\n";
echo"\n";
echo"
bgcolor=\"#ffffff\">\n";
echo"

\n";
echo"



\n"; echo" \n"; echo" \n"; echo" \n"; echo"
\n";
echo"

\n";
echo"
\n";
echo"Text Goes Here\n";
echo"

\n";
echo"
\n";
echo"";
echo"\n";
echo"";
?>
  九、聲明字串變數:
  建立一個頁面 ( config.php ) 粘貼下面代碼:
$name="Name";
$salutation="Hey!";
$title="webjx.com";
$copyrights="©2005 webjx.com";
$link_1="Home";
?>
  建立一個頁面( test.php )把下列代碼

  放在之前,然後在test.php頁裡來調用上面的字串


  也可以這樣來聲明:
$surname1="Marco"; $lastname1="Charette";
?>
  調用:

  十、顯示你的伺服器的資訊:

  十一:實現頁面的跳轉:
  建立"jump.php"頁面並且設定變數來隱藏串連的地址
if ($id == "1"){$link = "http://webjx.com";}
if ($id == "2"){$link = "http://google.com";}

header("Location: $link"); // 實現了跳轉
exit();
?>
  儲存之後,在另外一個頁中加入如下代碼:
Visit This Link
  跳轉並且還把訪客儲存在你的頁面內:
if ($id == "1"){$link = "http://webjx.com";}
if ($id == "2"){$link = "http://google.com";}

echo "\n";
echo "\n";
echo "\n";
echo "\n";

header("Location: $link");
exit();
?>
  儲存為jump.php然後再其他頁內加入串連代碼:
Visit This Link
  十二、保護頁面:
if (!strchr($Referer, "http://webjx.com/page.php")) {
echo "";
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 '

訪問的頁面被限制.

請檢查你的使用者名稱或密碼是否正確,正常登陸本站才能查看 本站的內容';
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 '

訪問的頁面被限制.

請檢查你的使用者名稱或密碼是否正確,正常登陸本站才能查看 本站的內容';
exit;
} else if (($PHP_AUTH_USER == "$admin_user_name") || ($PHP_AUTH_PW == "$admin_password")) { echo "$login
";
}
}

?>
  儲存為log.php,把下列代碼加到head頁內,那麼將會保護所有的頁面。

  十四、製作一個自動轉向:

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.");
?>
  十七.顯示當前日期或最新更新的日期:

顯示更新日期:


  十八.重複一個字元多次的代碼:


echo str_repeat("-", 30);
?>
本文轉自建站學 http://www.jzxue.com ,轉載請註明出處。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.