Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php session variable multidimensional associative array issue

I've looked around SO, but can't find an explanation to what is going on in my $_SESSION variables.

@ob_start();
$k=@ob_get_contents();
@ob_end_clean();
@session_start();
unset($s,$m);
$m1 = explode(" ", microtime());
$stime = $m1[1] + $m1[0];
echo $k;

$_SESSION['resendConfirmation']['function'] = 'resend';
$_SESSION['resendConfirmation']['id'] = '8';                

print_r($_SESSION);

outputs:

Array ( [resendConfirmation] => 8esend ) 

Why is it string replacing? I've never had this issue before.

What I want is thus:

Array([resendConfirmation] => Array(
                             [id] =>8
                             [function} => resend
                             )
)

I've never had this happen before, I'm totally confused!

UPDATE In response to @DanRedux I've changed to two non-existent variable names to take the referencing out of the equation, still the same result...

$_SESSION['resendConfirmation']['tweak'] = 'resend';
$_SESSION['resendConfirmation']['tweak2'] = '8';

Same result :(

Did a sitewide query of resendConfirmation and none were found, but once I change that array name, it all worked, baffled, but fixed...

$_SESSION['reConfirm']['function'] = 'resend';
$_SESSION['reConfirm']['id'] = '8';             

print_r($_SESSION);
like image 721
MaurerPower Avatar asked Aug 14 '26 09:08

MaurerPower


2 Answers

Since I dont really know what other sorts of shenanigans the code is up to outside of this block you gave us I would say to just try this instead:

$_SESSION['resendConfirmation'] = array('id' => 8, 'function' => 'resend');

If this also fails then there has to be something else going on outside of what you posted. Good luck!

like image 55
EmmanuelG Avatar answered Aug 17 '26 00:08

EmmanuelG


What you think is an multidimensional array really isn't. What really happens is:

What you think is an array is really a string. After that you are trying to access the string as an array. You are trying to access the element id which doesn't exists. PHP always tries to be smarter than it should and just says: OK I'll assume you meant the first index. So basically what happens is:

<?php
$notAnArray = 'somestring';
$notAnArray['id'] = '8'; 

var_dump($notAnArray); // 8omestring

This is the reason you should always enable error_reporting on your development machine:

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);

And never suppress errors using @. Well there are some situations where you can use @, but this really isn't one of them.

like image 29
PeeHaa Avatar answered Aug 16 '26 23:08

PeeHaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!