Jump to content

Recommended Posts

$user = mysql_real_escape_string(stripslashes(htmlentities(ucwords(strtolower($_POST["userid"])))));

$pass = md5(mysql_real_escape_string(stripslashes(htmlentities($_POST["password"]))));

 

thats my sql injection protection, it all works, except for when I add mysql_real_escape_string into the mix.

 

What exactly does it change?/do?

Link to comment
https://forums.phpfreaks.com/topic/94756-mysql_real_escape_string/
Share on other sites

if username = "text" and password = "text000"

 

mysql_real_escape_string($username) = "text"

mysql_real_escape_string($password) = "text000"

 

just use mysql_escape_string() unless you echoing the password/username, then just use htmlentities($username);

 

NOTE: mysql_escape_string MUST be used AFTER a connection to mysql has been established, ie:

 

<?php

mysql_connect();
mysql_select_db();
$query = "SELECT * FROM `table` WHERE `username`='".mysql_escape_string($_POST['userid'])."' AND `password`='".md5($_POST['password'])."'";
$Result = mysql_query($query);
print_r(mysql_fetch_array($Result);
mysql_close();

?>

 

hope this helps,

$user = mysql_real_escape_string(stripslashes(htmlentities(ucwords(strtolower($_POST["userid"])))));
$pass = md5(mysql_real_escape_string(stripslashes(htmlentities($_POST["password"]))));

 

How are you using these after the assignments?  Have you tried echo'ing the query you're generating and checking that it is correct?

Validating your credentials... Please wait a moment... 
<?php
/* get the incoming ID and password hash */


/* establish a connection with the database */
$server = mysql_connect("x", "x",
          "x");
if (!$server) die(mysql_error());
mysql_select_db("x");
  
$user = mysql_real_escape_string(strip_tags(htmlentities(ucwords(strtolower($_POST["userid"])))));
$pass = md5(mysql_real_escape_string(strip_tags(htmlentities($_POST["password"]))));

/* SQL statement to query the database */
$query = "SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'";

/* query the database */
$result = mysql_query($query);

if (mysql_fetch_row($result)) {
  /* access granted */
  
  $sql = "update users set online='<img src=\"/images/online.png\" border=\"0\" />' WHERE User = '$user'";
  mysql_query($sql) or die(mysql_error());
  
  session_start();
  header("Cache-control: private");
  $_SESSION["access"] = "granted";
  $_SESSION["user"] = $user;
  header("Location: index2.php");
  
  
} else
  /* access denied &#8211; redirect back to login */
  header("Location: index.php?a=Login has failed, please try again.");
?>

SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'

If you have a lot of users in your DB, create an index on `User` and maybe the first 5 or 6 characters of `Password`.  That should help the select query run quickly.

 

update users set online='<img src=\"/images/online.png\" border=\"0\" />' WHERE User = '$user'

The index above will also help with the speed of this query.  But why on Earth are you storing an image path in the database?  Change your `online` field to a TINYINT and set it to 1 if the user is online and 0 if they are not.  Alternatively you can use an ENUM field.  Then you use your program logic to display the online.png graphic for that user if they DB says they are online.

 

If you have relatively few users in your database, then it might be taking ages due to numerous factors.  A heavy server load, slow internet connection, something else inefficient in your program, or a million and one other possibilities.

A couple things. I wouldn't suggest using htmlentities when inserting data into the database. I woud use it when displaying the data. the problem is that htmlentities converts characters to their HTML character code, so the ampersand character '&' becomes '&'. Now you will have problems with character lengths. If a db field only takes 10 characters and the input field accepts 10 characters an input by the user of 'some&else' will result in a string that is 13 characters long.

 

Also, there is no need to do anything character escaping with the password. Just use MD5($_POST["password"]). The results will always be safe for insertion.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.