2021年7月25日 星期日

(示例) HTML中嵌入PHP的簡單方法

我們以一個提交訂單和顯示訂單資訊的例子為學習PHP的開始。這個例子包含兩個檔案。一個提交訂單的html檔案:orderform.html,一個顯示訂單資訊的php檔案:processorder.php。我將這兩個檔案放在test_1資料夾下,將test_1資料夾放在htdocs目錄下。
檔案的組織形式如下圖所示,使用xampps安裝的整合環境。

提交訂單的html檔案orderform.html如下所示:
<form action="processorder.php" method="post">
<table>
<tr bgcolor="#cccccc">
<td width="150">Item</td>
<td width="15">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit Order" /></td>
</tr>
</table>
</form>

顯示訂單資訊的php檔案processorder.php如下所示:

<?php
// create short variable names, also can use '$_REQUEST['name']'
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
?>

<!DOCTYPE html>
<html>
<head>
<title>Bob 's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob 's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date('H:i, jS F Y')."</p>";
echo "<p>Your order is as follows: </p>";
echo "$tireqty tires<br />";
echo $oilqty.' bottles of oil<br />';
echo $sparkqty." spark plugs<br />"
?>

---------------------------------------------------<br />
<?php
$testHeredoc = <<< EOF
line 1 
line 2 
line 3 
EOF;
echo "$testHeredoc"."<br />";
?>

---------------------------------------------------<br />
<?php
echo "About Comment:";
//Here is a comment.
#Here is a comment too.
/*
Here is multi line comment.
Here is multi line comment.
*/

?>

</body>
</html>

在瀏覽器中叫用 orderform.html

填入數字,然後點選"Submit Order"按鈕提交內容。則頁面將顯示processorder.php經過PHP解析器解析之後生成的html頁面

在這個例子中,我們可以學習到以下幾點內容:

2021年7月23日 星期五

(小感言) 人類中的垃圾 - 黑客

因為喜歡電腦. 剛好退休也可以用來打發些時間, 但是. 大部份時間花用在 log的觀察. 以及防火牆的修正上. 其實也蠻浪費生命的...
-
自家網站的防火牆雖日臻完善. 但的的確確也花了我不少時間...; 怪就怪在現在這個世界壞人. 居心不良的人太多. 總有數不清的不肖之徒不斷企圖入侵別人家電腦, 所以浪費了不少人類生命花在防火牆的攻防技術上...; 我雖不算得上是駭客之流. 但是為了讓自己的電腦運作順暢些. 逼使自己不得不花上許多時間在這上面...; 有點覺得浪費生命耗在這上面.., 微微的小收穫是: 我能夠用一兩千元的二手主機. 到現在還能順利運作老古董 RH9主機. 以及 XP等級的作業系統. 而且是虛擬多機使用的!!~
-
 

2021年7月3日 星期六

06_發送電子郵件(後試)

mail()函數用於在PHP中發送電子郵件。 mail()函數可送短信. HTML消息. 和附件。
PHP mail()函數
 
語法
 
    $to:指定郵件的接收者或接收者。 收信者格式:
        user@example.com
        user@example.com, anotheruser@example.com
        User user@example.com
        User user@example.com, User2 anotheruser@example.com
    $subject: 郵件主題。
    $message: 發送的郵件內容
    $additional_headers(可選):指定附加頭,如From,CC,BCC等。附加頭也用CRLF(\r\n)分隔。
    注意:消息的每一行應使用CRLF(\r\n)分隔,並且行不應大於70個字符。
 
PHP郵件範例
 
文件:mailer.php
 
<?php 
   ini_set("sendmail_from", "rh3@dyhms.no-ip.org"); 
   $to = "joke111.y@gmail.com"; //receiver address 
   $subject = "郵件主旨"; 
   $message = "從 localhost 轉 NAT server 發信"; 
   $header = "發信者: rh3@dyhms.no-ip.org\r\n"; 
 
   $result = mail ($to,$subject,$message,$header); 
 
   if( $result == true ){ 
      echo "Message sent successfully..."; 
   }else{ 
      echo "Sorry, unable to send mail..."; 
   } 
