做者: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}