Monday, July 13, 2026

.::: Sample Access Database MariaDB via Frontend & Backend using Lucee with accecc login Application.cfc, users save in table users :::

 

 
correlation https://teguhth.blogspot.com/2026/07/sample-access-database-mariadb-via.html

1. create user for login

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(100) NOT NULL,
    nama VARCHAR(100),
    aktif CHAR(1) DEFAULT 'Y'
);

INSERT INTO users(username,password,nama,aktif)
VALUES
('admin','123456','Administrator','Y');

INSERT INTO users(username,password,nama,aktif)
VALUES
('aisyah','123456','Administrator','Y');

INSERT INTO users(username,password,nama,aktif)
VALUES
('teguh','123456','readonly','Y');

 


2. Struktur file
 
3. Buat Application.cfc

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat Application.cfc
<cfcomponent output="false">

    <cfset this.name = "PembelianApp">
    <cfset this.sessionManagement = true>
    <cfset this.sessionTimeout = CreateTimeSpan(0,8,0,0)>

    <cffunction name="onRequestStart"
                output="false"
                returntype="boolean">

        <cfargument name="targetPage">

        <cfset page = ListLast(arguments.targetPage,"/")>

        <!-- halaman tanpa login -->
        <cfif ListFindNoCase("login.cfm",page)>
            <cfreturn true>
        </cfif>

        <cfif NOT StructKeyExists(session,"login")>

            <cflocation
                url="login.cfm"
                addtoken="false">

        </cfif>

        <cfreturn true>

    </cffunction>

</cfcomponent>
[root@teguhth-all ROOT]#

4. Buat login.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat login.cfm
<cfsetting showdebugoutput="false">

<cfif StructKeyExists(form,"username")>

    <cfquery
        datasource="dbteguhth"
        name="qUser">

        SELECT
            username,
            password,
            nama
        FROM users
        WHERE username=
        <cfqueryparam
            value="#form.username#"
            cfsqltype="cf_sql_varchar">

        AND aktif='Y'

    </cfquery>

    <cfif qUser.recordcount EQ 1>

        <cfif qUser.password EQ form.password>

            <cfset session.login=true>
            <cfset session.username=qUser.username>
            <cfset session.nama=qUser.nama>

            <cflocation
                url="pembelian_front.cfm"
                addtoken="false">

        <cfelse>

            <cfset pesan="Password Salah">

        </cfif>

    <cfelse>

        <cfset pesan="Username Tidak Ditemukan">

    </cfif>

</cfif>

<!DOCTYPE html>

<html>

<head>

<title>Login</title>

<style>

body{
font-family:Arial;
background:#f5f5f5;
}

.login{
width:350px;
margin:100px auto;
padding:20px;
background:white;
border:1px solid #ddd;
}

input{
width:100%;
padding:8px;
margin-bottom:10px;
}

button{
width:100%;
padding:10px;
}

</style>

</head>

<body>

<div class="login">

<h2>Login</h2>

<cfif IsDefined("pesan")>

<font color="red">

<cfoutput>#pesan#</cfoutput>

</font>

<br><br>

</cfif>

<form method="post">

Username

<input
type="text"
name="username"
required>

Password

<input
type="password"
name="password"
required>

<button
type="submit">

Login

</button>

</form>

</div>

</body>

</html>
[root@teguhth-all ROOT]#

 


5. Buat logout.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat logout.cfm
<cfset StructClear(session)>

<cflocation
url="login.cfm"
addtoken="false">
[root@teguhth-all ROOT]#


6. Ubah pembelian_front.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat pembelian_front.cfm
<cfquery
name="qPembelian"
datasource="dbteguhth">

SELECT *

FROM pembelian

</cfquery>

<html>

<head>

<title>Data Pembelian</title>

</head>

<body>

Selamat Datang

<b>
<cfoutput>#session.username#</cfoutput>
<cfoutput>#session.nama#</cfoutput>

</b>

|

<a href="logout.cfm">

Logout

</a>

<hr>

<table border="1">

<tr>

<th>Kode</th>
<th>Barang</th>
<th>Customer</th>
<th>Tanggal</th>
<th>Jumlah</th>

</tr>

<cfoutput query="qPembelian">

<tr>

<td>#KODE_PEMBELIAN#</td>
<td>#KODE_BARANG#</td>
<td>#KODE_CUSTOMER#</td>
<td>#dateFormat(TANGGAL_PEMBELIAN,"dd-mm-yyyy")#</td>
<td>#JUMLAH_PEMBELIAN#</td>

</tr>

</cfoutput>

</table>

</body>

</html>
[root@teguhth-all ROOT]#


7. pembelian_api.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat pembelian_api.cfm
<cfif NOT StructKeyExists(session,"login")>

    <cfheader
        statuscode="401"
        statustext="Unauthorized">

    <cfoutput>
    {"status":"401","message":"Unauthorized"}
    </cfoutput>

    <cfabort>

</cfif>

<cfquery
name="qPembelian"
datasource="dbteguhth">

SELECT
KODE_PEMBELIAN,
KODE_BARANG,
KODE_CUSTOMER,
TANGGAL_PEMBELIAN,
JUMLAH_PEMBELIAN
FROM pembelian

</cfquery>

<cfcontent type="application/json">

<cfoutput>
#serializeJSON(qPembelian)#
</cfoutput>
[root@teguhth-all ROOT]#

8. test login 

http://10.10.10.90:8888/pembelian_front.cfm
http://10.10.10.90:8888/pembelian_api.cfm

http://10.10.10.90:8888/pembelian_front.cfm
http://10.10.10.90:8888/pembelian_api.cfm
 


 

No comments:

Post a Comment

Popular Posts