高手整理的PHP+MySQL安全方案
在网站上,我们要想象每个访问者都是恶心的,这样才能整出更安全的网站。下面是高手整理的php+mysql安全方案,对于初学都特别有用。
1 不要依赖于服务器端的magic_quotes,虽然他们默认都是打开的(magic_quotes_gpc)
将下面两个关闭
ini_set("magic_quotes_runtime", 0);
ini_set("magic_quotes_sybase", 0);
全部自己手工对所有变量添加magic_quotes,就是添加addslashes
代码如下:
function stripslashes_deep($value){
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
function strip_slashes(){
// If already slashed, strip.
//把原有的slash去掉,重新添加自己的magic_quotes
if ( get_magic_quotes_gpc() ) {
$_GET = stripslashes_deep($_GET);
$_POST = stripslashes_deep($_POST );
$_COOKIE = stripslashes_deep($_COOKIE);
$_SERVER = stripslashes_deep($_SERVER);
$_REQUEST = stripslashes_deep($_REQUEST);
}
$_GET = add_magic_quotes($_GET );
$_POST = add_magic_quotes($_POST );
$_COOKIE = add_magic_quotes($_COOKIE);
$_SERVER = add_magic_quotes($_SERVER);
$_REQUEST = add_magic_quotes($_REQUEST);
}
strip_slashes();
显示的时候全部stripslashes还原,虽然这很麻烦,不过如果你有一个模板引擎,可以在变量赋值的时候,统一作这个事情,
例如:
function assign($var, $value = '') {
if (is_array($var)) {
foreach ($var as $k => $v) {
$this->vars[$k] = stripslashes_deep($v);
}
} else {
$this->vars[$var] = stripslashes_deep($value);
}
}
这样可以避免' " 等符号,下面还会有说明
2 对于用户名这样的字段,输入时候,检查不允许有空格,而且必须是字母数字下划线或划线这四种,用正则检查
还有诸如结点名,菜单名,角色名这些不需要用' " 的,一定要在录入之前就检查,或者用替换为空
3 所有ID为数字的变量,必须检查是否为数字,并将变量强制转换成数字
如果ID是前面带0的或者字符型的,可用编码规则对其进行检查,例如全是数字的正则
4 对于php的mysql函数,由于天生一次只能执行一条语句,即;不会自动断。所以无法通过追加语句实现注入,只可能通过将语句插入到变量中来起作用注入,所以对于delete ,select,update都可能有破坏作用。
例如:
delete from tbl_users where user_id = 'admin' 变成 delete from tbl_users where user_id = 'admin or user_id'0',即在输入栏为:admin or user_id'0
所以对于变量参数:
不该有空格的,空格要去掉
该位数字的,一定要转换成数字
有编码规则的,一定要检查编码规则
有长度限制的一定要加入长度限制
绝对不会有注入语句的,就筛查sql关键字
将一些危险字符进行替换,例如用“代替",空格用%20代替,特殊字符转成html等等
当然用addslashes还是有明显效果的,对于php来说,要想在变量中插入条件,必须通过'来完成,所以这一招可以彻底断了所有企图
5 apache,php,mysql不要以系统用户运行
6 连接mysql不要用root
7 系统的所有错误信息必须关闭或者屏蔽
8 屏蔽非主流浏览器的user-agent
9 记录所有的sql操作和用户ip,如果发现危险语句,可以立刻屏蔽该IP,例如3天
如果是用户登录后,有危险sql注入的语句,直接删除该用户,同时屏蔽IP
10 对于验证码要可以采用使用中文,变换字符串样式,必须点击弹出等方法
11 如果还是不安全,就要像电子银行那样,用U盾和专用控件了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode
