2015-09-14 92 views
1

我試圖用Bluemix中的DashDB服務創建一個使用PHP登錄表單,我真的不知道這個代碼有什麼問題,我真的很感謝你的幫助!我的代碼是由五個部分組成:ConexionDB.php,checklogin.php,login_success.php,logout.php和main_login.php使用DashDB登錄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> 

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> 

註銷

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

這如果您提供有關錯誤的更多詳細信息,將會有所幫助。 – wwkudu

+0

你的問題還不夠,如果有任何錯誤或者哪一部分代碼沒有按照你的理解工作,請添加你的錯誤。 –

+0

表格「MEMBERS」的模式名稱是什麼?如果可能,您應該在代碼中使用SCHEMA.TABLENAME。 –

回答

0

請描述你的errorYou可能檢索使用

CF日誌你的日誌[您的應用程序名稱](添加--recent來獲取最後的轉儲)

+0

感謝您的回答,問題是當我點擊登錄時,我被重定向到checklogin.php頁面,只顯示一個空白頁面,所以我認爲checklogin.pho代碼沒有正確檢查數據庫,它只是沒有做任何事情。 模式名稱將是DASH103758.MEMBERS,我在代碼中以thay的方式測試它,但是我得到了相同的結果。 –

+0

如果你得到一個空白頁面,很可能你的代碼出現錯誤。如上所述檢查應用程序日誌 –

1

您需要使用包含DB2客戶端的備用的Cloud Foundry構建包。

如果您還沒有完成,請下載Cloud Foundry命令行工具以及您的PHP應用程序啓動代碼(您可以從Bluemix UI中應用程序窗口右側的「開始代碼」鏈接下載這兩個代碼)。

運行「cf登錄」。

運行以下命令DB2與PHP buildpack把你的應用程序:應用程序再次重新啓動測試

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

後。

你可以在這裏找到更多的細節:

http://bit.ly/1UROtMO

1

有在你的代碼中的一些問題,並建立:)

1)改變

$ tbl_name = 「成員」 ;

$ tbl_name =「架構。會員「;

其中 '圖式' 是 'DASH103758' 你的情況(由data_henrik的建議)


2)你必須使用自定義推buildpack使您要使用db2client (由adasilva所建議的)的CF命令行是:

CF登錄-u yourusername -o yourorganization -s yourspace

CF推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其中username = '$名爲myusername' 和 密碼= '$輸入mypassword'「;

和校驗這樣的:

// DB2_num_row計數錶行
// $計數= db2_num_rows($結果);

//如果結果匹配$名爲myUsername和$輸入mypassword,表中的行必須在1個 行

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

如果($計數== 1){
.... ...


我希望它是有用的


問候