1

我正在尝试使用 PHP 和 Bluemix 的 DashDB 服务创建一个登录表单,我真的不知道这段代码有什么问题,非常感谢您的帮助!我的代码由五个部分组成:ConexionDB.php、checklogin.php、login_success.php、logout.php 和 main_login.php

DashDB 数据库:

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>

检查登录.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);
}
?>

登录成功

// 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>

登出

// Put this code in first line of web page.
<?php
session_start();
session_destroy();
?>
4

3 回答 3

1

您需要使用包含 db2 客户端的备用 Cloud Foundry 构建包。

如果您还没有完成,请下载 Cloud Foundry 命令行工具以及您的 PHP 应用程序启动代码(您可以从 Bluemix UI 中应用程序窗口右侧的“Start Coding”链接下载两者)。

运行“cf登录”。

运行以下命令以使用带有 db2 的 php buildpack 推送您的应用程序:

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

应用程序重新启动后再次测试它。

您可以在此处找到更多详细信息:

http://bit.ly/1UROtMO

于 2015-09-15T02:22:52.257 回答
1

您的代码和构建中有一些问题:)

1) 改变

$tbl_name="会员";

$tbl_name="SCHEMA.MEMBERS";

在您的情况下,“SCHEMA”是“DASH103758”(如data_henrik建议的那样)


2)您必须使用自定义构建包进行推送,因为您想使用 db2client(如adasilva建议的那样)。CF 命令行是:

cf 登录 -u 你的用户名 -o 你的组织 -s 你的空间

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


3) 函数 db2_num_rows(..);不接受布尔值。您传递的 $result 是一个布尔值。实际上,db2_execute 返回一个布尔值 ( http://php.net/manual/en/function.db2-execute.php )。

因此,我建议更改“ checklogin.php ”中的查询字符串:

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

和这样的检查:

// DB2_num_row 正在计算表行
数 //$count=db2_num_rows($result);

// 如果结果匹配 $myusername 和 $mypassword,表行必须是 1 行

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

如果($count==1){
.... ...


我希望它有用


问候

于 2015-09-15T10:13:02.900 回答
0

请描述您的错误您可以使用检索日志

cf logs [您的应用名称](添加 --recent 以检索最后一个转储)

于 2015-09-14T06:36:21.770 回答