5 Replies - 2692 Views - Last Post: 19 July 2010 - 10:41 PM Rate Topic: -----

#1 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

PHP Simple Login Page

Posted 19 July 2010 - 09:56 PM

I'm having some trouble with a simple login system. This is what I have and it doesn't seem to be working (i'll post the error).

okay i've created my database called hcprintc_mm , i have a table called user with 3 fields: id (primary key & auto increment), username, and password.

I then inserted the values mm36203 as the user name and sch2010 as the password.

I have an index.html with the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="stylesheet" type="text/css" href="style.css" title="style" media="screen" />

<script type="text/javascript">
/* <![CDATA[ */
	$(document).ready(function(){
			$(".block").fadeIn(1000);				   
			$(".idea").fadeIn(1000);
			$('.idea').supersleight();
			$('#username').example('Username');	
			$('#password').example('Password');
	});
/* ]]> */
</script>

</head>

<body>
    <div id="wrap">
    	<div class="idea">    
    	<p>Mellow Mushroom - Schedule Login</p>
    </div>
        
<div class="block">
            <form action="checkLogin.php" method="post" name="form1">
            	<div class="left"></div>
            	<div class="right">
            	<div class="div-row">
            		<input type="text" id="myusername" name="myusername" onfocus="this.value='';" onblur="if (this.value=='') {this.value='Username';}" value="Username" />
            	</div>
            	<div class="div-row">
            		<input type="password" id="mypassword" name="mypassword" onfocus="this.value='';" onblur="if (this.value=='') {this.value='************';}" value="************" />
            	</div>
            	<div class="rm-row">
            		<input type="checkbox" value="c" name="rm" id="remember"/> <label for="remember">Remember me?</label>
            	</div>
            	<div class="send-row">
            		<button id="login" value="login" type="submit" name="login"></button>
            	</div>
            	</div>
            </form>
        </div>
    </div>
</body>

</html>





Then I have a checkLogin.php file:
<?php
ob_start();
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="hcprintc_mm"; // Database name 
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

// 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 $tbl_name WHERE username='$myusername' and password='$mypassword'";
$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==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:loginSuccess.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>





Then I have a loginSuccess.php file:
<?php
session_start();
if(!session_is_registered(myusername))	{
	header("loacation:index.html");
}
?>

<!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></title>
</head>

<body>
<h1>Login Successful!</h1>

<a href="logout">Log Out!</a>
</body>
</html>



This is the error I am getting when i try to login on the website: "Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mm36203'@'localhost' (using password: YES) in /home3/hcprintc/public_html/mm/checkLogin.php on line 10
cannot connect"

am I supposed to give php the direct path (like in the error) to my database?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Simple Login Page

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Simple Login Page

Posted 19 July 2010 - 10:06 PM

Your username or password for connecting to MySQL is wrong. Also, I'd strongly advise against putting the password in the database as plaintext. You should hash it first. SHA1 is a good choice.
Was This Post Helpful? 0
  • +
  • -

#3 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: PHP Simple Login Page

Posted 19 July 2010 - 10:22 PM

View PostValek, on 19 July 2010 - 10:06 PM, said:

Your username or password for connecting to MySQL is wrong. Also, I'd strongly advise against putting the password in the database as plaintext. You should hash it first. SHA1 is a good choice.



are you talking about lines 4 & 5 on my checkLogin.php file?
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Simple Login Page

Posted 19 July 2010 - 10:39 PM

I'm talking about you only get that error if you're unable to connect to MySQL with the credentials specified. I know you made them blank in the code post above to protect them, and that's fine. But the actual credentials you're using are incorrect. Also, when passing variables as arguments for a function, they don't have to be in quotes.
Was This Post Helpful? 1
  • +
  • -

#5 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: PHP Simple Login Page

Posted 19 July 2010 - 10:40 PM

I got it figured out. Thanks Valek.
Was This Post Helpful? 0
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Simple Login Page

Posted 19 July 2010 - 10:41 PM

You're welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1