Jump to content

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


Technex

Recommended Posts

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

 

Yeah, you've gotta be more specific about what's "not working."  Clearly the code that's been posted for you works fine.  Like mpharo says, there's another part of your code that's screwy.

Link to comment
Share on other sites

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

 

Yeah, you've gotta be more specific about what's "not working."  Clearly the code that's been posted for you works fine.  Like mpharo says, there's another part of your code that's screwy.

 

Like I've said it just refreshes the page without displaying nothing at all if you enter nothing in the username form. It doesn't even echo that comment like it should.

Link to comment
Share on other sites

I've P.Med you the whole code mate ;).

 

Don't PM code if you want help. 50 eyes is better than 1 eye. Post it here.

 

 

<?php
if (isset($_POST['username']) AND isset($_POST['password'])) {
if(is_null($_POST['username'])){
	$errorusernamenull=1;
	echo 'Bad username';
}else {
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);
	$db_password = hash('sha512',$password);
                echo 'We are ready to enter data';
}
}else {
echo 'We did not have a username/password entered';
}
?>

 

There is a cleaner version of the code, give that a try and see what happens.

Link to comment
Share on other sites

Like I've said it just refreshes the page without displaying nothing at all if you enter nothing in the username form. It doesn't even echo that comment like it should.

 

Well that has jack-all to do with checking the nullness of $_POST values.  We have no idea what your other, unseen code is supposed to do unless you post a question with the relevant code attached.

 

To get the comment to appear, try empty() instead of is_null().

Link to comment
Share on other sites

Okay here is all the code...

 

<?
(connect stuff is here)-technex

if(empty($_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);

$checkusername = mysql_query("SELECT * FROM users WHERE username = '$username'");
$checkpassword = mysql_query("SELECT * FROM users WHERE password = '$db_password'");
if (mysql_num_rows($checkusername)==0){
$errorusername=1;
}
if (mysql_num_rows($checkpassword)==0){
$errorpassword=1;
}
if ((mysql_num_rows($checkusername) == 1) AND (mysql_num_rows($checkpassword) == 1)){
$getuserinfo = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$db_password'");
$userinfo =  mysql_fetch_array($getuserinfo);
setcookie(id, $userinfo[id], time()+60*60*24*30, "/");//30days
setcookie(password, $db_password, time()+60*60*24*30, "/");//30days

$ip = $_SERVER['REMOTE_ADDR']; 

mysql_query("DELETE FROM guests WHERE ip='$ip'");
mysql_query("UPDATE users SET status='1' WHERE id='$userinfo[id]'");
header('refresh: 0; url=/');
}
}
?>

 

 

 

 

I tried

 

if(empty($_POST['username'])){

$errorusernamenull=1;

echo 'Bad username';

}

 

same problem...

Link to comment
Share on other sites

Wow very inefficient

 

EDIT::: Fixed a syntax error in the mysql_num_rows check.

 

<?php
(connect stuff is here)-technex

if (isset($_POST['username']) AND isset($_POST['password'])) {
if(empty($_POST['username'])) {
	$errorusernamenull=1;
	echo 'Bad username';
}else {
	$username = mysql_real_escape_string($_POST['username']);
	$password = mysql_real_escape_string($_POST['password']);

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

	$check = mysql_query("SELECT * FROM users WHERE username = '" . $username . "' AND `password` = '" . $db_password . "' LIMIT 1");

	if (mysql_num_rows($check) == 1) {
		$userinfo =  mysql_fetch_array($check);
		setcookie('id', $userinfo['id'], time()+60*60*24*30, "/");//30days
		setcookie('password', $db_password, time()+60*60*24*30, "/");//30days

		$ip = $_SERVER['REMOTE_ADDR']; 
		mysql_query("DELETE FROM guests WHERE ip='$ip'");
		mysql_query("UPDATE users SET status='1' WHERE id='" . $userinfo['id'] . "'");
		header('refresh: 0; url=/'); // Why are we just refreshing the page, there is nothing here!
               }else {
                        echo 'Bad username and password';
               }
}
}else {
echo 'No post data was sent';
}
?>

 

Better version, more efficient.

 

Note the note on the header fresh line, that does not make sense to just refresh the page, but an actual page in there other than this one because this will obviously not output anything on a refresh as there are no echo statements, redirect to a "logged in" page or an index page or something.

Link to comment
Share on other sites

Thank you, but could you make it work with my custom error codes?

 

It just displays Bad username and password.

 

Thanks.

 

mpharo it does? Doesn't for me... It logs in fine but doesn't display any error if login was submitted without content (NULL). The connect script is just a inc file, it connects fine.

 

The whole basis of my site is to keep it all running off index.php. login.php is included if ?action=login which happens by the submit button.

header('refresh: 0; url=/'); just refreshes the home page after logging in.

 

Edit: Why did you do this: mysql_query("UPDATE users SET status='1' WHERE id='" . $userinfo['id'] . "'");?

 

And not just: mysql_query("UPDATE users SET status='1' WHERE id='$userinfo[id]'");

Link to comment
Share on other sites

Edit: Why did you do this: mysql_query("UPDATE users SET status='1' WHERE id='" . $userinfo['id'] . "'");?

 

And not just: mysql_query("UPDATE users SET status='1' WHERE id='$userinfo[id]'");

 

Because not having single quotes around indexes in an array causes a notice error, which in return will "slow down the site" as it sees id as being an undefined constant. Just not good practice.

 

Thank you, but could you make it work with my custom error codes?

 

Nope, but good luck. This is HELP not code for me.

 

