473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PHP/MySQL login script strength. Good enough?

5
Hi everyone.
I had to build a login script to authenticate users because i couldn't find one out there that would tailor my needs. It works great, but i just want to make sure it looks strong enough.

Pretty much, once a user is authenticated, it pulls further data based on the user that will be used for further security within the webpage (like a series of if statements). Depending on a persons department, security level, posistion etc, certain access or even menus will be available to the user. Like i said, it works wonderfully, but just need to ensure the code is good:

[PHP]
<?php
session_start();
if ($_SESSION["logged_in"] == "false" OR $_SESSION["logged_in"] == "") {

$db = mysql_connect('localhost', 'user', 'pass') or die("Couldn't connect to the database.");
mysql_select_db('networks') or die("Couldn't select the database");

$_POST['user'] = addslashes($_POST['user']);
$_POST['pass'] = md5($_POST['pass']);

$result = mysql_query("SELECT count(id) FROM username WHERE password='$_POST[pass]' AND UID='$_POST[user]'") or die("Couldn't query the user-database.");
$num = mysql_result($result, 0);

if (!$num) {
$_SESSION["logged_in"] = "false";
} else {
$_SESSION["logged_in"] = "true";
$web_user = $_POST[user];
$web_pass = $_POST[pass];
$_SESSION["web_user"] = $_POST[user];
$_SESSION["web_pass"] = $_POST[pass];

if ($remember_me == "true") {
$time_expire = time()+5184000;
setcookie("web_user", $_SESSION["web_user"], $time_expire);
setcookie("uid_save", "true", $time_expire);
} else {
setcookie("web_user", $_SESSION["web_user"], time()-3600);
setcookie("uid_save", "true", time()-3600);

}

}

} else {
$web_user = $_SESSION["web_user"];
$web_pass = $_SESSION["web_pass"];
}
if ($logout == "true") {
$_SESSION["logged_in"] = "false";
$web_user = "";
$web_pass = "";
$logout = "done";
}

if ($_SESSION["logged_in"] == "true") {

include 'includes/config.inc';
include 'includes/db.inc';

$cid = mysql_connect($host,$usr,$pwd);
$SQL = " SELECT * FROM table WHERE UID = '$web_user' AND web_pass = '$web_pass' ";
$retid = mysql_db_query($db, $SQL, $cid);

while ($row = mysql_fetch_array($retid)) {
$fname = $row["fname"];
$position = $row["position"];
$pname = $row["pname"];
$email = $row["email"];
$email_pass = $row["email_pass"];
$homenum = $row["homenum"];
$position = $row["position"];
$position_ab = $row["position_ab"];
$class = $row["class"];
$security = $row["security"];

}

}

?>
[/PHP]

I also noticed that i need to change the db.inc to db.php cause anyone surfing to http://site.com/inc/db.inc can see the SQL credentials... any comments on that one? heh.

Thanks for the help!
(this place is great!)
Nov 28 '06 #1
4 2080
stewy
5
Bump....
Anyone? LOL
Nov 29 '06 #2
Hi everyone.
I had to build a login script to authenticate users because i couldn't find one out there that would tailor my needs. It works great, but i just want to make sure it looks strong enough.
It looks good to me. Good to see you're storing password as MD5 hash instead of plain text.

I also noticed that i need to change the db.inc to db.php cause anyone surfing to http://site.com/inc/db.inc can see the SQL credentials... any comments on that one? heh.
I'd put any file containing credentials outside the web root directory, so that the server simply can't serve it up. For example, if your Apache web root is /var/www/html/, I'd store all my credential includes (or any file that *should* be private) in /var/www/.
Nov 29 '06 #3
stewy
5
Thanks for the feedback. Much appreciated!

As for the credentials, i develop locally on apache, but my company prefers having hosting. Have no access to anything below the http directory :(

Gonna be renaming some extensions for a little while. Heh.

Later!
Nov 30 '06 #4
steven
143 100+
You shouldn't use md5.

You should use the crypt function of PHP, with the crypt type set to MD5, if that's what you wish. Using md5 like this makes passwords more easily decryptable. The crypt function is a one way crypt.

Here is how I handle sessions and auth.

Expand|Select|Wrap|Line Numbers
  1. // login and create session
  2.   private function login() {
  3.     $user = $this->db->selectRow("SELECT * FROM users WHERE username = '".getValue('username')."'");
  4.     if ($user && crypt(getValue('passwd'), $user['password']) == $user['password']) {
  5.       unset($user['password']);
  6.       $_SESSION['simple']['user'] = $user;
  7.       header("Location: ".getValue('url'));
  8.     } else {
  9.       unset($user);
  10.       header("Location: ".getValue('url'));
  11.     }
  12.   }
  13.  
Notice how I use the password hash stored in the database as the salt for the checking against the entered one?

Check out the crypt function on http://php.net/
Nov 30 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Randell D. | last post by:
Folks, I have installed MySQL v4 (client, server and development rpm's). I've tried and failed to use the recommended mysqladmin to set a root password after the installation (I have another post...
7
by: MLH | last post by:
I have an sql script file that is supposed to create a set of database tables for me. Supposedly I type the following on my linux box and its all supposed to work... mysql (ENTER) \....
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
5
by: linuxlover992000 | last post by:
I am a newbie in the world of MySQL. In fact I enabled it in my Linux box only because it is required to run WordPress (the blogging software). I was trying to plan ahead and figure out a way to...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
8
by: sathyashrayan | last post by:
Dear group, For a log-in page I have created a mysql db and user registers with a user name and password. The password field is encrypted with $passwd = sha1($_REQUEST); I insert the...
3
by: bull1099 | last post by:
I designed a simple site which has a login for users to access their account page. When i had my files uploaded on a terrible hosting service site, my website I designed was fully functional. I moved...
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I have been trying to find a solution to this on the Internet for months. Literally, ages and ages and ages, praying that someone in the open-source community has enough knowledge to put together an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.