Technex Posted July 23, 2007 Share Posted July 23, 2007 $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. Quote Link to comment Share on other sites More sharing options...
ss32 Posted July 23, 2007 Share Posted July 23, 2007 you have a syntax error at that line. otherwise, it should work. Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 you have a syntax error at that line. otherwise, it should work. What's that line? Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 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'; } } Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 23, 2007 Share Posted July 23, 2007 he's right... should be if($username==NULL){ and youre missing a closing } at the end. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 23, 2007 Share Posted July 23, 2007 Try empty() instead of "... == NULL" Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 23, 2007 Share Posted July 23, 2007 also try... if (!isset($username)) Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 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']; } Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 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 . Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 You need to take out the: $username = ''; $password = ''; Once you do that it is no longer a Null value. Variables in PHP do not need to be declared. Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 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 . Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 hmm... after lookin at the code you used it should work. You are checking the POST and not the variable my mistake with the last post. I tested it on mine and it is working. Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 can you post your entire page code? Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 if($_POST['username']==NULL){ $errorusernamenull=1; echo 'Bad username'; } if (isset ($_POST['username']) AND ($_POST['password'])){ $username = $_POST['username']; I've got that. Not working. Is it still good? Thanks . Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 can you post your entire page code? Umm, is that necessary? The problem only lies at the top of the page. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 23, 2007 Share Posted July 23, 2007 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 } ?> Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 Thank you, the reason why is because I wanted to display a error if there was no username put in, or password. Not just refresh the page. Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 <?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.. Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 <?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 . Quote Link to comment Share on other sites More sharing options...
per1os Posted July 23, 2007 Share Posted July 23, 2007 I am soo amazed someone did not bring up the point that you cannot check for null using the == operator. www.php.net/is_null You have to use that function to check for nulls...wow. Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 I did mention that wayy back in my first post... Quote Link to comment Share on other sites More sharing options...
Technex Posted July 23, 2007 Author Share Posted July 23, 2007 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); Quote Link to comment Share on other sites More sharing options...
mpharo Posted July 23, 2007 Share Posted July 23, 2007 theres gotta be something else in your code causing it not to work because it is working perfect for me with that code... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.