Jump to content

My Login wont work


Ungerbla

Recommended Posts

I see that.

change

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

$count = mysql_num_rows ($result);

if($count ==1) {
session_start();
$_SESSION['myusername'] = $_POST['myusername'];
$_SESSION['logged_in'] = true;
header("location:login_success.php");
}
else {
$message = "Wrong Username or Password";
}
}

to

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername'";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
$data = mysql_fetch_assoc($result);
if ($data['password'] == $mypassword) {
	session_start();
	$_SESSION['myusername'] = $_POST['myusername'];
	$_SESSION['logged_in'] = true;
	header("Location: login_success.php");
} else {
	$message = 'Username/Password combination incorrect';
}
} else {
$message = 'Username/Password combination incorrect';
}

 

You may also want to look into hashing your passwords too.

Look at md5 in the php manual.

 

i did that.. and i got this error:

 

Parse error: parse error in C:\wamp\www\new 2.php on line 52

Can you show the whole page code again please.

 

sure!

 

<?php
$message = '';
if (isset($_POST['submit'])) {
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test";
$tbl_name = "members";

mysql_connect ($host, $username, $password) or die ("the server made a boo boo");
mysql_select_db ($db_name) or die (mysql_error () );

$myusername = $_POST ['myusername'];
$mypassword = $_POST ['mypassword'];

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername'";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
   $data = mysql_fetch_assoc($result);
   if ($data['password'] == $mypassword) {
      session_start();
      $_SESSION['myusername'] = $_POST['myusername'];
      $_SESSION['logged_in'] = true;
      header("Location: login_success.php");
   } else {
      $message = 'Username/Password combination incorrect';
   }
} else {
   $message = 'Username/Password combination incorrect';
}
?>

<html>
<head>
<titel>Main Login Page</titel>
<style type="text/css"> </style>
</head>
<body>

<div id="loginform">
<form method ="post" action="login_success.php"
<label for="username">Username:</label>
<input type"text" name="myusername" id="username" />
<label for="password">Password:</label>
<input type="password" name="mypassword" id="password" />
<input type="submit" name="submit" value="Login" />
<br/>
<?php echo $message; ?>
</form>
</div>
</body>
</html>

Ive added a few more little checks in there for you aswell..

 

<?php
$message = '';
if (isset($_POST['submit'])) {
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test";
$tbl_name = "members";

mysql_connect ($host, $username, $password) or die ("the server made a boo boo");
mysql_select_db ($db_name) or die (mysql_error () );

$myusername = (isset($_POST['myusername']) ? $_POST['myusername'] : '');
$mypassword = (isset($_POST['mypassword']) ? $_POST['mypassword'] : '');

if (empty($myusername) || empty($mypassword)) {
	$message = 'Username and Password cannot be empty';
} else {
	$sql = "SELECT * FROM $tbl_name WHERE username='$myusername'";
	$result = mysql_query($sql);
	if (mysql_num_rows($result) > 0) {
	   $data = mysql_fetch_assoc($result);
	   if ($data['password'] == $mypassword) {
	      session_start();
	      $_SESSION['myusername'] = $_POST['myusername'];
	      $_SESSION['logged_in'] = true;
	      header("Location: login_success.php");
	   } else {
	      $message = 'Username/Password combination incorrect';
	   }
	} else {
	   $message = 'Username/Password combination incorrect';
	}
}
}
?>

<html>
<head>
<titel>Main Login Page</titel>
<style type="text/css"> </style>
</head>
<body>

<div id="loginform">
<form method ="post" action="login_success.php"
<label for="username">Username:</label>
<input type"text" name="myusername" id="username" />
<label for="password">Password:</label>
<input type="password" name="mypassword" id="password" />
<input type="submit" name="submit" value="Login" />
<br/>
<?php echo $message; ?>
</form>
</div>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.