English 中文(简体)
PHP: 将重心从窒息转向过度流入
原标题:PHP: Converting big integer from string to int with overflow

PHP不支持未签名的帐篷。 有办法将未经签名的批量表示转换成已签字的批量过量分类?

Example:
On a 32 bit system, PHP can store int values <= 2147483647. I want a way to convert the string "2147483648" to integer, causing it to overflow to -2147483648 instead of being reduced to 2147483647.

Why do I want to do this?
I store IPv4 addresses in a database as unsigned int (32 bits). I want to do binary operations on the addresses in PHP to check for subnets. This needs to be done on every request, so it needs to be quick. Therefore it seems better to store the IP address as an unsigned int rather than storing a string which will have to be converted back and forth.

最佳回答

这里的工作环绕。

<?php
    $unsignedString = "3000000000";
    echo "unsigned string: ".$unsignedString."<br />";
    $signedInt = intval(doubleval($unsignedString));
    echo "signed int: ".$signedInt."<br />";
?>
问题回答

这样做的最快和最容易的方法是让你的RDBMS做些事情。

您可以发现,通过检查预先确定的固定代码(<代码>)的数值,计算出净值的大小。 PHP_INT_SIZE。 如果采用32个轨道系统,则将达到4个,如果采用64个轨道系统,则有8个。

我建议,例如,你把一个变量活起来。

$smallIntegers = intval(PHP_INT_SIZE == 4);

and then in your query use something like this:

SELECT
    ...,
    CASE
        WHEN :smallIntegers: = 1 AND IPAddress > 2147483647 THEN IPAddress - 4294967296
        ELSE IPAddress
    END AS IPAddress,
    ...
...

您可以把IP地址储存在一阵列中,一部分IP放在各阵列的每一部分,这样,最大价值将最终达到255个,储存在任何一户内:

$ipAddress = array(127, 0, 0, 1);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签