PHP登录表单使用DashDB [英] PHP Login Form using DashDB

查看:157
本文介绍了PHP登录表单使用DashDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



DashDB数据库: strong>

  CREATE TABLE MEMBERS(
ID INT NOT NULL,
USERNAME VARCHAR(65)NOT NULL DEFAULT,
PASSWORD VARCHAR(65)NOT NULL DEFAULT,
PRIMARY KEY(ID)
);

ConexionDB.php


$ b $如果(getenv(VCAP_SERVICES)){
$ json = getenv(VCAP_SERVICES),则


//解析VCAP
);
}
//没有DB凭据
else {
throw new Exception(No Database Information Available,1);
}

//解码JSON并收集DB Info
$ services_json = json_decode($ json,true);
$ bludb_config = $ services_json [dashDB] [0] [credentials];

//创建DB连接字符串
$ 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>应用程序名称< / title>
< / head>
< body>

< table width =300border =0align =centercellpadding =0cellspacing =1bgcolor =#CCCCCC>
< tr>
< form name =form1method =postaction =checklogin.php>
< td>
< table width =100%border =0cellpadding =3cellspacing =1bgcolor =#FFFFFF>
< tr>
< td colspan =3>< strong>会员登录< / strong>< / td>
< / tr>
< tr>
< td width =78>用户名< / td>
< td width =6>:< / td>
< td width =294>< input name =myusernametype =textid =myusername>< / td>
< / tr>
< tr>
< td>密码< / td>
< td>:< / td>
< td>< input name =mypasswordtype =textid =mypassword>< / td>
< / tr>
< tr>
< td>& nbsp;< / td>
< td>& nbsp;< / td>
< td>< input type =submitname =Submitvalue =Login>< / td>
< / tr>
< / table>
< / td>
< / form>
< / tr>
< / table>

< / body>
< / html>

checklogin.php

 <?php 

//包含DB conexion
require('includes / ConexionDB.php');

$ tbl_name =MEMBERS; //表名

//从表单
$ myusername = $ _ POST ['myusername']发送的用户名和密码;
$ mypassword = $ _ POST ['mypassword'];

//连接到BLUDB
$ conn = db2_connect($ conn_string,'','');
if($ conn){

//准备,执行SQL并通过结果集迭代
$ sql =SELECT * FROM $ tbl_name WHERE username ='$ myusername'和密码='$ mypassword';

$ stmt = db2_prepare($ conn,$ sql);
$ result = db2_execute($ stmt);

// $ result = db2_query($ sql);

?>

<?php

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

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

如果($ count == 1){

//注册$ myusername,$ mypassword并重定向到文件login_success.php

session_register(myusername);
session_register(mypassword);
header(location:login_success.php);
}
else {
echo用户名或密码错误;
}

?>


<?php
db2_close($ conn);
}
?>

login_success

  //检查会话是否未注册,重定向回主页。 
//将此代码放在网页的第一行。
<?php
session_start();
if(!session_is_registered(myusername)){
header(location:main_login.php);
}
?>

< html>
< body>
登录成功
< / body>
< / html>

注销

  //将此代码放在网页的第一行。 
<?php
session_start();
session_destroy();
?>


解决方案

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



1)更改


$ tbl_name =MEMBERS;



$ tbl_name =SCHEMA.MEMBERS ;


其中'SCHEMA'在您的情况下为'DASH103758'(由 data_henrik 建议)





2)您必须使用自定义构建包,因为您要使用db2client(如 adasilva 所建议) 。 CF命令行是:


cf login -u yourusername -o yourorganization -s yourspace



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




3)函数db2_num_rows(.. );不接受布尔值。你正在传递一个布尔值的$ result。 db2_execute,infact返回一个布尔值( 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
row



$ count = -1;

if($ result){

  $ rowcount = db2_fetch_array($ stmt);

  $ count = $ rowcount [0];

}



if($ count == 1){

.... ...




我希望它可以是有用的





问候


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();
?>

解决方案

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

这篇关于PHP登录表单使用DashDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