Jump to content

if($username==NULL{???????????? Won't work :( Thanks.


Recommended Posts

$username = '';
$password = '';

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];


if($username==NULL{
$errorusernamenull=1;
echo 'Bad username';
}

(all rest of code is fine here)

 

I would like the if($username==NULL to work it doesn't seem to work atm if someone posts nothing as there username.

 

Thanks :). The login script does work just I want to add more things to it.

Link to comment
https://forums.phpfreaks.com/topic/61368-ifusernamenull-wont-work-thanks/
Share on other sites

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

your missing a closing } for the first If statement and a ) in the second one. it is prolly there just not posted in the code.

 

Try doing this:

 

$username = '';
$password = '';

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];


if(is_null($username)){
$errorusernamenull=1;
echo 'Bad username';
}
}

Oh yes, sorry. That's just from me posting the wrong version.

 

It's fixed now yet it still doesn't work.

 

Just reloads the page shows nothing...

 

I'll try now Wildbug. thank you

 

Edit:

 

if(empty($username)){

$errorusernamenull=1;

echo 'Bad username';

}

 

Doesn't work either if I am doing it correctly...

 

Edit:

 

if(is_null($username)){

$errorusernamenull=1;

echo 'Bad username';

}

 

also doesn't work :(.

 

thank you

also try...

 

if (!isset($username))

 

 

 

if (!isset($username)){

$errorusernamenull=1;

echo 'Bad username';

}

 

Doesn't work either, just reloads page like all the others.

 

if (isset($username)){

$errorusernamenull=1;

echo 'Bad username';

}

If I do that then enter any old username/letter it works, displays bad username and error.

 

Thanks for all the helps guys though.

I see what the problem is, You have an nested If statement which sets the variable before it checks for a null value.

 

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

 

then you are checking for a null value, but if $POST['username'] is not set it just bypasses the rest of it...You need to close the first if statement and add an Else for the second one, or make it unnested.

 


if(is_null($username)){
$errorusernamenull=1;
echo 'Bad username';
}

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

}


Ah nice find!

 

 

I tried this:

 

$username = '';
$password = '';

if(is_null($_POST['username'])){
$errorusernamenull=1;
echo 'Bad username';
}

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

 

It doesn't work, same problem.

 

And I couldn't do if(is_null($username)){ because I haven't set the $username variable with the right info :P.

 

I read it on a website, it's good to clear all variables before executing the script to stop hackers using it?

 

Anyway trying now.

 

Not working. Should I be using $_POST['username'] or $username? Check if it's empty before starting sounds good...

 

==NULL is still good?

 

I'm trying everything :(.

Test case using the following code:

 

<html><body><pre><?php

echo "Username: ",$_POST['username'],"\n";
echo empty($_POST['username']),"\n";
echo $_POST['username'] == NULL,"\n";
print_r($_POST);

?></pre>
<form action="test.php" method="POST">
<input name="username" type="text" />
<input type="submit">
</form></body>
</html>

 

Nothing in the username field:

 

Username: 
1
1
Array
(
    [username] => 
)

 

Something in the field:

 

Username: somename


Array
(
    [username] => somename
)

 

Why don't you use something like:

 

<?php

if (
isset($_POST['username'])
&& !empty($_POST['username'])
&& isset($_POST['password'])
&& !empty($_POST['password'])
) {
// then whatever
}

?>

<?php

if(!$_POST['username']){
$errorusernamenull=1;
echo 'Bad username';
}

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

?>

 

I tested and verified it is working on my end..

 

Damn it looks like it's going to work.. But it doesn't :(.

 

if(!$_POST['username']){
$errorusernamenull=1;
echo 'Bad username';
}

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($_POST['password']);

$db_password = hash('sha512',$password);

 

Doesn't work just refreshes page, try it here:

 

http://84.26.66.197 Please only go on there if you need to, limited bandwidth. It's fine if you want to help ;).

 

Thanks so much :).

Yep that's true. Thank you very much.

 

But still it's not working even with this new one:

 

if(is_null($_POST['username'])){
$errorusernamenull=1;
echo 'Bad username';
}

if (isset ($_POST['username']) AND ($_POST['password'])){

$username = $_POST['username'];

$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($_POST['password']);

$db_password = hash('sha512',$password);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.