1

I am trying to obtain the first entry if nothing is set in the $_POST or $_SESSION but when I output the below using die(print_r($nmcu)); I always only get 1, if I add numbers to the end of "entry 1" and "exit 2" it seems to work but I'm not sure why, I dont want numbers in the names...

<?php
session_start();
$entry = array(
"entry" => array("user" => "username",
        "pass" => "password",
        "host" => "localhost",
        "port" => 1111,
        "protocol" => "http"),

"exit" => array("user" => "username",
        "pass" =>   "password",
        "host" =>   "localhost",
        "port" =>   1111,
        "protocol" => "http"));


if (isset($_POST['currentEntry'])) {
    $_SESSION['currentEntry'] = $_POST['currentEntry'];
}
if (isset($_SESSION['currentEntry'])) {
    $currentEntry = $_SESSION['currentEntry'];
} else {
    $keys = array_keys($entry);
    $currentEntry = $keys[0];
    $_SESSION['currentEntry'] = $currentEntry;
}
$nmcu = $entry[$currentEntry];
?>
6
  • 1
    Could you explain better what you are trying to do? Commented May 22, 2013 at 10:54
  • Me couldn't understand what you are trying Commented May 22, 2013 at 10:55
  • Sorry if it is not clear. I want this code to return the "entry" array if there is nothing being $_POST or in $_SESSION so if you go to the page without any form submit i want it to return the "entry" array details but if they $_POST =entry or =exit then return according array details Commented May 22, 2013 at 11:01
  • For me your code works just fine (as you expect) - I have tested it on my server. Commented May 22, 2013 at 11:01
  • When i run it and output the array using print_r($nmcu) i always get the number 1 and no array info. Commented May 22, 2013 at 11:05

1 Answer 1

1

Clean your session (or check it with var_dump($_SESSION). I'm quite sure that you have an invalid key there, so print_r() prints nothing and returns , which is then outputed bydie()`.

Also, I suggest using var_dump() instead of print_r() for such tests.

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

2 Comments

You are correct, i deleted my session and it started working. However when i delete the session and attempt to go to /index.php?currentEntry=exit it still sets entry as the currentEntry so it appears the $_POST is not being read, i have attempted deleting the session and still the same.
That's a get request, so you should use $_GET.

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.