Jump to content

Login Problem


tecmeister

Recommended Posts

I have created a register/login page on my web site.

But i having a problem to login.

It is saying:

 

Wrong Username or Password

 

Im using the password that it is giving me when i register.

This is the script that i have used:

 


<?php
session_start();
$_SESSION['logged_in']='yes';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>NuRevolution - Check Login</title>
</head>

<body>



<?php

//Database Information

$dbhost = "localhost";
$dbname = "web88-**********";
$dbuser = "web88-**********";
$dbpass = "**********";

// Connect to server and select databse.
mysql_connect("$dbhost", "$dbname", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM members WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==4){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}



ob_end_flush();
?>


</body>
</html>

Link to comment
Share on other sites

Well, $username and $password are undefined. This line:

 

$sql="SELECT * FROM members WHERE username='$username' and password='$password'";

 

Should be:

 

$sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'";

 

Are there really supposed to be 4 rows returned with the same username and password? Surely there should just be one matching row? In which case this:

 

if($count==4){

 

Should be:

 

if($count==1){

Link to comment
Share on other sites

Sorry, thats partly my fault. You should be using $myusername and $mypassword, since those are the variables you defined. Try:

 

$sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql) or die(mysql_error());
echo '<br />Query was:<br />'.$sql;

 

If those variables are still empty, then there's a problem with your form. Check that the names of the fields are "username" and "password" respectively, and that the method is set to POST. Of course, keeping the names consistant rather than switching in between would make things easier.

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.