PHP Login Form using DashDB

2019-08-10 21:27发布

问题:

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>&nbsp;</td>
<td>&nbsp;</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();
?>

回答1:

You need to use an alternate cloud foundry build pack that contains the db2 client.

If you have not done yet please download Cloud Foundry command line tool and also your PHP application starting code (you can download both from the "Start Coding" link on the right side of your application window in Bluemix UI).

Run "cf login".

Run the following command to push your application with a php buildpack with db2:

cf push <app-name> -b https://github.com/ibmdb/db2heroku-buildpack-php

After application is restarted test it again.

You can find more details here:

http://bit.ly/1UROtMO



回答2:

there are some problems in your code and build :)

1) change

$tbl_name="MEMBERS";

with

$tbl_name="SCHEMA.MEMBERS";

where 'SCHEMA' is 'DASH103758' in your case (as suggested by data_henrik )


2) you have to push using a custom buildpack cause you want to use db2client(as suggested by adasilva). The CF command line are:

cf login -u yourusername -o yourorganization -s yourspace

cf push YOUAPPNAME -b https://github.com/ibmdb/db2heroku-buildpack-php -p YOURAPP_LOCAL_PATH


3) the function db2_num_rows( .. ); does not accept a boolean value. You are passing $result that is a boolean. db2_execute, infact, return a boolean (http://php.net/manual/en/function.db2-execute.php).

So, I suggest change the query string in 'checklogin.php':

$sql="SELECT count(*) FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

and the check like this:

// DB2_num_row is counting table row
//$count=db2_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

$count = -1;
if ($result) {
  $rowcount = db2_fetch_array($stmt);
  $count = $rowcount[0];
}

if($count==1){
.... ...


I hope it can be useful


Regards



回答3:

Please describe your errorYou could retrieve your logs using

cf logs [your app name] (add --recent to retrieve dump of last ones)