Useful functions to provide secure PHP application

本文介绍PHP中用于防止SQL注入、XSS攻击等安全功能的几个实用函数,包括mysql_real_escape_string、addslashes、htmlentities、htmlspecialchars、strip_tags、md5、sha1和intval。同时提醒使用md5和sha1时应注意安全性问题,如盐的使用。

security is a very important aspect of programming. There are many functions or modules in any kind of real programming language providing security functionalities  In modern websites, we may often get inputs form users all around the world.There is a famous saying which says that never trust user input. So in web programming languages, we will often see functions which will guarantee the security of the data input from users. Today we will cover some of these functions in the most famous open source language  - PHP.

In PHP, there are few useful functions which is very handy for preventing your website from various attacks like SQL Injection Attack , XSS attack etc. Let’s check few useful functions available in PHP to tighten the security in your project. But note that this is not a complete list, it just list of functions which I found useful for using in your project.

1) mysql_real_escape_string() - This function is very useful for preventing from SQL Injection Attack in PHP . This function adds backslashes to the special characters like quote , double quote , backslashes to make sure that the user supplied input are sanitized before using it to query. But, make sure that you are connected to the database to use this function.

Currently mysql_real_escape_string() is not recommended to be used anymore, all new applications should use libraries like PDO to perform database operations, we can use prepared statement to refrain away from the SQL injections.

1)mysql_real_escape_string() 这个函数对于预防PHP中的SQL注入攻击很有帮助。这个函数给特殊字符添加反斜杠,比如引号、双引号,反斜杠可以确保用户提交到数据在被用于查询之前是干净的。但是,确保你使用这个函数连接上数据库。

现在不在提倡使用mysql_real_escape_string(),所有新成熟用户使用封装好的库去执行数据库操作,比如PDO,我们可以使用预制好的申明来避免SQL注入。

2) addslashes() – This function works similar as mysql_real_escape_string(). But make sure that you don’t use this function when “magic_quotes_gpc” is “on” in php.ini. When “magic_quotes_gpc” is on in php.ini then single quote(‘) and double quotes (“) are escaped with trailing backslashes in GET, POST and COOKIE variables. You can check it using the function “get_magic_quotes_gpc()” function available in PHP.

2)addslashes()-这个函数功能和mysql_real_escape_string()类似。但是确保在php.ini中已经启用magic_quotes_gpc的时候你没有使用这个函数。当在php.ini中开始magic_quotes_gpc时,GET、POST和cookie中的变量里面的单引号和双引号都会使用反斜杠转译。你可以通过使用get_magic_quotes_gpc()函数的值来检查该功能。

3) htmlentities() – This function is very useful for to sanitize the user input. This function converts the special characters to their html entities. Such as, when the user enters the characters like “<” then it will be converted into it’s HTML entities < so that preventing from XSS and SQL injection attack.

3)htmlentities()-这个函数时用来过滤用户输入值的。该函数将特殊字符转译成相应的html实体。比如,当用户输入字符“<”时,该函数会转译成对应的HTML实体<,从而避免的XSS和SQL注入攻击。

4) htmlspecialchars() -- Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with these conversions made. For example, '&' (ampersand) becomes '&'

4)htmlspecialchars() -- 特定的字符在HTML中有特殊的意义,为了维护字符原意应该将它们解释成HTML实体。这个函数在处理后返回一个string类型数据。比如'&' (ampersand) 变成'&amp;'

5) strip_tags() – This function removes all the HTML, JavaScript and PHP tag from the string. But you can also allow particular tags to be entered by user using the second parameter of this function.

5)strip_tags() - 这个函数移除字符串中所有的HTML、javascript和PHP标签。但是你可以通过设置该函数的第二个参数来允许特定标签输入。

6) md5() – Some developers store plain password in the database which is not good for security point of view. This function generates md5 hash of 32 characters of the supplied string. The hash generated from md5() is not reversible i.e can’t be converted to the original string.

This function now is not considered as safe because there are open databases which can be used to reverse checking the plaintext of a hashed value. You can find a list of MD5 hash database here.

6)md5() - 一些开发者将密码明文的存在数据库里,这样不安全。这个函数将目标字符串生成32位的md5 hash值。通过md5函数生成的hash值是不可逆的,就是说不能被还原成初始字符串。

