I'm Trying to create a Login form using PHP with the DashDB service from Bluemix, I really dont know whats wrong with this code and I would really appreciate your help! My code is composed of five parts: ConexionDB.php, checklogin.php, login_success.php, logout.php and main_login.php
DashDB Database:
CREATE TABLE MEMBERS (
ID INT NOT NULL,
USERNAME VARCHAR(65) NOT NULL DEFAULT,
PASSWORD VARCHAR(65) NOT NULL DEFAULT,
PRIMARY KEY (ID)
);
ConexionDB.php
<?php
// Parse VCAP
if( getenv("VCAP_SERVICES") ) {
$json = getenv("VCAP_SERVICES");
}
// No DB credentials
else {
throw new Exception("No Database Information Available.", 1);
}
// Decode JSON and gather DB Info
$services_json = json_decode($json,true);
$bludb_config = $services_json["dashDB"][0]["credentials"];
// Create DB connect string
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$bludb_config["db"].
";HOSTNAME=".
$bludb_config["host"].
";PORT=".
$bludb_config["port"].
";PROTOCOL=TCPIP;UID=".
$bludb_config["username"].
";PWD=".
$bludb_config["password"].
";";
?>
main_login.php
<!DOCTYPE HTML>
<html>
<head>
<title>Application Name</title>
</head>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
checklogin.php
<?php
//Include DB conexion
require('includes/ConexionDB.php');
$tbl_name="MEMBERS"; // Table name
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Connect to BLUDB
$conn = db2_connect($conn_string, '', '');
if ($conn) {
// Prepare, execute SQL and iterate through resultset
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);
//$result=db2_query($sql);
?>
<?php
// DB2_num_row is counting table row
$count=db2_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:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<?php
db2_close($conn);
}
?>
login_success
// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
logout
// Put this code in first line of web page.
<?php
session_start();
session_destroy();
?>