?>
 
如果在運行服務器上運行此代碼,它將向指定的接收方發送電子郵件。
PHP郵件:發送HTML消息
 
要發送HTML消息. 則需在消息標題中提及Content-type text/html。
 
<?php 
   $to = "joke111.y@gmail.com";//change receiver address 
   $subject = "This is subject"; 
   $message = "<h1>This is HTML heading</h1>"; 
 
   $header = "From: rh3@dyhms.no-ip.org\r\n"; 
   $header .= "MIME-Version: 1.0 \r\n"; 
   $header .= "Content-type: text/html;charset=UTF-8 \r\n"; 
 
   $result = mail ($to,$subject,$message,$header); 
 
   if( $result == true ){ 
      echo "Message sent successfully..."; 
   }else{ 
      echo "Sorry, unable to send mail..."; 
   } 
?>
 
PHP郵件:使用附件發送郵件
 
要發送附件, 則需提及許多標題信息,參示例:
 
<?php 
  $to = "joke111.y@gmail.com"; 
  $subject = "This is subject"; 
  $message = "This is a text message."; 
  # Open a file 
  $file = fopen("/tmp/test.txt", "r" );//change your file location 
  if( $file == false ) 
  { 
     echo "Error in opening file"; 
     exit(); 
  } 
  # Read the file into a variable 
  $size = filesize("/tmp/test.txt"); 
  $content = fread( $file, $size); 
 
  # encode the data for safe transit 
  # and insert \r\n after every 76 chars. 
  $encoded_content = chunk_split( base64_encode($content)); 
 
  # Get a random 32 bit number using time() as seed. 
  $num = md5( time() ); 
 
  # Define the main headers. 
  $header = "From:xyz@example.com\r\n"; 
  $header .= "Content-Type: multipart/mixed; "; 
  $header .= "boundary=$num\r\n"; 
  $header .= "--$num\r\n"; 
 
  # Define the message section 
  $header .= "Content-Type: text/plain\r\n"; 
  $header .= "Content-Transfer-Encoding:8bit\r\n\n"; 
  $header .= "$message\r\n"; 
  $header .= "--$num\r\n"; 
 
  # Define the attachment section 
  $header .= "Content-Type:  multipart/mixed; "; 
  $header .= "name=\"test.txt\"\r\n"; 
  $header .= "Content-Transfer-Encoding:base64\r\n"; 
  $header .= "Content-Disposition:attachment; "; 
  $header .= "filename=\"test.txt\"\r\n\n"; 
  $header .= "$encoded_content\r\n"; 
  $header .= "--$num--"; 
 
  # Send email now 
  $result = mail ( $to, $subject, "", $header ); 
  if( $result == true ){ 
      echo "Message sent successfully..."; 
   }else{ 
      echo "Sorry, unable to send mail..."; 
   } 
?>

2021年6月30日 星期三

(PHP) phpinfo()

幹! php 還真難學...
明明記得當初 RH9架站時. 有依鳥哥教學用過 phpinfo() 列出版本資訊, 為何自己怎試就不行(沒顯示任何文字)!?...
原來...
 
<?php  //沒有寫 'php'. 不行!
phpinfo();
?>
 
<? php  // '?' 和 'php' 間多空一格也不行!!
phpinfo();
?>

05_include和require

PHP允許您包括文件,以便頁面內容可以重複使用。
兩種方法在PHP中包括文件:
    include
    require
 
包括文件的優點
代碼重用性:可以在許多PHP腳本中重複使用HTML代碼或PHP腳本。
 
文件: menu1.html
<head>
    <meta charset="utf-8" />
    <title>html菜單示例</title>
