0
<?php session_start();?>

<?php
    !isset($_GET["id"]) ? header("Location: store.php") : $id = $_GET['id'];
?>

<?php
    $_SESSION["items"][$id] = 1; 
    print_r($_SESSION);
?>

This outputs

    Array
    (
        [items] => Array
            (
                [1] => 1
                [] => 1
                [2] => 1
                [3] => 1
            )
    )

There always appears one key empty. Why that key is empty.

3
  • 1
    Try clearing out the Session in your browser and test it again. Commented Dec 30, 2011 at 17:32
  • 1
    Can you add the output of print_r($_GET) to your question? Commented Dec 30, 2011 at 17:32
  • Do a session_destroy(); and try again Commented Dec 30, 2011 at 18:10

1 Answer 1

4

It's this line here:

$_SESSION["items"][$id] = 1;

if $id is undefined, then the undefined key is set to 1, you need to validate the $id before you assign it, a simple way to do this would be:

if(isset($id)){
    $_SESSION["items"][$id] = 1;
}

Should do the trick :-)

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

2 Comments

But if $_GET["id"] isn't set, it should redirect to a different page, so he shouldn't have that problem.
@Shredder - PHP will keep processing the document regardless of headers, it's the client that decides to follow the Location header. So good point! Remember to add an exit(); command after outputting your location header :-)

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.