Jump to content

[SOLVED] Make login script safer?


dezkit

Recommended Posts

Is it possible to make this login script even safer then it is?

 

<?php
$host="localhost";
$username="";
$password="";
$db_name="test";
$tbl_name="members";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/119981-solved-make-login-script-safer/
Share on other sites

well i am not trying to be rude, but that script is everything but safe, you are vulnerable to sql injection and php injection (if register_globals = on)

 

advices:

- encapsulate your db connection and its configuration variables

- use prepared queries

- do not only use stripslashes() but also validate it using ctype (http://be.php.net/manual/en/book.ctype.php)

- do not store to much information in your session, storing a user id is more than sufficient (certainly do not store passwords in sessions)

- enable full error reporting when in your development environment, disable all error reporting on production (for example create a function that when called on your development server it echo's the message and on your production server it stores it to a password protected directory on your server or under your server root

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.