Jump to content

[SOLVED] login problem..


Grant132

Recommended Posts

Hello...

 

I've got a simple login system and everything works except for one little problem. It has an error system that will display "An error occured, please re-enter your details." if what you put into the login form doesn't match with the FFDB. That will work if you get it wrong, but if you get the details correct then it will still show the error message ontop of the included page..Is there a way to fix this?

 

<?php
session_start(); 
$user    =    $_POST['username']; 
$pass    =    $_POST['password'];
$Admin  =  "Admin";
$test  =  "reallysecureadminpassword";

$details    =    file("db.php"); 

foreach($details    as    $key    =>    $Val) 
{ 
$data[$key]    =    explode("||",    $Val); 
} 
for($k    =    0;    $k    <    sizeof($details);    $k++) 
{ 
                $u    =    $data[$k][0]; 
                $p    =    $data[$k][1];

if($user  ==  $Admin  AND  $pass  ==  $test)  {
                                        $_SESSION['user']  =  $user; 
                                        $_SESSION['logged']  =  "yes"; 
                                        $_SESSION['test']  =  "1"; 
                                echo  "welcome  admin";
                                        include("news.php");
                                        break;       
                }
        if($user  ==  $u)  {
        if($pass  ==  $p)  { 
                                        $_SESSION['user']  =  $user; 
                                        $_SESSION['logged']  =  "yes"; 
                                        $_SESSION['test']  =  "1"; 

                                        include("news.php");
                                        break;
                                }  else  {       
                                              session_destroy();
                                        include("wrong_pw.php");
                                        break;
                                        }

          }else  {
          if($pass  ==  $p  ||  $pass  !=  $p){
                echo  "An  error  occured,  please  re-enter  your  details.";
                }
}
}
?> 

 

Any help would be much appreciated,

Grant

Link to comment
Share on other sites

<?php
if($pass  ==  $p  ||  $pass  !=  $p){
                echo  "An  error  occured,  please  re-enter  your  details.";
                }
?>

you are showing the error even if the password is right, get rid of the $pass == $p || and you should be good

Link to comment
Share on other sites

Hmm..

Your code should read as :

  if($pass  !=  $p){

echo  "An  error  occured,  please  re-enter  your  details.";

}

Just asking cause I'm not seeing where it is messing up.

 

Some thoughts though, echo out the variables to see what you are getting.

 

Also, get rid of that if completely because it is not needed. Inside your if ($user = $u) you check to see if the password matches, you can just echo it out in that if.

 

A thing I am confused at is the for loop, it looks like you are checking multiple users in, i could just be misreading it since I'm not too familiar with that code.

 

Good luck, sorry I'm not much more of any help.

 

The14thGOD

Link to comment
Share on other sites

what do you mean checking multiple users in? like there is a "db.php' which stores all the usernames and passwords which is what that statements for. And i don't think its the variables that are the problem. The logics to it is that while its scanning thought that db.php looking for the matching set to your input, it will output an error each time it doesnt match. like if the details your entering are the 6th line then you will get 5 error messages etc. So i guess its the for loop causing the problems...but is there another way to do it? =S

Link to comment
Share on other sites

YES!!! OMG YES!! it actually works!! except for one thing.. lol if you enter a incorrect username it won't show anything..so i tried

<?php
session_start(); 
$user  =  $_POST['username']; 
$pass  =  $_POST['password'];
$Admin = "Admin";
$test = "reallysecureadminpassword";

$details  =  file("db.php"); 

foreach($details  as  $key  =>  $Val) 
{ 
$data[$key]  =  explode("||",  $Val); 
} 
for($k  =  0;  $k  <  sizeof($details);  $k++) 
{ 
if($user == $data[$k][0]) {
        $u  =  $data[$k][0]; 
        $p  =  $data[$k][1];


if($user == $Admin AND $pass == $test) {
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 
			echo "welcome admin";
				include("news.php");
				break;	
	} else {	

if($user == $u) {
if($pass == $p) { 
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 

				include("news.php");
				break;
                }
 }

}
if($pass != $p) {
echo "An error occured, please re-enter your details.";
break;
}
} elseif($user != $data[$k][0]) {
echo "wrong username";
}
}
?>

but then its showing again when you enter the correct details..any ideas? =D

Link to comment
Share on other sites

hmm, well the reason is because at some point that will be correct.

 

there are 2 ways i can think of, at the end of the if $user = $data[$k][0] put a exit(0); this well stop it, and nothing after it will be executed, or, this is the way i would do it.

 

change it so that if the user is valid send them to a different page with the info that they need on it using the:

header("Location page.php");

exit(0);

