php 常用验证类及正则
正则表达式在遇到新的时候将会不断更新
include "ValidateParameterConfig.php";
class Validation
{
private static function getRexp($rexp)
{
$_rexp = array (
'letter_number'=>'/^[0-9A-Za-z]+$/',//只有字母数字包括大小写
'account'=>'/^[0-9A-Za-z_]+$/',//只有字母数字下划线包括大小写
'ids'=>'/^[0-9]+(\,[0-9]+)*$/',//验证多个id以','分割的类型 例如'1,2,3,4,5'
'number'=>'/^[0-9]+$/',//只可以使数字
'personal_card'=>'/^[0-9A-Za-z]+$/',//身份证
'email'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/',//邮箱
'date'=>'/^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/'//日期,包含了闰年
);
if (isset($_rexp[$rexp])) {
return $_rexp[$rexp];
} else {
return $rexp_not_defind;
}
}
public static function validate($data, $config)
{
$_config = ValidateParameterConfig::getConfig($config);
$k = self::allowExist($data, $_config);
if ($k !== null) {
return $k;
}
foreach($_config as $k=>$c) {
if (isset($data[$k]))
{
if(isset($c['rexp']))
{
if (self::vRexp( $data[$k], $c['rexp']) === false) {
return $k;
}
}
if (isset($c['length']))
{
if (self::vLength($data[$k], $c['length']))
{
return $k;
}
}
if (isset($c['min_length']))
{
if (!self::vMinLength($data[$k], $c['min_length']))
{
return $k;
}
}
if (isset($c['max_length']))
{
if (!self::vMaxLength($data[$k], $c['max_length']))
{
return $k;
}
}
}
}
return null;
}
private static function allowExist($data, $config)
{
foreach ($config as $k=>$v)
{
if (!isset($v['allow_exist']) || $v['allow_exist'] == true) {
if (!isset($data[$k]))
{
return $k;
}
}
}
return null;
}
public static function vRexp($data, $rexp) {
$_rexp = self::getRexp($rexp);
if (preg_match($_rexp, $data) == false) {
return false;
}
return true;
}
public static function vLength($data, $l) {
if (strlen(trim($data)) == $l) {
return false;
}
return true;
}
public static function vMinLength($data, $l) {
if (strlen(trim($data))
return false;
}
return true;
}
public static function vMaxLength($data, $l) {
if (strlen(trim($data)) > $l) {
return false;
}
return true;
}
public static function vLetterNumber($data) {
if (preg_match(self::getRexp('letter_number'), $data) == false) {
return false;
}
return true;
}
public static function vLetterNumber_($data) {
if (preg_match(self::getRexp('letter_number_'), $data) == false) {
return false;
}
return true;
}
public static function vNumber($data) {
if (preg_match(self::getRexp('number'), $data) == false) {
return false;
}
return true;
}
public static function vEmail($data) {
if (preg_match(self::getRexp('email'), $data) == false) {
return false;
}
return true;
}
class ValidateParameterConfig
{
public static function getConfig ($key)
{
//'allow_exist'=>true或allow_exist不存在表示该参数必传
$_config = array(
'test'=>array(
'letter_number'=>array('rexp'=>'letter_number', 'allow_exist'=>true),
'number'=>array('rexp'=>'number', 'min_length'=>1'allow_exist'=>false),
'account'=>array('rexp'=>'account', 'max_length'=>20),
)
);
if (isset($_config[$key])) {
return $_config[$key];
} else {
return $config_not_defind;
}
}
}
使用例子:
从应用端发过来的参数为
$arr = array('letter_number'=>'abc123', 'account'=>'acb1234_');
//参数验证
$_msg = Validation::validate($arr, 'test');
if ($_msg !== null) {
return $_msg . ' is invalid parameter';
}

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。