Reputation: 1239
I have the following code, where the var $username doesn't echo, when you type in a value.
//TODO: SET AUTH TOKEN as random hash, save in session
$auth_token = rand();
if (isset($_POST['action']) && $_POST['action'] == 'Login')
{
$errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
$username = $_POST['username'];
if ($username = '')
{
$errors['username'] = 'Username is required';
}
echo $username; // var_dump($username) returns string 0
}
require_once 'login_form.html.php';
?>
login_form is this:
<form method="POST" action="">
<input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
Username: <input type="text" name="username">
Password: <input type="password" name="password1">
<input type="submit" name="action" value="Login">
</form>
The auth token part isn't important, it just when I type in a value in username textbox and press the login button, the username wont echo, var_dump returns string (0) and print_r is just blank.
Upvotes: 0
Views: 140
Reputation: 44444
//TODO: SET AUTH TOKEN as random hash
$auth_token = rand();
if (isset($_POST['action']) && $_POST['action'] == 'Login')
{
$errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
$username = $_POST['username'];
if ($username == '') // **You are assiging not comparing**
{
$errors['username'] = 'Username is required';
}
echo $username; // var_dump($username) returns string 0
}
require_once 'login_form.html.php';
?>
In your login form: (the action attribute..)
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
Username: <input type="text" name="username">
Password: <input type="password" name="password1">
<input type="submit" name="action" value="Login">
</form>
Upvotes: 1
Reputation: 33936
This line is an assignment, not a comparison:
if ($username = '')
You want:
if ($username == '')
Upvotes: 2
Reputation: 47321
silly mistake
if ($username = '') <-- this is an assignment
Should be this
if ($username == '') <-- this is comparison
Upvotes: 3
Reputation: 360602
if ($username = '')
You're missing a =
, so you're assigning an empty string to $username
. Change it to
if ($username == '')
^-- note the 2 equal signs.
Upvotes: 2