做者:20135216 平臺:windows10 軟件:XAMPP,DreamWeaver 說明:該筆記是對創建網站(除PHP語法外)相關內容的基礎學習;主要是form表單、CSS以及HTTP身份驗證
表單處理是一個多進程。首先,建立一張表單,以供用戶輸入詳細的請求信息。接着,輸入的數據被髮送到網頁服務器,在服務器裏這些數據獲得編譯和錯誤檢測。若是PHP代碼標識出一個或多個須要從新輸入的字段,則帶有相關錯誤信息的表單會被從新顯示。當精確的輸入信息知足代碼須要時,代碼會採起一些調用數據庫的行動。php
例子css
<?php echo <<<_END <html> <head> <title>Form Test</title> </head> <body> <form method = "post" action = "fromtest.php"> what is your name? <input type = "text" name = "name"> <input type = "submit"> </form> </body> </html> _END; ?>
示例html
<?php if(isset($_POST['name']) $name = $_POST['name']; else $name = "(not entered)"; echo <<<_END <html> <head> <title>Form Test</title> </head> <body> your name is:$name<br> <form method = "post" action = "fromtest.php"> what is your name? <input type = "text" name = "name"> <input type = "submit"> </form> </body> </html> _END; ?>
isset函數用來檢測$_POST['name']是否已經被賦值。若已經被賦值,則輸出一條單句進行顯示web
代碼算法
<form method = "post" action = "calc.php"><pre> loan amount <input type = "text" name = "monthly"> monthly repayment <input type = "text" name ="years" value = "25"> number of years <input type = "text" name = "years" value = "25"> Interest Rate <input type = "text" name = "rate" value = "6"> <input type = "submit"> </pre></form>
說明:value屬性字段顯示默認值,用戶能夠按意願對 其進行修改數據庫
文本框windows
<input type = "text" name = "name" size = "size"(制定文本框寬度) maxlength = "[容許輸入字符的最大值]" value = "[默認值]">
文本域數組
<textarea name = "[名稱]" cols = "[長度]" rows = "[長度]" wrap = "type"> This is default text. </textarea>
複選框(默認複選框是方形的)瀏覽器
<input type = "checkbox" name = "name" value = "value" checked = "checked">
例子(這種狀況下,只有最後一個被選中的複選框纔會被提交;以前被選中的值都會被忽略)安全
Vanilla <input type = "checkbox" name = "ice" value = "Vanilla" > Chocolate <input type = "checkbox" name = "ice" value = "Chocolate" > Strawberry <input type = "checkbox" name = "ice" value = "Strawberry" >
例子(容許多個選項被選中並提交,這裏提交的是一個數組)
Vanilla <input type = "checkbox" name = "ice[]" value = "Vanilla" > Chocolate <input type = "checkbox" name = "ice[]" value = "Chocolate" > Strawberry <input type = "checkbox" name = "ice[]" value = "Strawberry" >
例子
Vanilla <input type = "radio" name = "ice" value = "1" > Chocolate <input type = "radio" name = "ice" value = "2" checked = "checked" >(默認值) Strawberry <input type = "radio" name = "ice" value = "3" >
有時將表單字段隱藏起來,便於對錶單的輸入狀態進行跟蹤。好比
echo '<input type = "hidden" name = "submitted" value = "yes">'
好比第一次PHP程序接收輸入的時候,這行代碼沒有被執行,因此沒有叫作submitted的字段出現。PHP程序從新建立表單,增長輸入字段。而後,當瀏覽者從新提交表單時,PHP程序接受進行並將submitted字段設置爲「yes"
例子
<select name = "veg" size = 4(顯示行數) multiple = "multiple(多選;無此屬性則爲單選)"> <option value = "Peas">Peas</option> <option value = "Beans">Beans</option> <option value = "Carrots">Carrots</option> <option value = "Cabbage">Cabbage</option> </select>
例子
<label>8am-noon<input type = "radio" name = "time" value = "1"></label>
代碼
<?php $f = $c = ''; if(isset($_POST['f'])) $f = sanitizeString($_POST['f']); if(isset($_POST['c'])) $c = sanitizeString($_POST['c']); if($f != '') { $c= intval((5/9)*($f - 32)); $out = "$f'f equals $c 'c"; } else if($c != '') { $f = intval((9/5)*$c+32);//intval函數將結果轉換爲整型值 $out = "$c 'c equals $f 'f"; } else $out = ""; echo <<<_END <html> <head> <title>Temperature Converter</title> </head> <body> <pre> Enter either Fahrenheit or Celsius and click on Convert <b>$out</b> <form method = "post" action = "exercise2.php"> Fahrenheit <input type = "text" name = "f" size = "7"> Celsius <input type = "text" name = "c" size = "7"> <input type = "submit" value = "Convert"> </form> </pre> </body> </html> _END; function sanitizeString($var) { $var = stripslashes($var);//返回已經剝離反斜槓的字符串 $var = strip_tags($var);//剝去字符串中的HTML、PHP、XML標籤 $var = htmlentities($var);//將字符轉換爲HTML實體,其中,沒法被識別的字符集將被忽略 return $var; } ?>
代碼
<?php if(isset($_SERVER['PHP_AUTH_USER'])&& isset($_SERVER['PHP_AUTH_PW'])) { echo "Welcome user:" . $_SERVER['PHP_AUTH_USER'] . "Password:" . $_SERVER['PHP_AUTH_PW']; } else { header('WWW-Authenticate:Basic realm = "Restricted Section"');//basic realm就是被保護部分的名稱 header('HTTP/1.0 401 Unauthorized'); die("Please enter your username and password"); } ?>
代碼
<?php $username = 'admin'; $password = 'letmein'; if(isset($_SERVER['PHP_AUTH_USER'])&& isset($_SERVER['PHP_AUTH_PW'])) { if($_SERVER['PHP_AUTH_USER'] == $username && $_SERVER['PHP_AUTH_PW'] == $password ) echo "you are now logged in"; else die("Invalid username / password combination");//給黑客提供的信息越少越好 } else { header('WWW-Authenticate:Basic realm = "Restricted Section"'); header('HTTP/1.0 401 Unauthorized'); die("Please enter your username and password"); } ?>
舉例
$token = hash('ripemd128','mypassword');
補充:ripemd128是一種能夠替換md5的開源算法
直接在HTML的頭部標籤之間添加
<head> <title>Hello World</title> <style> h1{color:red;font-size:3em;font-family:Arial; } </style>
導入樣式表:將樣式運用到整個網站的話,更好的方法是將樣式放在獨立文件中,使用時進行導入
<style> @import url("style.css"); </style>
從HTML內部導入樣式表(不能用於將一根樣式表導入到另外一個樣式表,不能放在一對style標記內)
使用ID
<div id = 'welcome'>Hello there</div> (對應的CSS語句以下) #welcome {font-style:italic;color:blue;} (#的使用指明只有名爲welcome的ID纔會用這條語句來設置樣式)
至關於使用ID的進階版,能夠爲多個元素設置同一種樣式
<div class = 'welcome'>Hello there</div> (對應的CSS語句以下) .welcome {font-style:italic;color:blue;}
屬性選擇器:避免使用ID和類引用屬性
[type = "submit"]{width:100px;}//將屬性爲type = "submit"的全部元素設置爲100像素寬 form input[type = "submit"] {width:100px;}//爲擁有這一屬性的表單input元素設置爲100像素寬
好比:
p{color:#ff0000 !important;}//以前全部等價的設定都會被覆蓋,以後全部等價的處理規則也會被忽略
絕對定位
#object{position:absolute;top:100px;left:200px}
相對定位(相對於對象在原始文檔中的位置)
#object{position:relative;top:100px;left:200px}
固定定位(當文檔滾動時,對象仍然保持在它原來的位置上)
#object{position:fixed;top:100px;left:200px}