Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:09:55+00:00 2026-05-28T17:09:55+00:00

This question gets asked a lot, but I still haven’t found a straight answer

  • 0

This question gets asked a lot, but I still haven’t found a straight answer on stackoverflow. Are these two functions sufficient or not? There are a lot of contradictory comments around the internet “yes its fine?, “no, never use it”. Others say, use PDO, which I don’t understand. I’m just a beginner to PHP, so I don’t fully understand all of the ins and outs of security.
I’ve tried reading and understanding the following, but many don’t make much sense to me.

http://ha.ckers.org/xss.html
Do htmlspecialchars and mysql_real_escape_string keep my PHP code safe from injection?

What if I use preg_replace to strip unwanted characters?

I’m incredibly confused and don’t know where to start.

EDIT: Could someone please also recommend how I go about understanding prepared statements (assuming this is the best option).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:09:55+00:00Added an answer on May 28, 2026 at 5:09 pm

    Sam, if you are storing the input in a database, to avoid SQL injection and XSS then those two functions are enough. If you are storing passwords, you must encrypt the passwords with one-way encryption (that is they can not be decrypted).

    Let me expand my answer:
    First of all, SQL Injection is a method where a malicious user will attempt to modify your SQL statement to make it do their will. For example, let’s say you have a login form. By inserting one of the following values into an un-protected form, I will be able to log into the first account without knowing the username or password:

    ' or 1=1 -- 
    

    There are many versions of the above injection. Let’s examine what it does to the SQL executed on the database:

    The PHP:
    mysql_query(“SELECT * FROM users WHERE username='” . $username.”‘ AND password='” . $password . “‘;”);

    When the above is executed, the following SQL is sent to the database:

    SELECT * FROM users WHERE username='' or 1=1-- ' AND password='' or 1=1--';
    

    The effective part of this SQL is this:
    SELECT * FROM users WHERE username=” or 1=1

    as the double dash (with the space afterwards) is a comment, removing the rest of the statement.

    Now that gives the malicious user access. With use of an escaping function such as mysql_real_escape_string, you can escape the content so the following is sent to the database:

    SELECT * FROM users WHERE username='\' or 1=1-- ' AND password='\' or 1=1--';
    

    That now escapes the quotes, making the intended strings, just that – strings.

    Now let’s view some XSS.
    Another malicious user would like to change the layout of a page. A well known XSS attack was the Facespace attack on Facebook back in 2005. This involves inserting raw HTML into forms. The database will save the raw HTML and then it will be displayed to users. A malicious user could insert some javascript with use of the script tag, which could do anything javascript can do!

    This is escaped by converting < and > to <l; and > respectively. You use the html_special_chars function for this.

    This should be enough to secure normal content on a site. However passwords are a different story.

    For passwords, you must also encrypt the password. It is advisable to use PHP’s crypt function for this.

    However, once the password is encrypted and saved in the database as an encypted password, how can you decrypt it to check that it is correct? Easy answer – you don’t decrypt it. HINT: A password always encrypts to the same value.

    Were you thinking ‘We can encrypt the password when the user logs in and check it against the one in the database’, you are correct…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this kind of question gets asked alot, but I still haven't been
I know this question gets asked a lot because I have looked at many
It seems like this question gets asked frequently, but having looked through all of
It seems that this question gets asked frequently , but I am not coming
I've seen this question asked around before, and found an answer for how to
This question may have been asked before, but I'm starting to get into game
I did a search on SO and looks like this question gets asked quite
Not sure if this is the usual sort of question that gets asked around
I thought this question would have been asked before, but I couldn't find it
I've seen this question asked on this site, but mine has a slight variance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.