Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

php login security


I have a login for mysql with php that I tweaked and it works.
Apart from password is readable (just testing it ) what else would I need to do for security?


$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="db_jagguy"; // Database name
$tbl_name="tblogin"; // Table name

// Connect to server and select databse.
mysql_connect($host, 'root') or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");

// Define $myusername and $mypassword

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM " . $tbl_name . " WHERE login='" . $myusername . "' and password='" . $mypassword . "'";

// Mysql_num_row is counting table row

// If result matched $myusername and $mypassword, table row must be 1 row


// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['mypassword']=$mypassword ;

else {
echo "Wrong Username or Password";



// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.


if(!isset($_SESSION['myusername'])) {
//echo "helloe";


Open in new window

  • 6
  • 3
  • 2
  • +1
1 Solution
Jagadishwor DulalBraces MediaCommented:
You are using mysql_real_escape_string and stripslashes yo prevent from sql injection see wikihow site for further works:
Stop using mysql_query
And follow the practises in the link
And of course have the login over SSL
Ray PaseurCommented:
Add error_reporting(E_ALL) to the top of all your PHP scripts.  This will help you avoid common errors like relying on an undefined variable or constant.

This article shows how to get started in PHP.

This article tells the design pattern for PHP client authentication.

This article tells why and how to get off the MySQL extension.

Make a Google search for PHP + MySQL security and read the links you find on the first page.  Then join the OWASP project, to keep up-to-date on security.  It's a moving target.  If you think about it carefully, the mantra should be "accept only known good values."
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

jagguyAuthor Commented:
My question was about the code I posted which I understand and I have done some php before. Worrying about  newer versions of php/mysql can be looked at in due time and the host server wont move to a newer version anytime soon as this will be bad for business.

Also my code does work which and I did research on security which is why I got the code in the first place.

 I will remove all non alpha numeric chars from a input string .

I dont believe my question has been answered as I did research and I would like to know what exactly is the security issue I am facing.
I cannot see any security issues with your code. I pointed to the PDO version because it is a more secure way of running queries - it will automatically take care of sql injection formats.
You say the security issue I am facing - what security issue?
jagguyAuthor Commented:
Yes I will look at PDO version in time. I thought there was a security issue but it looks like it is ok. Another issue is that php 5.5 hasnt  even been released officially ? The idea that all the ecommerce websites will suddenly not work as host servers will update to php 5 is just not going to happen anytime soon. This migration will take YEARS.
PDO is available now as standard not just in 5.5
That mysql_query will become unsupported means you should be writing your queries future proof i.e. do not be using old technologies for new code.
jagguyAuthor Commented:
how much work is involved in updating my code? I dont think it really matters for now but i see your point.
The main difference is using prepared statements - its slightly different than mysql_query but no more work (probably less work/code)

Setup the db connection, pass in host, db name, username and password or hardcode them
$conn = new PDO('mysql:host=localhost;dbname='.$database_name, $username, $password);

Prepare your sql
     $stmt = $conn->prepare("SELECT column1,column2 FROM table WHERE comparison1=:mydata1 AND comparison2=:mydata2");
            ':mydata1' => $_POST['mydata1']),
            ':mydata2' => $_POST['mydata2'])

$myresults= $stmt->fetch(PDO::FETCH_ASSOC);

And that basically is it, except for accessing the data - no escaping anything or worrying about injection.
PDO seperates the logic of the sql when passing it to MySQL removing any problem of sql injection - basically it tells MySQL here I am selecting something from a table based on this column and then it passes the information to base the select on
Ray PaseurCommented:
We write answers here for the purpose of helping you.  If you read the answers and they point you to an article, it's to save you from having to ask a million little questions.
how much work is involved in updating my code?
Here is the answer:
I dont think it really matters for now...
If you worked for me, that statement just got you fired.  We told you it matters now!  I need developers who can see the future and avoid future shock.  Especially if the future is written into the documentation of the online man pages!

Best of luck with your project, ~Ray
Even though you have accepted my answer I will again (in that I have pointed to it in the past) point you to Ray's great article - a lengthy thing but well worth reading.
What I gave you is a condensed version of writing future proof code (though that in 5 years may well be deprecated)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now