I can't seem to figure out this simple issue.

Basically, I'm doing an integer validation to ensure a user does not exceed the range of a "BIGINT" for MYSQL.

As you know, the range of a BIGINT is:
-9223372036854775808 to 9223372036854775807

So, to test the validation, I used:
-9223372036854775809 and 9223372036854775808

The validation test is returning saying that it's valid...however, when I put an extra digit on the end of each number, it then becomes invalid.

Here's my code:

if ($data == "bigint" && ($value < -9223372036854775808 || $value > 9223372036854775807)) {
    $error = true;
    echo 4;
}

Recommended Answers

All 20 Replies

Integer overflow is happening here i think.
PHP is not able to handle unsigned integers, and converts values over 2^31 to signed. Also note that php int size is platform dependant. For 32 bits PHP_INT_SIZE is 4 and for 64bit systems is 8.

Use GMP or bcmath extensions ("for arbitray lenght integers").

Thanks for your reply.

I tried the GMP method by doing the following:

if ($data == "bigint" && ($value < gmp_abs("-9223372036854775808") || $value > gmp_abs("9223372036854775807"))) {
    $error = true;
    echo 4;
}

However it turned the following error message:

<b>Fatal error</b>:  Call to undefined function:  gmp_abs() in <b>/home/content/z/u/r/zurompeta/html

/inventory/validation/validation.php</b> on line <b>230</b><br />

Change the config of php.ini, search for:

;extension=php_gmp.dll

or

extension=php_gmp.so

under "Dynamic extension" section and erase the semicolon, save your php.ini file then restart your server. Also check that in the ext folder you already have the gmp ext installed, if not go to http://gmplib.org/#DOWNLOAD

Hmm, I can't seem to find my php.ini file.

I created a file that consisted of:

<?php phpinfo() ?>

It said the file path is: /web/conf/php.ini

When I tried: www.mydomain.com/web/conf/php.ini it said page not found.

Any ideas?

php.ini file is to be modified by admin, you cannot access it that way becuase it is not to be viewed by anyone except the server admin or the person that administers the site, if you pretend to modify the php.ini then do so by editing directly with notepad (if you are in windows) or any other similar tool. Perform a search in your server php directory for php.ini

So, I installed WAMP and transfered my files onto my localhost server vesus the one provided by godaddy. Adjusting from PHP 4.3.11 to PHP 5.3.0 has been a bit stressful but will be worth it in the end.

Anyways, I found the php.ini file and the semi-colon was not there, however the actual .dll file was not in the ext folder. I went to the site you provided a URL for but I'm not sure what to do with what I downloaded, there's a bunch of files that has got me pondering.

On some hosting companies by default the gmp extensions are not enabled in the php.ini file or even they are not supported at all, contact your hosting company and ask them if they support gmp extensions, if so that means they already have the libraries so you only need to enabled them to do this download the php.ini file from your root dir (web/conf/php.ini) modify it (whether you erase the semicolon at the beginning of ;extension=php_gmp.dll or adding the complete line (without semicolon of course) then reupload it to the same dir, for this to happen it is neccessary some kind of server restarting in order for the changes to take effect (perhaps only godaddy can do this) so give them some time.

So, I installed WAMP and transfered my files onto my localhost server vesus the one provided by godaddy. Adjusting from PHP 4.3.11 to PHP 5.3.0 has been a bit stressful but will be worth it in the end.

If you are using WAMP for development and testing purposes, and if there is no php_gmp.dll file in folder ext then google for this file and download it to the ext folder, I dont use WAMP but XAMPP so as far as I can tell the latest version of this .dll is 5.2.9.9 after that the most easy way to enable the extension is from the WAMP tay icon (PHP->PHP_Extension->php_gmp).

I went to the site you provided a URL for but I'm not sure what to do with what I downloaded, there's a bunch of files that has got me pondering.

Since you are using WAMP (Windows) there is no need to mess with gmp files of the url I gave you as I did not know what os you were using, those files are only for non-windows os.

Yea I downloaded the .dll file and put it in the proper folder and php_gmp has been checked since it's been installed.

I'm sure I'd need to refresh the server, which as far as I know, would be closing/opening WAMP after closing Firefox.

Is the code being properly used?

I tried the GMP method by doing the following:

if ($data == "bigint" && ($value < gmp_abs("-9223372036854775808") || $value > gmp_abs("9223372036854775807"))) {
    $error = true;
    echo 4;
}

end quote.

Sure, it will print 4 as long $data = "bigint" and value is )-9223372036854775808, 9223372036854775807(

Yea for sure.

It's still not working though. This is certainly frustrating. I'm gonna call it a day and come back to it tomorrow.

Thanks again for your help thus far

Ok I think I'm getting somewhere.

However, I need to get past another error. When I open WAMP, this error pop-up reads:

PHP Startup: gmp: Unable to initialize module
Module compiled with module API=20050922
PHP compiled with module API=20090626
These options need to match

Any ideas?

like I said, I prefer to use XAMPP instead WAMP for devel so I'm not able to give an accurate answer about the WAMP.
However it seems you are having version incompatabilities for the gmp funcs (outdated), search in WAMP site about this issue or google for it... google is your firend :), by the way in order to smash this issue once and for all I've tested your snippet in XAMPP and it ran with no problems and the only thing I did was to un-comment the the extension statement.

