Home arrow PHP arrow Page 4 - User Authentication with patUser (part 1)

Zone Six - PHP

Need to add authentication to your PHP-based Web application? Getit done in a jiffy with patUser, a PHP class which makes it possible torapidly add powerful user management capabilities to your Web application.

TABLE OF CONTENTS:
  1. User Authentication with patUser (part 1)
  2. Power User
  3. Dump Truck
  4. Zone Six
  5. Breaking It Down
  6. A Different Realm
  7. Icing On The Cake
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 15
April 23, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
I'll begin with something simple - using patUser to restrict access to a Web page. In this scenario, only users who provide appropriate credentials - a login name and password - will be allowed access to the page; all other users will simply be presented with an error message.

Here's the code:

<?php // include classes include("../include/patDbc.php"); include("../include/patUser.php"); include("../include/patTemplate.php"); // initialize database layer $db = new patMySqlDbc("localhost", "db111", "us111", "secret"); // initialize template engine $tmpl = new patTemplate(); $tmpl->setBasedir("../templates"); // initialize patUser $u = new patUser(true); // connect patUser to database/template engines $u->setAuthDbc($db); $u->setTemplate($tmpl); // check credentials before displaying page $uid = $u->requireAuthentication("displayLogin"); // restricted page goes here ?> <html> <head> <basefont face="Arial"> </head> <body> <center> <h2>Welcome to Zone 6!</h2> <u>This is a restricted zone. Trespassers will be vaporized.</u> </center> <p align="right"> Your user ID is <?=$uid?> <br> <a href="logout.php">Log out</a> </body> </html>
As you can see, the script above contains an HTML page which I'm assuming you want to restrict access to, surrounded with some fairly complicated code. The code is all explained a little further down, so don't worry too much about it just yet; instead, try accessing this page through your Web browser.

You should see something like this:



Try entering a random user name and password - since the database is currently empty, the system should barf and throw you back out with an error message.



Now, add a user to the "users" table,

INSERT INTO users (`uid`, `username`, `passwd`) VALUES ('', 'joe', 'joe');
and try logging in again with that username and password. This time, patUser should recognize that your credentials are valid and allow you access to the page.



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: