Starting out with php, I have written a basic authentication script which prints out a list of database on a mysql server if a userid (supplied by user) exists in the user table of "test" database.
The problem is that this script outputs database list even if the userid does not exist in the database. I am not sure what's wrong with the script. pls look through the script and help me understand as to why the db list is being outputted even though the userid does not exist in the db. Here is the script:
<?php
if(isset($_POST['submitted']))
{
$userid=$_POST['userid'];
$userpassword=$_POST['userpassword'];
$link_id=mysql_connect("localhost","root","pass");
$result_db_list=mysql_list_dbs($link_id);
mysql_select_db("test",$link_id);
if(!($result_ptr=mysql_query("Select userid from user where Userid='$userid'",$link_id))) die ("Please enter correct userid");
while($test=mysql_fetch_row($result_db_list))
{
echo $test[0]."<br>";
}
}
else
{
?>
<!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>Authentication Script</title>
<style type="text/css" >
#header{
padding-top:0px;
margin:0px;
background-color:#CCCCCC;
}
.container{
width:950px;
margin:0 auto;
border:1px solid red;
}
.authbox {
padding-top:100px;
margin:0 auto;
}
#footer{
background-color:#666666;
color:white;
}
</style>
</head>
<body>
<div id="header">
<div class="container">
<form action="authentication script.php" method="post">
<div class="authbox">UserName: <input type="text" name="userid" /><br/>
Password: <input type="password" name="userpassword" /><br/>
<input type="hidden" name="submitted" value="true" />
<input type="submit" value="Submit" />
</div>
</form>
</div>
</div>
<div id="footer">
Copywright 2010 NT Technologies.
</div>
</body>
</html>
<?php
}
?>
Thanks rseni.
mysql_real_escape_string(us3.php.net/manual/en/function.mysql-real-escape-string.php). The script above is vulnerable to SQL injection. I realize it's a test script, perhaps, but it's best to get into the habit of using these methods.