<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Create a log-in system for registered users with WhizBase

Published on
10,909 Points
4,809 Views
1 Endorsement
Last Modified:
Approved
Community Pick
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles about WhizBase).

In the last article "Create an AJAX supported registration form with WhizBase" I have explained how we can make a registration process, now when users register to your site, you need to make them log-in to secured pages.

The Log-in Page
I will create a log-in page which is a simple HTML code and save it as "login.ic", I will explain later why I did not save it as HTML.
<html>
<head></head>
<body>
         <form action="login.wbsp" method="post">
          Please enter your log-in information:<br />
          Username <input type="text" name="username" /><br />
          Password <input type="password" name="password" /><br />
          <input type="submit" value="Login" />
          </form>
</body>
</html>

Open in new window


I will not explain this code, it is a simple HTML code with to fields and a submit button.

The secured page
I will create a page where its data must not be seen by non-registered users, I will save it as "secured.ic", again I am saving it with IC extension, I will explain that later.

<html>
<head></head>
<body>
Hello $wbgets[username],<br />
you are logged in!
</body>
</html>

Open in new window



In this code we have just one new function, it is the $wbgets[] which is a function to get the session variable "username" where the username is stored.

What to show?
This is my main page where I will check if the visitor is logged in or not, if yes I will show him the secured page, if not I will show the log-in form. Here we will discuss sessions in WhizBase. Please use WhizBase 6, because sessions are not used in earlier versions.

[FormFields]
WB_UseSessions=T 
#* Use sessions in this page *#
<!--WB_BeginTemplate-->
$wbif["$wbgets[logged]"="1"| #* check if we have a session variable logged and have a value 1 *#
$wbrinc[secured.ic] #* if yes, show the secured page, the user is logged in *#
|
$wbrinc[login.ic] #* if not, show the log-in form, the user is not logged in *#
]

Open in new window


As you can see from comments in the code I have turned the sessions on by putting WB_UseSessions=T in the Form-Fields section, so WhizBase will start processing session data. Sessions are not included in WhizBase by default, so you need to turn it on in every page you use them in.

Next, I am testing if there is a session variable "logged" with value "1" by an if statement, this variable will be set in the file "login.wbsp", I will explain that in the next section. Now just consider that we have a session variable "logged" with value "1" if we are logged in and nothing if we are not.

Now why I have saved two files with "IC" extension, it is because in WhizBase I can tell the server not to interpret files with "IC" extension if it is called directly, these files can only be called with-in a WhizBase file by $wbrinc or $wbinc. This process is called including files. In this case I am using $wbrinc because I do not want to render the file until it is needed. In my case I have two files "secure.ic and login.ic", so I will render one of them only. $wbinc would include and render both files and that takes more resources. You can check the differences between these two functions in the help file of WhizBase.

If yes, which is if the session variable "logged" have a value "1" the user is logged in, so I will show him the content of "secured.ic", if not I will show him the content of "login.ic".

Finally save this file as "default.wbsp", this is just a one file example, if you have more than one page you rather use a DB than "IC" files.

Log-in process
In this file I will check if the username and the password the visitor provided do match with the data in the DB. I will use the DB I have created in my last article "Create an AJAX supported registration form with WhizBase".

[FormFields]
WB_UseSessions=T 
#* We need sessions *#
WB_command=Q 
#* Query the DB *#
WB_BaseName=reg.mdb 
#* connect to red.mdb DB file*#
WB_RcdSet=profile 
#* use profile table for our query*#
WB_Query=username='$wbv{username}' and password='$wbv{password}' 
#* get all records which have this username and this password in the same time *#
WB_MaxRec=1 
#* one record is enough *#
[MsgAndLbl]
WBM_NoMatch=$empty$
<!--WB_BeginTemplate-->
<html>
<head></head>
<body>
$wbif[$wbp[RC]=1| #* if we have match we will have one record *#
   $wbsets[logged|1|f] #* set logged as 1 *#
   $wbsets[username|$wbv[username]|f] #* set username with the username provided *#
   You have been successfully logged in, please click <a href="default.wbsp">here</a> to return to your page.
|
   Your username and password does not match, please go back and try again!
]
</body></html>

Open in new window


In this file I will use sessions, I need to set the session variable "logged" as "1" if the user provides true data. Here I am querying the DB looking for a match, I need just one record with the provided username and password. One record is enough because there is no two users with the same username.

I am using $wbp[RC] to return number of records returned by the query, I can get one record or no records, If there is one record then it is a match and the session variable "logged" is set to "1", I will also set the session variable "username" with the username provided.

In case I do not have a match, simply give an error message.

For more information email me at: NurAzije [at] Gmail [dot] com Or visit the WhizBase official site at www.whizbase.com

NurAzije is a PHP and WhizBase programmer, who at the time of article publication is working in partnership with WhizBase on several projects.
1
Comment
Author:NurAzije
0 Comments

Featured Post

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month