unserialize(): Error at offset 181 of 849 bytes - php

This works -
$serialized = 'a:23:{s:6:"fields";a:5:{s:21:"display_name_creditor";s:1:"1";s:24:"display_name_constituent";s:1:"1";s:14:"email_creditor";s:1:"1";s:14:"phone_creditor";s:1:"1";s:12:"total_amount";s:1:"1";}s:5:"id_op";s:2:"in";s:8:"id_value";a:0:{}s:21:"receive_date_relative";s:1:"0";s:17:"receive_date_from";s:0:"";s:15:"receive_date_to";s:0:"";s:25:"contribution_status_id_op";s:2:"in";s:28:"contribution_status_id_value";a:1:{i:0;s:1:"1";}s:16:"total_amount_min";s:0:"";s:16:"total_amount_max";s:0:"";s:15:"total_amount_op";s:3:"lte";s:18:"total_amount_value";s:0:"";s:6:"gid_op";s:2:"in";s:9:"gid_value";a:0:{}s:8:"tagid_op";s:2:"in";s:11:"tagid_value";a:0:{}s:11:"description";s:20:"Soft Credit details.";s:13:"email_subject";s:0:"";s:8:"email_to";s:0:"";s:8:"email_cc";s:0:"";s:10:"permission";s:21:"access CiviContribute";s:6:"groups";s:0:"";s:9:"domain_id";i:1;}';
$result = unserialize($serialized);
This does not -
$serialized = 'a:23:{s:6:"fields";a:5:{s:21:"display_name_creditor";s:1:"1";s:24:"display_name_constituent";s:1:"1";s:14:"email_creditor";s:1:"1";s:14:"phone_creditor";s:1:"1";s:12:"amount";s:1:"1";}s:5:"id_op";s:2:"in";s:8:"id_value";a:0:{}s:21:"receive_date_relative";s:1:"0";s:17:"receive_date_from";s:0:"";s:15:"receive_date_to";s:0:"";s:25:"contribution_status_id_op";s:2:"in";s:28:"contribution_status_id_value";a:1:{i:0;s:1:"1";}s:16:"total_amount_min";s:0:"";s:16:"total_amount_max";s:0:"";s:15:"total_amount_op";s:3:"lte";s:18:"total_amount_value";s:0:"";s:6:"gid_op";s:2:"in";s:9:"gid_value";a:0:{}s:8:"tagid_op";s:2:"in";s:11:"tagid_value";a:0:{}s:11:"description";s:20:"Soft Credit details.";s:13:"email_subject";s:0:"";s:8:"email_to";s:0:"";s:8:"email_cc";s:0:"";s:10:"permission";s:21:"access CiviContribute";s:6:"groups";s:0:"";s:9:"domain_id";i:1;}'
I have just changed the total_amount field to amount and I receive this error --
unserialize(): Error at offset 181 of 849 bytes
I've checked the solution provided at unserialize() [function.unserialize]: Error at offset & http://davidwalsh.name/php-serialize-unserialize-issues, but nothing worked.
Also tried using base64_encode/decode before serialize/unserialize
Any hints ?

The second one is invalid the reason being that field s:12:"amount" states that this value is a string of length 12. However amount only has 6 bytes. so you want to use s:6:"amount"
That being said its probably easier to unserialize then make a change then serialize the new value.

Related

Incorrect PHP calculation

Can someone give me an explanation on why this is happening in PHP:
echo (0.29*100)%100 // result 28
It's probably very simple but logically I do not see any explanation. Probably how PHP works in the background.
I was trying to get the first two decimal positions of a number when I ran into this case. Result should be 29 naturally.
If I round the multiplication the result is fine:
echo (round(0.29*100))%100 // result 29
If you run that code
echo (0.29*100)%100;
in in PHP8.1.1 the error message gives you a clue
PHP 8.1.1
Deprecated: Implicit conversion from float 28.999999999999996 to int loses precision in D:\PHP-SOURCE\Testing\tst.php on line 14
Call Stack:
0.0001 393688 1. {main}() D:\PHP-SOURCE\Testing\tst.php:0
28

Notice: Use of undefined constant ‏ - assumed '‏' in C:\xampp\htdocs\aa.php on line 5 vs Notice a non well former numeric value

