PHPintval function security tips _ PHP Tutorial

Source: Internet
Author: User
PHPintval function security application tips. We will learn 1. PHPintval function description intval function has a feature: conversion is started only when a number or positive or negative sign is encountered, and the conversion ends when a non-number or string ends, when we learn

1. PHP intval function description

The intval function has a feature: "Conversion starts only when a number or positive or negative sign is encountered, and the conversion ends when a non-number or string ends ", in some applications, due to insufficient understanding of the intval function, improper use may cause security vulnerabilities caused by bypassing some security judgments.

II. PHP intval function analysis

  1. PHP_FUNCTION (intval)
  2. {
  3. Zval ** num, ** arg_base;
  4. Int base;
  5. Switch (ZEND_NUM_ARGS ()){
  6. Case 1:
  7. If (zend_get_parameters_ex (1, & num) = FAILURE ){
  8. WRONG_PARAM_COUNT;
  9. }
  10. Base = 10;
  11. Break;

When the intval function receives a string parameter, it calls convert_to_long_base () for processing, and then calls Z_LVAL_P (op) = strtol (strval, NULL, base). The strtol function is used to process the parameter.

The PHP intval function is prototype as follows:

Long int strtol (const char * nptr, char ** endptr, int base );

This function converts the nptr parameter to a growth integer number based on the base parameter. the base parameter ranges from 2 to 36, or 0. the base parameter indicates the base mode. for example, if the base value is 10, the base value is 10. if the base value is 16, the base value is hexadecimal.

The process is as follows:

Strtol () scans the nptr parameter string, skips the leading space character, and ends the conversion only when a number or positive or negative sign is encountered, and return the result.

When intval is used in the if and so on, it will cause the judgment to be de-meaningful, leading to security vulnerabilities.

III. PHP intval function test code

 
 
  1. //intval.php
  2. $var="20070601";
  3. if (intval($var))
  4. echo "it's safe";
  5. echo '$var='.$var;
  6. echo "
  7. ";
  8. $var1="1 union select 1,1,1 from admin";
  9. if (intval($var1))
  10. echo "it's safe too";
  11. echo '$var1='.$var1;
  12. ?>

IV. practical application of PHP intval functions

WordPress <= 2.0.6 wp-trackback.php Zend_Hash_Del_Key_Or_Index/SQL injection exploit


Sequence 1: PHP intval function description intval function has a feature: conversion is started only when a number or positive or negative sign is encountered, and the conversion ends when a non-number or string ends, in some...

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.