该函数现在不被认为是安全的,因为有开放的彩虹表用于还原hash值,你可以在这儿找到一个开放的hash数据库。

7) sha1() – This function is similar to md5 but it uses different algorithm and generates 40 characters hash  of a string compared to 32 characters by md5(). And one more note is that don't forget to put the salt, otherwise your life will be salty.

7)sha1() - 这个函数与md5类似,但是它是用不同的算法生成了40位的hash值,与md5生成的32位不同。另外需要提醒,别忘记使用salt,否则你会遇到麻烦。

8) intval() – Please don’t laugh. I know this is not a security function, it is function which gets the integer value from the variable. But you can use this function to secure your PHP coding. Well, this function is most frequently used when you want to parse some value of integers such as ids, ages etc.

8)intval() - 请别觉得好笑。我这个这不是个安全函数。这个函数时用户从变量中取得整型的。但是你可以使用它提高PHP编码的安全。这个函数常用于需要解析的整数值,比如ids,年龄等等


不断学习,如果有不对的地方,请帮忙指出

内容概要:本文围绕EKF SLAM(扩展卡尔曼滤波同步定位与地图构建)的性能展开多项对比实验研究,重点分析在稀疏与稠密landmark环境下、预测与更新步骤同时进行与非同时进行的情况下的系统性能差异,并进一步探讨EKF SLAM在有色噪声干扰下的鲁棒性表现。实验考虑了不确定性因素的影响,旨在评估不同条件下算法的定位精度与地图构建质量,为实际应用中EKF SLAM的优化提供依据。文档还提及多智能体系统在遭受DoS攻击下的弹性控制研究,但核心内容聚焦于SLAM算法的性能测试与分析。; 适合人群:具备一定机器人学、状态估计或自动驾驶基础知识的科研人员及工程技术人员,尤其是从事SLAM算法研究或应用开发的硕士、博士研究生和相关领域研发人员。; 使用场景及目标:①用于比较EKF SLAM在不同landmark密度下的性能表现;②分析预测与更新机制同步与否对滤波器稳定性与精度的影响;③评估系统在有色噪声等非理想观测条件下的适应能力,提升实际部署中的可靠性。; 阅读建议:建议结合MATLAB仿真代码进行实验复现,重点关注状态协方差传播、观测更新频率与噪声模型设置等关键环节,深入理解EKF SLAM在复杂环境下的行为特性。稀疏 landmark 与稠密 landmark 下 EKF SLAM 性能对比实验,预测更新同时进行与非同时进行对比 EKF SLAM 性能对比实验,EKF SLAM 在有色噪声下性能实验
内容概要:本文围绕“基于主从博弈的售电商多元零售套餐设计与多级市场购电策略”展开,结合Matlab代码实现,提出了一种适用于电力市场化环境下的售电商优化决策模型。该模型采用主从博弈(Stackelberg Game)理论构建售电商与用户之间的互动关系,售电商作为领导者制定电价套餐策略,用户作为跟随者响应电价并调整用电行为。同时,模型综合考虑售电商在多级电力市场(如日前市场、实时市场)中的【顶级EI复现】基于主从博弈的售电商多元零售套餐设计与多级市场购电策略(Matlab代码实现)购电组合优化,兼顾成本最小化与收益最大化,并引入不确定性因素(如负荷波动、可再生能源出力变化)进行鲁棒或随机优化处理。文中提供了完整的Matlab仿真代码,涵盖博弈建模、优化求解(可能结合YALMIP+CPLEX/Gurobi等工具)、结果可视化等环节,具有较强的可复现性和工程应用价值。; 适合人群:具备一定电力系统基础知识、博弈论初步认知和Matlab编程能力的研究生、科研人员及电力市场从业人员,尤其适合从事电力市场运营、需求响应、售电策略研究的相关人员。; 使用场景及目标:① 掌握主从博弈在电力市场中的建模方法;② 学习售电商如何设计差异化零售套餐以引导用户用电行为;③ 实现多级市场购电成本与风险的协同优化;④ 借助Matlab代码快速复现顶级EI期刊论文成果,支撑科研项目或实际系统开发。; 阅读建议:建议读者结合提供的网盘资源下载完整代码与案例数据,按照文档目录顺序逐步学习,重点关注博弈模型的数学表达与Matlab实现逻辑,同时尝试对目标函数或约束条件进行扩展改进,以深化理解并提升科研创新能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值