I am attempting to allow for a user to remain logged into a content management system I am creating using PHP but am having some trouble coming up with a method of securing the session data. You can read this in two ways simple guide to preform a simple system hack, or to offer your advice on alternate solutions.
With this there are a number of issues that might be of use. I am operating on a shared server which I do not own or administer, and I am unable to have the administrator modify any of the server components (i.e. excellent non-standard encryption package X will not be installed). I am looking for pretty good security that will stop the script kiddies, I am sure that a good hacker could hack a session no matter what I do. I am also not able to require the users to have support for cookies. It should be noted that this is preventing a lose of data that will ultimately be backed up, and it will offer no easy method of gaining root though comprimising this system.
Where I am now is that I have a user table in a MySQL database, and I access this information to return the user's id for use in preserving the session.
Below is a truncated psuedo-php code for storing the session data.
code:
$sql = SELECT id FROM table WHERE username = '$username' AND password = '$password';
$result = @mysq_query($sql);
$row = @mysql_fetch_array($result);
session_start();
$_SESSION['login_user_id'] = $row['id'];
header("location: mainCMSpage.php");
now with that code I am storing the user's id into the variable $login_user_id and I would check that the user is logged in using a simple function
code:
session_start();
if(!isset($_SESSION['login_user_id']){
header("location:loginPage.php");
}
This works togeather well and good. The authentication script works well, I get the ability to traverse the pages without needing to worry about storing my login values in hidden fields or of the like (which require a post request to authenticate correctly).
With the session I have the problem of being able to do something like type into the browser http://site.com/mainCMSpage.php?login_user_id=1 and bam I am the CMS administrator. The security of the above site rests on the idea that an attacker will not know both the variable name and a valid input for the user.
This is a decent amount of security unless a user somehow releases their 'GET' information via a referal URL or another method of simple social engineering.
This is a decent level of security since a couple of pieces of information must come togeather, but for some reason I feel that there is a good posability of comprimise and possible lose of data associated with this, and it will take time to rebuild the data even with backups.
So, after all of this it comes down to a the simple question, is there a better method. I was considering using some sort of encryption, but I am not sure exactly how to work the encryption in this sense as it is all very open. If you do know of a better solution to this problem could you maybe supply me with a link to some implementation details or a tutorial? I have read bits and pieces on using the IP address for validation or to use a generated key, but I am unsure as to how this would be implimented and have not found anything to point out how it would work in code.
Let me know if you have anything!
Thanks a lot,
-Dan-