</head>
<a href="https://JimmyKenny.blogspot.com">蕃薯家的生活</a> |  
<a href="https://50air.blogspot.com/">50新空域</a> |  
<a href="https://sites.google.com/view/yellowcat">黃子貓工作室 (雲端主頁)</a> |   
<a href="https://dyhms.blogspot.com/">黃子貓工作室 (部落格)</a>
 
文件: include1.php
<?php include("menu1.html"); ?> 
<h1>這是主菜單(include)</h1>
include VS require 差異在於: 文件丟失時的處理方式
include語句允許腳本繼續
require語句暫停腳本產生致命的E_COMPILE_ERROR級別錯誤。

2021年6月29日 星期二

04_表單處理

要獲取表單數據,需要使用: $_GET和$_POST。
表單請求可以是get或post。
要從get請求中檢索數據,需要使用$_GET,而$_POST用於檢索post請求中的數據。
 
GET表單
GET是表單的默認請求。 以get請求傳遞的數據在URL瀏覽器上是可見的,因此它不太安全。通過 get 請求發送的數據量是有限的,所以不適合使用Get請求方法發送大量數據。
 
文件: form1.html
<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
        <title>Get表單示例</title>
    </head>
<body>
 
<form action="welcome.php" method="get"> 
用戶名: <input type="text" name="name"/> 
<input type="submit" value="提交"/> 
</form>
 
文件: welcome.php
<head>
    <meta charset="utf-8" />
    <title>歡迎</title>
</head>
<?php 
$name=$_GET["name"];     //接收$name的內容 
echo "歡迎您, $name"; 
?>
Post表單
Post廣泛用於提交大量數據的表單,如:文件上傳,圖像上傳,登錄表單,註冊表單等。
以post請求傳遞的數據在URL瀏覽器上不可見,因此它是安全的。可以用來發送請求發送大量的數據。
 
從PHP中接收來自post請求的數據的例子
文件: form2.html
<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
        <title>POST表單示例</title>
    </head>
<body>
<form action="login.php" method="post">  
用戶名:<input type="text" name="name"/>
密碼:<input type="password" name="password"/>  
<input type="submit" value="登錄"/>
</form>
 
文件: login.php
<head>
    <meta charset="utf-8" />
    <title>登錄</title>
</head>
<?php 
$name=$_POST["name"];
$password=$_POST["password"];
echo "歡迎: $name, 您密碼是: $password"; 
?>
 
 
 
 

(不想探究) Timeout waiting for PADO packets

不想探究!? 才怪!
你搞得我連網不順. 不探 '咎'. 才怪!!
-
斷斷續續. 總會發生網路斷線. 連不上網路. 或開不了某些網頁的問題...
不斷地嚐試. 追蹤. 除錯, 當然情況漸入佳境!!...
 
今天的進展心得是: 可能是中華電信的委外廠商的網管問題!!?...
新追蹤到的問題情況:
- 網頁開不了: 發生 [無法與 reCAPTCHA連線] 問題
- 斷線重撥訊息: Timeout waiting for PADO packets
 
處置方式:
- 以 cmd 的 tracert 指令查詢卡關的路由: 發現 168.95.98.254 這傢伙很卡...
- 以 iptables -A INPUT -i $EXTIF -s 168.95.98.254 -j DROP 在 NAT Sever 上過濾掉. 一切好像就變順了...
 
我的道理:
- 有可能電信的委外廠商以網管手段. 轉址方便所造成
- 因此. 我採 [我只發訊息叫你處理. 不接受你網管訊息] 的策略應對之
 
有待後續持續觀察策略的成效....
 

03_數據類型

數據類型
分爲3大類型
    標量類型
    複合類型
    特殊類型
 
4種標量數據類型
    布爾
    整數
    浮動
    字符串
 
2種複合數據類型
    數組
    對象
 
2種特殊的數據類型
    資源
    NULL值

(Centos-7s) 更新: 網卡名稱改回 eth0 的方法

將 CentOS 7 網卡名稱修改, 用回 Eth0 的方法:   1) # vi /etc/sysconfig/grub 內容大概是這樣:   GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release...