else (

error = "Please re-enter your information";

header("Location login.php?error=$error

 

then the login page can use the $_GET[error], though that sounds like a lot of work hah

 

Up to you, if you need me to explain further let me know.

Link to comment
Share on other sites

hmm is this right?

 

<?php
session_start(); 
$user  =  $_POST['username']; 
$pass  =  $_POST['password'];
$Admin = "Admin";
$test = "reallysecureadminpassword";

$details  =  file("db.php"); 

foreach($details  as  $key  =>  $Val) 
{ 
$data[$key]  =  explode("||",  $Val); 
} 
for($k  =  0;  $k  <  sizeof($details);  $k++) 
{ 
if($user == $data[$k][0]) {
        $u  =  $data[$k][0]; 
        $p  =  $data[$k][1];


if($user == $Admin AND $pass == $test) {
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 
			echo "welcome admin";
				include("news.php");
				break;	
	} else {	

		if($user == $u) {
		if($pass == $p) { 
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 

				include("news.php");
				break;
                }
 }

}
		if($pass != $p) {
		echo "An error occured, please re-enter your details.";
		break;
		}
exit(0);
} else {
echo "wrong username";
}
}
?>

 

cause that doesnt want to work

Link to comment
Share on other sites

put it here:

<?php
session_start(); 
$user  =  $_POST['username']; 
$pass  =  $_POST['password'];
$Admin = "Admin";
$test = "reallysecureadminpassword";

$details  =  file("db.php"); 

foreach($details  as  $key  =>  $Val) 
{ 
$data[$key]  =  explode("||",  $Val); 
} 
for($k  =  0;  $k  <  sizeof($details);  $k++) 
{ 
if($user == $data[$k][0]) {
        $u  =  $data[$k][0]; 
        $p  =  $data[$k][1];


if($user == $Admin AND $pass == $test) {
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 
			echo "welcome admin";
				include("news.php");
				break;	
	} else {	

		if($user == $u) {
		if($pass == $p) { 
				$_SESSION['user'] = $user; 
				$_SESSION['logged'] = "yes"; 
				$_SESSION['test'] = "1"; 

				include("news.php");
				break;
                }
//Me here
exit(0);
 }

}
		if($pass != $p) {
		echo "An error occured, please re-enter your details.";
		break;
		}
} else {
echo "wrong username";
}
}
?>

Link to comment
Share on other sites

hmm..alright try this

<?php
session_start(); 
$user  =  $_POST['username']; 
$pass  =  $_POST['password'];
$Admin = "Admin";
$test = "reallysecureadminpassword";
$bad_user = true;

$details  =  file("db.php"); 

foreach($details  as  $key  =>  $Val) { 
$data[$key]  =  explode("||",  $Val); 
} 
for($k  =  0;  $k  <  sizeof($details);  $k++) { 
if($user == $data[$k][0]) {
		$u  =  $data[$k][0]; 
		$p  =  $data[$k][1];
}
}
if($user == $Admin and $pass == $test) {
$_SESSION['user'] = $user; 
$_SESSION['logged'] = "yes"; 
$_SESSION['test'] = "1"; 
$bad_user = false;
echo "welcome admin";
include("news.php");
break;	
}
else {	
if($user == $u) {
	if($pass == $p) { 
			$_SESSION['user'] = $user; 
			$_SESSION['logged'] = "yes"; 
			$_SESSION['test'] = "1"; 
$bad_user = false;
			include("news.php");
			break;
         }
 }

}
if($pass != $p and $bad_user == true) {
echo "An error occured, please re-enter your details.";
break;
}
else {
echo "wrong username";
}
?>

 

This is how this works,

first of all we set the $bad_user to true, if the username matches an entry, then we set the variables u and p, if it matches both username and password we set the value to true, so that the only way to get false is if the username or password dont match up.

 

also, if you dont mind, try to keep your code similar to the way it is formated here (i had to change something so the $bad_user=true is a little off. it's just easier to troubleshoot cuase you can see the end of the if/for etc statements, and is cleaner to read to

 

*****EDIT*****

added a second "=" to the last statement since it was setting the value to true =P

Link to comment
Share on other sites

ok, had to make a few changes there but it works perfectly now ^^

 

<?php
session_start(); 
$user  =  $_POST['username']; 
$pass  =  $_POST['password'];
$Admin = "Admin";
$test = "reallysecureadminpassword";
$bad_user = true;

$details  =  file("db.php"); 

foreach($details  as  $key  =>  $Val) { 
$data[$key]  =  explode("||",  $Val); 
} 
for($k  =  0;  $k  <  sizeof($details);  $k++) { 
if($user == $data[$k][0]) {
		$u  =  $data[$k][0]; 
		$p  =  $data[$k][1];
}
}
if($user == $Admin and $pass == $test) {
$_SESSION['user'] = $user; 
$_SESSION['logged'] = "yes"; 
$_SESSION['test'] = "1"; 
$bad_user = false;
echo "welcome admin";
include("news.php");
}
else {	
if($user == $u) {
	if($pass == $p) { 
			$_SESSION['user'] = $user; 
			$_SESSION['logged'] = "yes"; 
			$_SESSION['test'] = "1"; 
$bad_user = false;
			include("news.php");
         }
 }

}
if($pass != $p || $pass != $test and $bad_user == true) {
echo "An error occured, please re-enter your details.";
}
?>

Had to remove all the break; 's as i was getting fatal errors from them lol.

And i had to add || $pass != $test to that last if statement so that i wouldn't get the error message when i logged in as the admin.

 

Just going to test it a bit more but that works so far.

Thankyou soo much for the help The14thGOD =) much appreciated

 

oh and sorry about the formatting.

 

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.