Create a log-in system for registered users with WhizBase

AID: 2909
  • Status: Published

3310 points

  • ByNurAzije
  • TypeTutorial
  • Posted on2010-04-21 at 02:58:19
Awards
  • 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:

Select allOpen 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 *#
]
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen 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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen 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.
Asked On
2010-04-21 at 02:58:19ID2909
Tags

WhizBase

,

Username

,

Password

,

Security

,

Login

,

Registration

,

XHTML

Topic

Scripting Languages

Views
2235

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Scripting Languages Experts

  1. mplungjan

    145,789

    Master

    0 points yesterday

    Profile
    Rank: Savant
  2. Ray_Paseur

    91,292

    Master

    0 points yesterday

    Profile
    Rank: Savant
  3. billprew

    63,376

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. RobSampson

    54,636

    Master

    0 points yesterday

    Profile
    Rank: Genius
  5. DaveBaldwin

    51,700

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  6. sedgwick

    43,950

    1,600 points yesterday

    Profile
    Rank: Genius
  7. leakim971

    43,184

    0 points yesterday

    Profile
    Rank: Genius
  8. dgofman

    36,400

    0 points yesterday

    Profile
    Rank: Genius
  9. COBOLdinosaur

    28,424

    0 points yesterday

    Profile
    Rank: Genius
  10. woolmilkporc

    24,200

    0 points yesterday

    Profile
    Rank: Genius
  11. ozo

    20,600

    0 points yesterday

    Profile
    Rank: Savant
  12. Qlemo

    19,984

    2,000 points yesterday

    Profile
    Rank: Genius
  13. chaituu

    19,100

    0 points yesterday

    Profile
    Rank: Sage
  14. tagit

    19,000

    0 points yesterday

    Profile
    Rank: Genius
  15. farzanj

    18,550

    0 points yesterday

    Profile
    Rank: Genius
  16. nap0leon

    15,094

    0 points yesterday

    Profile
    Rank: Sage
  17. paultomasi

    14,700

    0 points yesterday

    Profile
    Rank: Master
  18. StingRaY

    13,136

    0 points yesterday

    Profile
    Rank: Wizard
  19. jason1178

    13,044

    0 points yesterday

    Profile
    Rank: Genius
  20. HainKurt

    12,350

    0 points yesterday

    Profile
    Rank: Genius
  21. HonorGod

    11,600

    0 points yesterday

    Profile
    Rank: Genius
  22. ahoffmann

    11,136

    0 points yesterday

    Profile
    Rank: Genius
  23. basicinstinct

    10,800

    0 points yesterday

    Profile
    Rank: Genius
  24. leew

    10,030

    0 points yesterday

    Profile
    Rank: Savant
  25. ChrisStanyon

    9,864

    0 points yesterday

    Profile
    Rank: Sage

Hall Of Fame