Note: Please look at our updated article, How To Create a Secure PHP Login Script, written in 2011. How Does This Work This is a short explanation why I have chosen these authentication methods. Users with shell access to the web server can scan valid session id's if the default /tmp directory is used to store the session data. The protection against this kind of attack is the IP check. Somebody who has a site (on a shared host with you) can generate valid session for your site. This is why the checkSession method is used and the session id is recorded in the database. Somebody may sniff network traffic and catch the cookie. The IP check should eliminate this problem too. Preparation You need first to decide what information to store about members, the examples provided will assume almost nothing to make it easier to read. I will use the PHP 4.1 super global arrays like $_SESSION, $_GET, etc. If you want to make it work on an earlier version of PHP you will have to substitute these with $GLOBALS['HTTP_SESSION_VARS']. Database Schema This is only an example bare structure suitable for online administration, if you want to have registered members you should add more columns. The schema is somewhat MySQL specific, I have yet to use another database other than MySQL and PostgreSQL but if you are using PostgreSQL you can convert the schema with the example script provided in my article Converting a database schema from MySQL to PostgreSQL. CREATE TABLE member ( The password and cookie fields are md5 hashes which are always 32 octets long. Cookie is the cookie value that is sent to the user if he/she requests to be remembered, session and ip are respectively the session id and the current IP of the visitor. Connecting to the Database function &db_connect() { This function connects to the database returning a pointer to a PEAR database object. Session Variables To ease access to the current user's information we register it as session variables but to prevent error messages and set some defaults we use the following function. function session_defaults() { ... with a check like: if (!isset($_SESSION['uid']) ) { to set the defaults. Of course session_start must be called before that.
blog comments powered by Disqus |
|
|
|
|
|
|
|