Jump to content

[SOLVED] I dont understand for the life of me! (log in script)


compsci

Recommended Posts

Hello all,

 

I am trying to validate a username and password provided by a user in my database and something weird keeps happening!  :o Firstly here is my script (i am new with php and i welcome corrections):

 

<?php
echo "php active ";
require_once("connect2database.php");
//retrieving data from a database
$query = sprintf("SELECT * FROM user");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

$email = $_POST['email'];
$password = $_POST['password'];
$login = $_POST['login'];

if($login && $email && $password){
echo " logging you in....";
for($i=0; $i < sizeof($row); $i++){
if($row[$i]!=null){
echo "row not null ".$i;
if($row['username']==$email and $row['password']==$password){
echo " ****Your Logged in****";
}
}
}
}
?>

 

Out put it this when i insert the username and password ina  simple form that has a login buttton:

 

php active logging you in....row not null 0row not null 1row not null 2

 

If i put in the username and password in the first row of the database, i am logged in succesfully, otherwise i get the above output. Will really appreciate any help and tips and corrections!

Link to comment
Share on other sites

Here's what I use. Try some of this with your code and see if you can get it to work.

<?php
if(isset($_POST["submit_login"])) {
$u = $_POST["username"];
$p = md5($_POST["password"] . $salt);
dbconnect();
$query = "SELECT * FROM users WHERE username='$u' AND password='$p'";
$result = mysql_query($query) OR DIE ("ERROR: login - " . mysql_error());
dbclose();
if(mysql_num_rows($result) > 0) {
	while($r = mysql_fetch_array($result)){
		$user = $r["username"];
		$pass = $r["password"];
		if ($u == $user && $p == $pass) {
			$_SESSION["User"] = $u;
			$_SESSION["Role"] = $r["role"];
			$loggedIn = TRUE;
			$loginError = FALSE;
		}
	}
} else {
	$loginError = TRUE;
}
}
?>

Link to comment
Share on other sites

Thanks charlieholder! Will definitly check that out! I solved one of the problems of my script not going through the whole table! Which is now this new script not much different though:

 

<?php
echo "php active ";
require_once("connect2database.php");
//retrieving data from a database
$query = sprintf("SELECT * FROM user");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);

$email = $_POST['email'];
$password = $_POST['password'];
$login = $_POST['login'];

if($login && $email && $password){
echo " logging you in....";
for($i=0; $i<9; $i++){
echo "row not null ".$i;//different here
if($row[$i]!=null){
if($row['username']==$email and $row['password']==$password){
echo " ****Your Logged in****";
}
}
}
}
?>

Link to comment
Share on other sites

so i'm assuming your longing is when they click submit try this.

<?php
echo "php active ";
require_once("connect2database.php");
//retrieving data from a database
$email = $_POST['email'];
$password = $_POST['password'];
$login = $_POST['login'];
if(isset($login)){
if($email!='' || $password!=''){
$query = sprintf("SELECT * FROM user WHERE email='$email' AND $password='$password'");
$row = mysql_fetch_array($query);
if($row>0){
echo " logging you in....";
}else{
echo "your record was not found";
}
}else{
echo "your email or password field is empty";
}}else{ echo "you got no right to be on this page";}
?>

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.