2

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);
6
  • 1
    Please fix the first line of your code so that it's readable. Commented May 10, 2012 at 19:11
  • 1
    First line is actually not needed. However, this is a really strange but cool question.. $_SESSION['resentConfirmation']['id'] is somehow referring to the first index, that's why the first character is changing, so why would ['id'] refer to [0].. Maybe you set it by reference at some point? Commented May 10, 2012 at 19:12
  • 8
    Don't abuse the use of @, it suppresses warnings you might care about and has a pretty hefty performance hit. None of the functions in your code should misbehave if you check your inputs properly. There are some exceptions where @ is useful but even then it's dubious. Commented May 10, 2012 at 19:12
  • 1
    PHP 5.3.8 returns Array ( [resendConfirmation] => Array ( [function] => resend [id] => 8 ) ) Commented May 10, 2012 at 19:14
  • the session initialization (the first 8 lines) has been used throughout the project. I don't have this issue anywhere else... Commented May 10, 2012 at 19:19

2 Answers 2

6

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!

Sign up to request clarification or add additional context in comments.

1 Comment

not sure what was going on, but there must have been some reference to resendConfirmation somewhere. renamed, and all worked.
6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.