[RESOLVED] Invalid JSON: Fatal error: Cannot use object of type carrito as array
I've already checked a long list of details and can't find the solution.
I have a website hosted on two different servers, with the same exactly OS and PHP version, while on one host the script works without any issue, on the other host I get the following error:
"Invalid JSON: <br />
<b>Fatal error</b>: Cannot use object of type carrito as array in <b>/home/username/public_html/lib/class/cart.class.php</b> on line <b>186</b><br />"
What could it be?
Any help will be much appreciated...
We'll need to see line 186 and some preceding lines to have any likelihood of figuring out the root cause. All we know right now is that you are probably passing a variable to json_encode() which is an object of type "carrito", while json_encode() expects that variable to be an array.
I've searched for json_encode() strings before, following this http://blog.ryanrampersad.com/2010/0...lass-as-array/, and could'n find any.
Here you have some code Before line 186:
$sql = mysql_query("
$art = mysql_fetch_assoc($sql);
if ($art['articulo_estado'] == 1)
// obtener ultimo indice del array carrito
$i = 0;
foreach($_SESSION['carrito'] as $k => $v)
$i = $k + 1;
if ($i == 0) $i = 1;
$i = count($_SESSION['carrito']);
// imagen para mostrar en el carrito
$imagen = 'colores/'.$colores_sql_ok['color_imagen'];
$imagen = $art['articulo_imagen'];
if ($art['articulo_precio_oferta'] > 0 and $art['articulo_precio_oferta'] != $art['articulo_precio'])
$precio = $art['articulo_precio_oferta'];
$precio = $art['articulo_precio'];
// finalmente, despues de todo, añadimos el articulo
$_SESSION['carrito'][$i]['id'] = $art['articuloid'];
//After line 186:
$_SESSION['carrito'][$i]['titulo'] = $art['articulo_titulo'];
$_SESSION['carrito'][$i]['dto'] = $art['articulo_descuento'];
$_SESSION['carrito'][$i]['precio'] = $precio;
$_SESSION['carrito'][$i]['imagen'] = $imagen;
$_SESSION['carrito'][$i]['url'] = $art['categoria_url'].'/'.$art['articulo_url'];
$_SESSION['carrito'][$i]['cantidad'] = $cantidad;
$_SESSION['carrito'][$i]['talla']['id'] = $talla['id'];
$_SESSION['carrito'][$i]['talla']['titulo'] = $talla['titulo'];
$_SESSION['carrito'][$i]['color']['id'] = $color['id'];
$_SESSION['carrito'][$i]['color']['titulo'] = $color['titulo'];
if ($_SESSION['user']['dto'] > $art['articulo_descuento'])
$art['articulo_descuento'] = $_SESSION['user']['dto'];
$_SESSION['carrito'][$i]['dto'] = $art['articulo_descuento'];
$precio = $this->aplicar_dto($precio,$art['articulo_descuento']);
$total = $precio * $cantidad;
$_SESSION['carrito'][$i]['total'] = $total;
$_SESSION['carrito'][$i]['ahorro'] = $precio;
$result = true;
$_SESSION['compra']['exito'] = false;
// el articulo no está activo
$this->errors = 'Lo sentimos, este artículo no está disponible';
$this->errors = 'Ups! parece que no hemos encontrado el artículo que buscas...';
if ($sql) mysql_free_result($sql);
You have the entire file attached.
Last edited by NogDog; 12-12-2013 at 04:07 PM.
Reason: added [php] tags
My best guess at this point is some other file earlier in the process is setting $_SESSION['carrito'] incorrectly, but you aren't seeing the problem until the point where it tries to use it within that class.
That's a type error.. you usually see that when trying to use StdClass to an array...
On line 186 you're trying to declare $_SESSION['carrito'][$i]['id'] = $art['articuloid'];
I'm assuming that $_SESSION['carrito'][$i] is a object and not an array... so you'd have to do $_SESSION['carrito'][$i]->id = $art['articuloid'];
OR $_SESSION['carrito'] is the object and you'd have to do $_SESSION['carrito']->$i->id. Or just cast the whole variable
$_SESSION['carrito'] = (array) $_SESSION['carrito']; // put this at line 185
Last edited by mistin.ca; 12-13-2013 at 06:31 AM.
Hello to all,
I'm not able to track the error
What I don't understand is why this script works well on one host and not on another, they have the same Apache and PHP versions...
$_SESSION['carrito'] = array(); is set on the index.php file, can this be the cause?
count() counts countable items, example $ok = count(false); would return 1 to $ok, so my thoughts are that the $_SESSION['carrito'] is not an array or object but some other variable.
Perhaps it is a serialized string still and has not been properly extracted.
... and please remember to wrap code with forum BBCode tags:-
[CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]
If you can't think outside the box, you will be trapped forever with no escape...
Any suggestion for why this script works well on one host and not on another, with the same Apache and PHP versions...
Maybe understanding this could help to fix the issue...
Originally Posted by drtduarte
1. Something changed in the code for one of them, maybe a different version of the software you're using, or an inadvertent change. (Maybe reload the application files?)
2. You're using a different version of PHP, and that has some obscure side effect (maybe a bug fix) that's causing a difference.
3. Different OS (some things work differently under Windows Server than they do on Linux -- not many, but there are some)
Make sure the same PHP modules have been included on each server, JSON for example.
You can check modules loaded and typically their versions by using:
Be sure to delete the file once you've determined everything is the same.
Some more funny info.
Working perfectly on Firefox and IE, just doesn't work on Chrome...
Anything different in the browsers' security/privacy settings regarding cookies?
Are you using the same exact domain name in each instance, including the presence or lack of a "www." sub-domain?
Either of those might affect the session cookie exchange. For the latter, it's a good idea to set session.cookie_domain to ".yourdomain.com" (note the leading dot) so the cookie gets exchanged regardless of whether or not "www." is used.
Thank you all for your help!
So the server was using the same PHP version but not the same php config, so I used a php.ini file to put the same config. The problem is that bad cookies were already saved by the browser with the old php settings...
That's why the script was working on Firefox and IE, as I only used these browsers after changing php.ini settings...
After deleting cookies from chrome, the error is gone. I was not able to understand exactly what setting is causing this issue, but I think I will never know...
Thank you all
After all my curiosity was killing me, so I decided to compare each setting step by step and fighured out that register_globals ON was the setting causing the issue, as almost all the time, the easy issues are the harder to find...
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)