Considerations for using the Is_numberic function in PHP

Source: Internet
Author: User
Tags numeric sql injection sql injection attack

0x00 Introduction

Some of the domestic CMS program has been useful to the Is_numberic function, we first look at the structure of this function

BOOL Is_numeric (mixed $var)


It is important that is_numeric determine whether a string is a numeric type. For example-1, 1.5 This type of number in Is_numberic is returned true because it is a numeric type

Whether the 0X01 function is secure

Let's take a look at an example to show whether the function is secure.

The code is as follows Copy Code

$s = is_numeric ($_get[' s ']) $_get[' s ']:0;
$sql = "INSERT into test (type) values ($s);"; is values ($s) is not values (' $s ')
mysql_query ($sql);

The above fragment program is to determine whether the parameter S is a number, is to return the number, not to return 0, and then bring into the database query. (This will not construct the SQL statement)
We convert ' 1 or 1 ' to the value of the s parameter of the 16 0x31206f722031

After the program runs, we query the database to see the following figure:

If you requery the field of this table, and do not filter into another SQL statement, it will result in 2 injections.

0x02 Summary

Try not to use this function, if you want to use this function, it is recommended to use the canonical SQL statement, the condition of the single quotation mark, so that the 16 0x31206f722031 will be displayed in the database. The 1 or 1 will not appear.

If we want to judge whether it is a number we can use the intval () function instead of the is_numeric function so that the above method is invalid, the following example

  code is as follows copy code

$id = Intval ($_get[' id ']);

Echo intval//
Echo intval (4.2);//4
Echo intval (' n ');//km
Echo intval (' +42 ');//The
Echo I Ntval ('-42 '); -42
Echo intval (042);//1e10
Echo intval (' 042 ');//The
Echo intval ();//1410065408
Echo intval (' 1e1 0 '); 1
Echo intval (0x1A);//-
Echo intval (42000000);//42000000
Echo intval (420000000000000000000);//0
echo intval (' 420000000000000000000 '); 2147483647
Echo intval (8);//
Echo intval (' n ', 8);//

The most common use of

Intval is in the process of filtering the data into the database, converting it to an integral type, avoiding the occurrence of SQL injection attack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.