Forum Moderators: coopster
If you don't mind, I'll attach my current login page, just to see if you guys spot any major flaws. I'd be very grateful for any ideas you may have !
As you see I've improvised a lot, hoping to boost my security. But I'm not entirely sure if I did any good work.
I've also read about session IDs and them being stored in some temp directory. I'll be honest, I have no clue what they are and where they are stored : (
Also I've read that it would be wise to check user-agent before login? Again I'm clueless on this..
Anyway, here is my code so far:
(I've also added small comments in the code for you)
my login page:
<?php session_start();
if (isset($_SESSION['username'])) {
//output stuff goes here...
}
else {
echo 'not logged in:
<form method="post" >
username:<input type="text" name="username" maxlength="255" value="">
<br>
password:<input type="password" name="password" maxlength="50">
<input type="submit" class="submit" name="action" value="Login">
</form>';
}
my login script:
(I placed it above my root folder and gave it a "funny" name, it is not called login.php lol)
<?php
// somewhere I've read about brute force cracking password, thus I've put a 1 second sleep before processing info. Good or useless idea?
sleep(1);
define('SQL_HOST','localhost');
define('SQL_USER','username');
define('SQL_PASS','longpassssword');
define('SQL_DB','companybase');
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die('Could not connect to the database; ' . mysql_error());
mysql_select_db(SQL_DB, $conn)
or die('Could not select database; ' . mysql_error());
function redirect($url) {
if (!headers_sent()) {
header('Location: http://' . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . '/' . $url);
} else {
die('Could not redirect; Headers already sent (output).');
}
}
if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'Login':
if (isset($_POST['username'])
and isset($_POST['password']))
{
// added this function to check if any BAD characters are in there
function letters($string) {
$eregi = eregi_replace("([A-Z0-9]+)","",$string);
if(empty($eregi)){
return true;
}
return false;
}
$keywords = $_POST['password'];
if(letters($keywords)) {
$go1 = "ok";
}
else{
$go1 = "no";
}
$keywords = $_POST['username'];
if(letters($keywords)) {
$go2 = "ok";
}
else{
$go2 = "no";
}
// if both check went well...
if (($go1=="ok")&&($go2=="ok")){
$pass=$_POST['password'];
$salt='a looooooooooooooong salt goes here';
$salted_md5=md5($salt.$pass);
$sql = "SELECT * FROM login WHERE username='" . $_POST['username'] . "' AND password='" . $salted_md5 . "'";
$result = mysql_query($sql, $conn)
or die('Could not look up user information; ' .
mysql_error());
if ($row = mysql_fetch_array($result)) {
session_start();
$_SESSION['username'] = $row['username'];
$_SESSION['realname'] = $row['realname'];
$_SESSION['level'] = $row['level'];
}
}
}
redirect('mainpanel.php');
break;
case 'Logout':
session_start();
session_unset();
session_destroy();
redirect('mainpanel.php');
break;
}
}
?>
Thanks for reading ! : D
I've also read about session IDs and them being stored in some temp directory. I'll be honest, I have no clue what they are and where they are stored
Check your php.ini config file. If you're on a UNIX like server platform (Linux,BSD,MacOSX), then you'll probably find session data stored in the /tmp directory. If you're on the Windows platform, your session data will probably be stored in the user's "temp" directory (i.e. the user that is running the httpd process), or perhaps C:\Windows\Temp or another directory defined in the php.ini config. They're just plain ASCII files that store all the session data in plain text, and usually start with "sess_" followed by the MD5 hash session id value (or SHA1 value if you've selected that).
Being that you're not sure where the session data is even stored, you may not know much more about the server configuration. If you're on a "shared server" hosting account, then the session data is typically in the /tmp or /var/tmp directories, and available to anyone that has an account with that host (on that server) to look at your session data. BE SURE YOU DO NOT STORE ANY SENSITIVE DATA IN THE SESSION.
// somewhere I've read about brute force cracking password,
// thus I've put a 1 second sleep before processing info.
// Good or useless idea?
sleep(1);
define('SQL_HOST','localhost');
define('SQL_USER','username');
define('SQL_PASS','longpassssword');
define('SQL_DB','companybase');
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die('Could not connect to the database; ' . mysql_error());
mysql_select_db(SQL_DB, $conn)
or die('Could not select database; ' . mysql_error());
// added this function to check if any BAD characters are in there
function letters($string) {
$eregi = eregi_replace("([A-Z0-9]+)","",$string);
if(empty($eregi)){
return true;
}
return false;
}
// if both check went well...
if (($go1=="ok")&&($go2=="ok")){
...and this conditional can then be simply...
if ( ctype_alnum($_POST['username'])
&& ctype_alnum($_POST['password']) ) {
$sql = "SELECT * FROM login WHERE username='" . $_POST['username'] . "' AND password='" . $salted_md5 . "'";
$result = mysql_query($sql, $conn)
or die('Could not look up user information; ' .
mysql_error());
Secondly, take out that call to mysql_error() reporting the error output from the login attempt. This is the worst place to allow the user to see what causes the script to error out, i.e. column names that store the login info.
The use of a salted hash is always a good idea, but I might go with SHA256 or a stronger hashing mechanism.
Oh, and session_start() should simply be at the top of the script, since you use it in either event (login or logout).
Also I've read that it would be wise to check user-agent before login? Again I'm clueless on this.
Missed this on the first pass through. The user-agent string can be spoofed, so this is also pretty much useless. IP addresses, unless you're on a controlled LAN, are useless. Using JavaScript to control login attempts is useless (as it can be circumvented or just plain turned off).
You also mention that I never should store any sensitive data in my session. What kind of data do you mean?
As far as I know, I store these variables:
$_SESSION['username'] (eg marc_black)
$_SESSION['realname'] (eg Marc Black)
$_SESSION['level'] (eg 3)
This data is just rudimentary way to show certain elements on site, like "welcome Marc Black" etc.
Dunno If any other info is being exposed without me declaring it, hope not : )
You also mention that I never should store any sensitive data in my session. What kind of data do you mean?As far as I know, I store these variables:
$_SESSION['username'] (eg marc_black)
$_SESSION['realname'] (eg [/quMarc Black)
$_SESSION['level'] (eg 3)