Using Zend Studio with Zend Framework = super fast / easy development.

SolarPHP authentication using MySQL

September 24th, 2008 by Ray in Cookbook, Solar | Tags: , | No Comments

This post is an extension to SolarPHP’s current manual page on User Authentication and is intended for folks that already have a project setup using the SolarPHP framework. What I hope to accomplish here is to show you how to setup user authentication using MySQL and SolarPHP, something the Solar manual currently does not show you how to do. What I do not cover here is setting up the login box for your project. The login box itself comes packaged with the Solar framework and will appear on your project page if you do not change the default layout.

Resources related to this tutorial can be found on the last page.

Getting Started – Your configuration file

First, we want to setup our configuration file to tell Solar to use the MySQL adapter so we can establish a connection to the database. For more information on how to setup a configuration file please reference SolarPHP’s manual.

[PHP]
$config = array();

// Front controller HREF prefix
$config['Solar_Uri_Action']['path'] = '/index.php';

// Public directory HREF prefix
$config['Solar_Uri_Public']['path'] = '/public';

// Specify our SQL adapter
$config['Solar_Sql'] = array(
'adapter' => ‘Solar_Sql_Adapter_Mysql’,
);

/*
*
* SQL adapter information
*
* `host` : Host that houses our database.
* `user` : User name that has access to the database.
* `pass` : Associated password with the user name.
* `name` : Name of the database we are connecting to.
*
*/
$config['Solar_Sql_Adapter_Mysql'] = array(
‘host’ => ‘localhost’,
‘user’ => ‘root’,
‘pass’ => ‘myS3cR37p@ssw0rd’,
‘name’ => ‘MyProject’,
);

// Done!
return $config;
?>
[/PHP]

MySQL table creation

Next, we want to create the table that will hold our user information. At this point I am assuming that you have already created your database and the proper credentials associated with it. *Note the created and updated columns. I have thrown these in here for later use not covered in this post. You can leave these two columns out if you want.

[CODE]
#
# Basic user table layout
#
# name : Real name of our user.
# handle : User name used to login with.
# password : MD5 hash will be stored here.
# created : Timestamp when the row was added.
# updated: Timestamp when the row was modified.
#
CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
name VARCHAR(255),
handle VARCHAR(255),
password VARCHAR(32),
created DATETIME,
updated DATETIME
)
[/CODE]

Back to the configuration file – Setting up the authentication

Now that we have our users table, we want to jump back into our configuration file and tell Solar to use the SQL adapter for authentication. This is what our final configuration file will look like.

[PHP]
$config = array();

// Front controller HREF prefix
$config['Solar_Uri_Action']['path'] = '/index.php';

// Public directory HREF prefix
$config['Solar_Uri_Public']['path'] = '/public';

// Specify our SQL adapter
$config['Solar_Sql'] = array(
'adapter' => ‘Solar_Sql_Adapter_Mysql’,
);

/*
*
* SQL adapter information
*
* `host` : Host that houses our database.
* `user` : User name that has access to the database.
* `pass` : Associated password with the user name.
* `name` : Name of the database we are connecting to.
*
*/
$config['Solar_Sql_Adapter_Mysql'] = array(
‘host’ => ‘localhost’,
‘user’ => ‘root’,
‘pass’ => ‘myS3cR37p@ssw0rd’,
‘name’ => ‘MyProject’,
);

// Specify our authentication adapter
$config['Solar_Auth'] = array(
‘adapter’ => ‘Solar_Auth_Adapter_Sql’,
);

/*
*
* Authentication adapter information
*
* `table` : MySQL table holding our user information.
* `handle_col` : Specify the column for the user’s handle.
* `passwd_col` : Specify the column for the user’s password.
*
*/
$config['Solar_Auth_Adapter_Sql'] = array(
‘table’ => ‘users’,
‘handle_col’ => ‘handle’,
‘passwd_col’ => ‘password’,
);

// Done!
return $config;
?>
[/PHP]

Done! – Conclusion and additional resources

That’s it! It really is that simple to change your authentication source from using a .htpasswd file to using MySQL. If any of you got lost in the process, please reference the materials below. If you are still stuck, please join the SolarPHP mailing list and/or IRC channel and post up ;-)


Leave a Reply