0

I'm creating an 11 page website all within one PHP file. On this specific part, I'm trying to take usernames from $_POST['regUser'] and put into a "logins" array defined as "$logins = array();" .

When I var_dump the array, all I get is "NULL". I'm certain I'm using the right post names and such. I realize that this code might not be near enough to diagnose my problem but I really hope it is.

Remember, this is all just a fraction of a larger program if it seems weird. This specific piece is supposed to log the user in and show the "userHome" page if they supply the correct password and add their username to the $logins array. Everything other than that is working fine. (I call the function in another part of the program)

I'm going to post a few lines of my code because I believe the problem lies somewhere within them (the whole thing is probably 400+ lines).

$regUser = $_POST['regUser'];
$regPass = $_POST['regPass'];

$goodUsername = array('user1', 'user2');
$goodUserPass = array('user1', 'user2');

$logins = array();

if (isset($_POST['userLog'])) {
    if (in_array($regUser, $goodUsername)) {
        $key = array_search($regUser, $goodUsername);
    }

    if($goodUserPass[$key] == $regPass) {
        array_push($logins, $regUser);
        echo userHome(); return;
    }
    else {
        echo invalidLogin(); return;
    }
}

function adLogins() {
    echo phpHeader();

    echo "<center><h1>User Logins</h1></center><br><br>";
    var_dump($logins);
    echo phpFooter();
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Shane
  • 33
  • 5

1 Answers1

0

your $logins var is outside of the scope of the adLogins function

var_dump($logins);

outside of the ad logins function or pass it into the function

function adLogins($logins){}
Dan
  • 3,755
  • 4
  • 27
  • 38
  • Well, at least I got a swift lesson on php variable scope. If that's the only problem anyone saw with the given code, I guess my problem is elsewhere - I still got "NULL" when I did var_dump. I even tried just var_dumping elsewhere outside of a function and got NULL - the post data just isn't going into the array. I guess I can't really post the entire program as I'm re-typing it all on here, plus that's too much for anyone to go through lol. At least I know about scope now. – Shane Nov 19 '14 at 02:22