The whole basis of my site is to keep it all running off index.php. login.php is included if ?action=login which happens by the submit button.

header('refresh: 0; url=/'); just refreshes the home page after logging in.

 

Makes more sense.

Link to comment
Share on other sites

Because not having single quotes around indexes in an array causes a notice error, which in return will "slow down the site" as it sees id as being an undefined constant. Just not good practice.

Okay great, thanks I'll use mysql_query("UPDATE users SET status='1' WHERE id='".$userinfo['id']."'"); from now on. I'll just change the rest of my site when it comes to it.

 

Nope, but good luck. This is HELP not code for me.

 

Umm, thanks then :/. Code comes under help as far as I'm concerned, it's okay I just won't use your single check method.

 

Makes more sense.

 

Yep indeed.

Link to comment
Share on other sites

just use this and check wht is there in POST

 

print_r($_POST) and paste the result

 

It shows nothing, just refreshes the page.

 

But if I put it after if (isset($_POST['username']) AND isset($_POST['password'])){ it then tells me the password and username.

 

Thanks still :).

Link to comment
Share on other sites

this code work fine with me ............. only thing added is trim function for username

 

 

<?

//connection string

 

$_POST['username'] = trim($_POST['username']);

 

if(empty($_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);

 

$checkusername = mysql_query("SELECT * FROM test WHERE test_id = '$username'");

$checkpassword = mysql_query("SELECT * FROM test WHERE test_one = '$password'");

if (mysql_num_rows($checkusername)==0) { $errorusername=1;}

if (mysql_num_rows($checkpassword)==0) {$errorpassword=1;}

if ((mysql_num_rows($checkusername) == 1) AND (mysql_num_rows($checkpassword) == 1)) {

$getuserinfo = mysql_query("SELECT * FROM test WHERE test_id = '$username' AND test_one = '$password'");

$userinfo =  mysql_fetch_array($getuserinfo);

setcookie(id, $userinfo[id], time()+60*60*24*30, "/");//30days

setcookie(password, $password, time()+60*60*24*30, "/");//30days

 

$ip = $_SERVER['REMOTE_ADDR'];

 

// mysql_query("DELETE FROM guests WHERE ip='$ip'");

// mysql_query("UPDATE users SET status='1' WHERE id='$userinfo[id]'");

header('refresh: 0; url=/');

}

}

?>

<form action='arr2.php' method='post'>

<div id='loginbutton'>

<input type='image' name='login' src="" alt='LOGIN' />

</div>

<p>Username:</p>

<p><input type='text' name='username' /></p>

<p>Password:</p>

<p><input type='password' name='password' /></p>

</form>

Link to comment
Share on other sites

Nope doesn't work. I've even copied and pasted all the posted codes, still doesn't :/.

 

Oh well... Guess if people are dumb enough to put nothing in the form then they shouldn't be on the site... :P

 

Thanks though, very much.

Link to comment
Share on other sites

paste the present code which ur trying on the site

 

I like this code, just doesn't show empty forms...

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

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

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

$checkusername = mysql_query("SELECT * FROM users WHERE username = '$username'");
$checkpassword = mysql_query("SELECT * FROM users WHERE password = '$db_password'");
if (mysql_num_rows($checkusername)==0){
$errorusername=1;
}
if (mysql_num_rows($checkpassword)==0){
$errorpassword=1;
}
if ((mysql_num_rows($checkusername) == 1) AND (mysql_num_rows($checkpassword) == 1)){
$getuserinfo = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$db_password'");
$userinfo =  mysql_fetch_array($getuserinfo);
setcookie(id, $userinfo[id], time()+60*60*24*30, "/");//30days
setcookie(password, $db_password, time()+60*60*24*30, "/");//30days

$ip = $_SERVER['REMOTE_ADDR']; 

mysql_query("DELETE FROM guests WHERE ip='$ip'");
mysql_query("UPDATE users SET status='1' WHERE id='".$userinfo['id']."'");
header('refresh: 0; url=/');
}
}

Link to comment
Share on other sites

I've tried that already, it just still refreshes :/.

 

I have a new idea do you think it's because when the form submits it goes to index.php?

 

I'll try messing around with that now.

 

 

<form action='index.php' method='post'>

 

Edit: Also header('refresh: 0; url=/'); doesn't work with IE 7! :S

Link to comment
Share on other sites

try putting else statement ......... as below n check

 

 

 

if(empty($_POST['username'])) {

$errorusernamenull=1;

echo 'Bad username';

}  else {

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

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

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

 

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

 

$checkusername = mysql_query("SELECT * FROM users WHERE username = '$username'");

$checkpassword = mysql_query("SELECT * FROM users WHERE password = '$db_password'");

if (mysql_num_rows($checkusername)==0){

$errorusername=1;

}

if (mysql_num_rows($checkpassword)==0){

$errorpassword=1;

}

if ((mysql_num_rows($checkusername) == 1) AND (mysql_num_rows($checkpassword) == 1)){

$getuserinfo = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$db_password'");

$userinfo =  mysql_fetch_array($getuserinfo);

setcookie(id, $userinfo[id], time()+60*60*24*30, "/");//30days

setcookie(password, $db_password, time()+60*60*24*30, "/");//30days

 

$ip = $_SERVER['REMOTE_ADDR'];

 

mysql_query("DELETE FROM guests WHERE ip='$ip'");

mysql_query("UPDATE users SET status='1' WHERE id='".$userinfo['id']."'");

header('refresh: 0; url=/');

}

}

}

 

 

Link to comment
Share on other sites

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.