I googled a short tutorial to connect a database and then I wanted to compare the username and password to then can gain access. It is kind of novice coding, but at least I can practice and hopefully pick up programming more.
check.php
<?php
//Need session//
$username = 'root';
$password = 'root';
$host = 'localhost';
$db_name = 'pcart';
$connection = mysql_connect($host, $username, $password) or die('cannot be connected');
mysql_select_db($db_name, $connection) or die ('Could not select database');
$user=$_POST['username'];
$pass=$_POST['password'];
$query = "Select username, password from tbladmins";
$user_result = mysql_query($query);
echo $user_result;
if (mysql_num_rows($user_result)==0)
{
echo "no rows to print";
}
while ($row=mysql_fetch_array($user_result, MYSQL_ASSOC))
{
$checkuser=$row["username"];
$checkpass=$row["password"];
}
mysql_free_result($user_result);
if !($user == $checkuser and $pass == $checkpass)
{
echo'where are you';
$results= mysql_query("Select * from tbladmins;") or die('error connecting to mysql');
if(mysql_num_rows($results)==0)
{
echo "no rows found, nothing to print so i am exiting";
}
print "test successful"
mysql_free_result($results);
}
else
{
echo "Wrong user or password! If you forgot, email Josephine for the username
and password";
}
?>
index.php
<html>
<head>
<title>Partner Portal
</title>
</head>
<body id="partners-page">
<div id="main">
<form method="post" action="Check.php">
<input type="text" name="username" value=""/>
<input type="password" name="password" value=""/>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>
<a href="#">forgotten password</a>
</div>
<!--#main-->
</div><!--div partner-page-->
</html>
EDIT
The problem is that it won’t show anything when I am testing if username and password match the one in database… So did I overlook anything? Why was the page blank when I tested inputting username and password?
Nice Josephine, that’s a great start! Now I know you have heard of the saying, there are a million ways to skin a cat, and this is no exception. There are really a lot of ways to accomplish the above but as of late, this method of doing it have become a bit, how should I say it, un-secure.
Database connection and user management and implementing it all in a secure way is now contained in every PHP framework you can find. This is all done for you so basically what you have to do is fill in the configuration file and you are good to go.
Saying that, it’s great to learn how it all ties together and you are on the right track. 😀
A framework is there to re-use code that you use in every application, and being a software engineer, we re-use a lot, so you do not want to go and duplicate it all over the place.
Another thing is that the code like this, stays hidden so for instance, I, can’t muck it up and I know it’s tested properly.
Keep up the good work 😀 It gets a lot more interesting the deeper you go down the rabbit hole.