954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Check if user exists in db?

Hi,

could someone tell me what the correct syntax is to check is a user exists in my database.

I got a login page and when user enters username and password i need it to check if they exist obviously in database and if so re-direct them to the page i specify else tell them login details don't exist.

Anyone be able to help me please?

thank you,
genieuk

genieuk
Junior Poster
150 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,

could someone tell me what the correct syntax is to check is a user exists in my database.

I got a login page and when user enters username and password i need it to check if they exist obviously in database and if so re-direct them to the page i specify else tell them login details don't exist.

Anyone be able to help me please?

thank you, genieuk

So, in essence, you want a login script?

$config_basedir = "http://www.yoursite.com";
$user = mysql_real-escape_string($_POST['email']);
$password = mysql_real-escape_string($_POST['password']);

$loginsql = "SELECT * FROM main WHERE email = '$user' AND password = '$password'";
$loginres = mysql_query($loginsql);
$numrows = mysql_num_rows($loginres);
		
if($numrows == 1){				
$loginrow = mysql_fetch_assoc($loginres);
								
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_NAME'] = $loginrow['username'];

//If login is correct					
header("Location: " . $config_basedir . "/index.php");
}
else {

//if login is incorrect
header("Location: " . $config_basedir . "/login.php?error=2");
}


Then you can put this where to show the errors

<?php

switch($_GET['error']) {
		
case "1":
echo "<font color='#FF0000'>You must be logged in.</font>";
break;
			
case "2":
echo "<font color='#FF0000'><strong>Incorrect username/password</strong></font>";
break;				
}
?>
timmah
Newbie Poster
5 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

Thank you very much, i want to do it myself thou as i am learning, best way to learn and get wirting scripts is doing it myself. But thanks ever so much for your kindness.

I should have been more clear, the point of all this is for me to learn and learn from my mistakes.

Here is my code, i am not sure what i am doing wrong. To my knowledge 1 equals true and 0 equals false.

Please take a look and let me know what is wrong, i am doing it very basic then i will be adding cookies etc in future i am only doing it to learn then when i learn more i will eventually re-write the scripts to be less code and more efficent, bt as a newbie i am obviously on a big rollercoaster learning. It connects to DB etc but dont search for the info to see if it exists.

<?php include("db_connect.php"); // Database Connection ?>

<?php

if (isset($_POST['submitted'])) { // Handle the form.

$username = htmlspecialchars( stripslashes( strip_tags($_POST['username'] ) ) );
$password = htmlspecialchars( trim($_POST['password'] ) );
$url = "http://wwww.bbbb.com/cp.php";

if ( empty ($_POST['username'] ) ) { // Check if username field is empty. If it is tell them
	echo "<font color=\"#FF3F00\" face=\"Verdana, Geneva, sans-serif\"><p>Please Enter your Username in order to login.</p></font>";
}

if ( empty ($_POST['password'] ) ) { // Check if password field is empty. If it is tell them
	echo "<font color=\"#FF3F00\" face=\"Verdana, Geneva, sans-serif\"><p>Please Enter the Password associated with your account.</p></font>";
}

	
$query = mysql_query("SELECT * FROM userinformation WHERE username = '$username' AND 'password' = '$password'");
if( mysql_num_rows( $query ) == 1 ) {
    echo "<font color=\"#FF3F00\" face=\"Verdana, Geneva, sans-serif\"><p>Login Successful</p></font>"; 
	header("location: $url");
}

    else if (mysql_num_rows($query) == 0 ){
		
		echo "Login Details Incorrect, Please try again";
}
}

?>
<h1><font color="#007FAA" face="Verdana, Geneva, sans-serif">Login</font></h1>
<form action="login.php" method="post">
<center><p>Username:</p> <input type="text" name="username" size="20" maxlength="20" value="<?php if (isset($_POST["username"])) { echo htmlspecialchars($_POST["username"]); } ?>" />
<p>Password:</p> <input type="password" name="password" size="20" maxlength="20" />
<p> <input type="hidden" name="submitted" value="TRUE" /></p>
<p><input type="submit" value="Login" /></p></center>
</form>


thank you
genieuk

genieuk
Junior Poster
150 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You