1

In my form am validating user input so i made dataValidation and added htmlspecialchars()

      <?php

header('Content-Type: text/plain');

$post_cat = dataValidation("<a href='test'>Test</a>", ENT_QUOTES, 'UTF-8');

function dataValidation($cleandata) {
    $data = trim($cleandata);
    $data = stripslashes($cleandata);
    $data = htmlspecialchars($cleandata);
    return $cleandata;
}

echo $post_cat;

When i echo i get output like this <a href='test'>Test</a>

But actually when using htmlspecialchars()

Output should be like this :

&lt;a href='test'&gt;Test&lt;/a&gt;

But in my case dataValidation function is not working

1
  • you aren't using any of your other arguments. Commented Jan 10, 2015 at 13:31

2 Answers 2

1
return $cleandata;

Should be

return $data;

Also you're throwing away intermediate values of $data. You probably don't want that.


Not really part of the answer, but I would recommend you use an IDE with code analysis. Code analysis would be able to tell you that $data isn't getting used after each assignment. This would allow you to catch this bug early.

Sign up to request clarification or add additional context in comments.

4 Comments

am using net beans how do i check in that??
en.wikipedia.org/wiki/Integrated_development_environment basically a really fancy text editor geared for writing code. I personally use PHPStorm which is really great but not free. Eclipse is equally great and free.
I think NetBeans does it automatically. Make sure it detects your language correctly (PHP). Some editors get a little confused when you mix PHP, HTML, CSS and JavaScript in one file.
If you're worried about XSS you don't need stripslashes, htmlspecialchars is sufficient.
1

i think it should be like this

   function dataValidation($cleandata) {
     $data = trim($cleandata);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.