vendredi 27 juin 2014

Failed to find username/password in MySQL Database


Vote count:

0




I have just started out with PHP, and am finding it an odd programming language so far. I've 'borrowed' code from this PHP login script tutorial, and the code worked find before I modified it.


The specific problem is that it doesn't seem to be finding the username & password that exist in the database. I have verified that I am in fact inputting the correct username and password.


I'm new to PHP, so what exactly am I doing wrong? To the best of my (current) PHP knowlege, there are no syntax errors, nor are there basic programming errors such as leaving a quote.


Code (no, I don't mind that my database details are here, I plan to change them once this problem has been fixed):



<?php

$host="localhost"; // Host name
$username="crysisor_tstuser"; // Mysql username
$password="Password1234"; // Mysql password
$db_name="crysisor_test"; // Database name
$tbl_name="members"; // 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['session']="mypassword";
$_SESSION['session']="myusername";
$authorized = true;
$uname = $myusername;
header("Location: http://ift.tt/1llmeUl"); /* Redirect browser */
exit();
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>


HTML (if relevant):



<html xmlns="http://ift.tt/lH0Osb" xml:lang="en">
<html>
<head>
<title>Server Portal | Login</title>
<link href="./css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="navbar">
</div>
<div class="loginui">
<div class="loginfield">
<form id="login" name="form1" method="post" action="checklogin.php">
<input name="myusername" type="text" id="myusername" />
<input name="mypassword" type="text" id="mypassword" />
<input type="submit" name="Submit" value="" />
</form>
</div>
</div>
</body>
</html>


asked 18 secs ago






Aucun commentaire:

Enregistrer un commentaire