/    Sign up×
Community /Pin to ProfileBookmark

Problem with MySQL Database linked PHP login form

Hey people, Im new to PHP and MySQL and have bumped into a little problem trying to get my username and password verified. Most of the script is functional for example not granting access if the both/one of the login fields are empty.

But, it actually seems to allow the user to login if they have entered text into both the username and password fields regardless of whether these (username/password) are stored in the corresponding login database.

Here is the main script:

[code=php]
<html>
<body>

<form action=”index3.php?login=yes” method=”POST”>
Username:<input type=”text” name=”user”><br />
Password:<input type=”password” name=”pass”><br/>
<input type=”submit” name=”login” value=”login” ><p>
</form>

<?php

$user=$_POST[‘user’];
$pass=$_POST[‘pass’];
$login=$_POST[‘login’];

function denied()
{
echo ‘<h3><span style= “color:red”> Access Denied!!! </span></h3><br><br>’;
}

function granted ($user) //function with username parameter retrieved
{
echo ‘<h3><span style= “color:green”> Access Granted!!! </span></h3>’;
echo ‘Welcome, ‘ . $user;
}

if($login==’login’)
{
$con= include_once “mysql_connect.php”;

$query = “SELECT id FROM login WHERE user=’$user’ AND pass=’$pass’ “;
echo $query;
$result = mysql_query($query) or die (“ERROR IN SQL STATEMENT: “.mysql_error());
$row = mysql_fetch_assoc($result);

if (empty($user) || empty($pass))
{
denied();
die(“<br>Please fill out user login fields carefully….<br>”);
}

if ($result!=1)
{
granted($user);
}

else
{
denied ();
}
}

?>

</body>
</html>
[/code]

And the following is external scripting associated with the the above:

[code=php]
<?php

$db_host = “localhost”;
$db_username = “rossbryan”;
$db_pass = “security”;
$db_name = “login”;

@mysql_connect(“$db_host”, “$db_username”, “$db_pass”) or die (“Could not connect to MySQL”);
@mysql_select_db(“$db_name”) or die (“No $db_name Database “);

?>
[/code]

Any ideas of what the problem is and how i could possibly resolve this issue?

For some reason i believe it could be something to do with the password not being verified correctly once retrieved from the database, maybe in the $return variable but im not entirely sure, just a guess. Even if it is that i wouldn’t know another way of going about fixing it.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 20.2011 — $result will be a mysql query resource, assuming the SQL is valid. It tells you nothing about whether the query actually returned any results: it will still return a resource even if there are zero result rows. For what you want, you could use mysql_num_rows() to determine if anything was matched.
×

Success!

Help @RossBryan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...