[php]表单和验证

时间:2022-04-17 00:17:12
<?php
/*
表单的作用: 通过表单 发布和收集 信息. 对html表单进行编码 只是有效接受用户输入的必要操作的(一部分), 必须由[服务器端]组件来处理 一 标头函数(header())
标头(header)是 服务器 从http协议 传html资料到浏览器
在[标头]和[html文件]之间 需空一行分隔. header() 函数用来发送一个原始 HTTP 标头。
void header ( string string [, bool replace [, int http_response_code]] ) 标头 可以 有两种形式:
1. 重导向 指定的url
header('Location:http://www.baidu.com');
2. 设置 页面字符编码
header('Content-Type: text/html; charset=utf-8'); [注意]
除非启用了{输出缓冲}, 否则 这些命令必须放在 返回任何输出的语句之前执行.(就是放在文件的开头)
启用输出缓冲: ob_start(); 使用$_POST和$_GET 接受数据:
1. $_GET['username']发送的表单 method必须是get;
2. $_POST['username']发送的表单 method必须是post;
3. 用 isset() 验证 $_GET['username']超级全局变量 是否定义;
4. 使用 htmlspecialchars() 过滤 html特殊字符. 对{数据有效性}进行 验证:
1. trim() 去除数据的前后 空格;
2. strlen() 判断数据的长度;
3. is_nuimeric() 判断数据是否纯数字;
4. 用{正则表达式} 验证邮箱是否合法. */ /* if(!isset($_POST['send']) || $_POST['send']!='提交'){
header('Location:demo1.php');
exit;
}else{
echo "<h1>". $_POST['name']."</h1> <br><b>".$_POST['said']."</b>";
} */ /*
例如一个生成的 PDF 文件,可以通过发送 Content-Disposition 标头提供推荐的文件名来强制浏览器弹出一个保存文件对话框。 <?php // 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf'); // 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"'); // 这是 original.pdf 的源文件
readfile('original.pdf');
?> * */ /* 表单元素 描述
-----------------------------
text input 文本框
password input 密码框
hidden input 隐藏框
select 下拉列表
checkbox 复选框
radio 单选
textarea 区域框
file 上传
submit 提交按钮
reset 重置 * */ /*
if (strlen($username)<2 ||strlen($username)>10) {
echo "<script>alert('用户名不能小于两位或者大于10');history.back();</script>";
exit;
}
*/ /*
if (preg_match( '/([\w\.]{2,255})@([\w\-]{1,255}).([a-z]{2,4})/' , $_POST [ 'email' ])) {
echo ' 电子邮件合法 ' ;
} else {
echo ' 电子邮件不合法 ' ;
} * */ ?>