Regards.

Yea I've been googling like a mad-man with no luck unfortunately. I did try out XAMPP though, there were a few uncertainties with it though.

Such as:
-Where is the root folder?
-I had trouble Starting up the Apache server in the XAMPP control panel

Lastly, I'd assume, I'd need to uninstall WAMP prior to installing XAMPP correct?

-Where is the root folder?

x:\xampp\htdocs

Lastly, I'd assume, I'd need to uninstall WAMP prior to installing XAMPP correct?

that's correct

Alright, I'll give that a shot and let you know how it goes

Alright, here's what I did:

1. Uninstalled WAMP
2. Deleted WAMP folder
3. Installed XAMPP (Was I supposed to install Apache, MySQL, FireZilla as services?)
4. Setup phpMyAdmin
5. Moved site directory to htdocs folder
6. Editted php.ini (deleted the appropriate semi-colon)
7. Checked to see if I have php_gmp.dll (which I do)
8. Ran xampp_stop.exe
9. Closed the XAMPP control panel
10. Ran xampp_start.exe
11. Opened firefox and went to the proper page
12. Result: Fail
13. :'(

13. :'(

lol
I dont know more what to tell you... All my replies had been based solely in the code you provided in your very first post and every time you came with a failure (this is not a problem since that's why forums are for provided that you have tried harder to solve it before...) moreover in your las post you dont specified accurately the reason of the 'failure' so I assume that the real error would be happening somewhere else in your code and not in the snipet you provided in your first post.

REgards

So strangely enough, the gmp_abs functions started to work...

However, I'm not sure they're working the way they should be. I've done some reading in all sorts of GMP functions and it looks as if "gmp_abs" returns a "resource" instead of an actual integer. (Not to mention gmp_abs only returns positive numbers.) Then I would have to use "gmp_intval" to convert the resource to an integer, but I hit a wall and end up back to square one by getting stopped by the PHP max number of 274982683358.

You said you got it to work didn't you?

Then I would have to use "gmp_intval" to convert the resource to an integer, but I hit a wall and end up back to square one by getting stopped by the PHP max number of 274982683358.

Yes, that is like trying to push an elephant in a bottle...

You said you got it to work didn't you?

That's correct, but only for comparison purposes as you stated in your fist post. If you intend to do maths that IS another thing....
Try something like:

$val = (float)gmp_strval("-9223372036854775808");

Take in mind that the conversion from string to float may lead to lost of precision if you are working with floating point numbers (this is not your case, you know...just in case...)

Bah, nothing worked. I just decided to do:

$str = strval($value);
$len = strlen($str);
$isNeg = strchr($str,"-");
$isMax = strpos($str,"99");
$isExceed = false;

if ($isNeg != "") {
    $isNeg = true;
}

if (!$isNeg && $isMax == 0 && $len >= 19) {
    $isExceed = true;
} elseif ($isNeg && $isMax == 1 && $len >= 20) {
    $isExceed = true;
}

So this allows a user to input a max number of 999,999,999,999,999,999 and a min number of -999,999,999,999,999,999 without exceeding MySQL's max number.

It's better than nothing I suppose :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.