I tried running this simple php code in 2 different machines and i get different results for some reason.
the code:
<?php
$a = 5;
$b = "22ff67";
$s = $a - $b;
echo $s;‏
?>
in machine 1 i get the error:
Notice a non well former numeric value encountered in .... on line 4
17
and in machine 2 i get:
-17
Notice: Use of undefined constant ‏ - assumed '‏' in
C:\xampp\htdocs\aa.php on line 5
i run the code using XAMPP apache server...
what could be the reason for the different results?
the output is -17 because it tries to subtract 22 from 5 but because 22 is part of a string it gives the error? (it takes only 22 because it's the number before the first letter in the string (in strings which has signs other than '.', 'e','E' it takes the first number up to a sign other than those)
Thanks.

PHP Division by zero warning with a check

I have a script that does some math and makes an excel sheet but i randomly get a
Warning: Division by zero in myfile.php on line 170
Which throws off my header() changes.. I say that its random because it will work one time but if i refresh the page it breaks the most confusing part is that i also have a check if its zero here is the code
167 if($cartonCount > 0){
168 echo "-----" . $cartonCount . "-----";
169 $mellow = $qty/$cartonCount;
170 $leftovers = $qty % $cartonCount;
171 for($x = 1 ; $x <= $mellow ; $x++){
If the carton count is 0 it shouldn't run at all yet i still get the warning.. if anyone has an idea let me know here is a sample of the out put
-2.38----------7.63----------12----------10----------7.5---------
7.5----------4.5----------4.5----------4.5----------4.5----------7.5-
---------1----------9.5----------7.5----------2.38----------0.06-----
Warning: Division by zero in /nfs/c08/h02/mnt/122022/domains/superstructs.com/html/catalog/test/samples/upsExport.php on line 170
-----2.38----------7.63----------7.5----------7.5----------2.38------
----0.06-----
Then, I'm putting this in as answer:
It might be because the value is NULL which, in fact, is not >0. But it will still give you that error
I notice that the place where it breaks is when $cartonCount is "0.06". I suspect that $cartonCount is being stored as a string, possibly with the European style 0,06 decimal notation. When PHP tries to convert that to a number, it sees it as a zero. I'm not sure why it is acting differently for the modulo operator and not for division or equality checks, but putting $cartonCount = intval($cartonCount) before line 167 should make things act consistently (although it might end up interpreting your 0.06 as a zero).
The modulo operator converts (or rounds) the second number to an integer. 0.06 results in 0 which creates the warning.
Possible options are for example casting to integer or round() before checking > 0, round up using ceil() or check > 1.
Based on the context ceil() seems to be the best option.

Array Offset Encryption

Right now I am developing a cryptography app and I have a problem with this one
$stralphabet=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
As you can see the string above is a string of alphabet. I am making a caesar shift encryption, the algorithm is simple.
Count the number of string and then substitute each letter based from the position, so for example:
string is "AB" after encryption it would be "CD"
The problem is if I have the string YZ" it gives me an error
Notice: Undefined offset: 26 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19
Notice: Undefined offset: 27 in C:\xampp\htdocs\cryptographer\encrypt.php on line 19
Can you help me with this one?
You apparently are accessing $stralphabel without checking for edge case (i.e. $stralp[$i+1]). You should add some checks or use modulo operator, so instead of just:
$stralp[$i+1]
you'd have
$stralp[($i+1) % XXX]
where XXX is number of entries in your stralp array.
PS: this is not even close to cryptography.
Your array has 26 letters indexed from 0 to 25, so something is wrong with your cycle end condition.

Unserialize offset error, string length seems too long

I am trying to unserialize a longtext (utf8_unicode_ci) from a database query, but when I do I get the following error.
unserialize(): Error at offset 6 of 737 bytes
I guessed that my string was invalid so I outputted it to the browser. I noticed the string length was a little longer then what it was actually supposed to be. When I copy and paste the output in the browser and hard code it into the php the sting length changes to a smaller one and the unserialize works.
Database string...
ASCII - string(737) "a:10:{s:2:"id";i:2234950;s:13:"full_describe";s:11:"Brace start";s:6:"person";s:17:"Dr Aalok Y Shukla";s:8:"datetime";s:25:"2014-01-06T09:00:00+00:00";s:8:"duration";i:30;s:11:"on_waitlist";b:0;s:10:"company_id";s:5:"35927";s:8:"attended";b:1;s:6:"_links";a:2:{s:4:"self";a:1:{s:4:"href";s:60:"http://uk.bookingbug.com/api/v1/admin/35927/bookings/2234950";}s:6:"client";a:1:{s:4:"href";s:57:"http://uk.bookingbug.com/api/v1/admin/35927/client/809828";}}s:11:"appointment";a:1:{s:11:"description";s:11:"Brace start";}}"
Copied and pasted string into php...
ASCII - string(517) "a:10:{s:2:"id";i:2234950;s:13:"full_describe";s:11:"Brace start";s:6:"person";s:17:"Dr Aalok Y Shukla";s:8:"datetime";s:25:"2014-01-06T09:00:00+00:00";s:8:"duration";i:30;s:11:"on_waitlist";b:0;s:10:"company_id";s:5:"35927";s:8:"attended";b:1;s:6:"_links";a:2:{s:4:"self";a:1:{s:4:"href";s:60:"http://uk.bookingbug.com/api/v1/admin/35927/bookings/2234950";}s:6:"client";a:1:{s:4:"href";s:57:"http://uk.bookingbug.com/api/v1/admin/35927/client/809828";}}s:11:"appointment";a:1:{s:11:"description";s:11:"Brace start";}}"
As you can see the strings are the same but the string from the database call shows the offset error.
Do I need to do some sort of decoding or formatting on the string returned from the database?
Thanks
The problem was that I was serializing multidimensional arrays which causes problems.
To fix just base64_encode() the string before you use serialize and vise versa when you use unserialize.
//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));
//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
Article found at http://davidwalsh.name/php-serialize-unserialize-issues
Hope this helps others

Categories