This is hard to answer, because there's not much code to go by, however:
Don't use PHP_SELF
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Don't do this, or at least sanitize the value, because using the raw value leaves the site open to XSS attacks! Posting to yourself can be done simply like this:
<form action="" method="POST">
Alternatively, use an absolute (and static) URI as the form destination.
Addendum
Don't let others talk you into thinking that you're somehow magically protected against it because you're hosting a small site, using some framework xyz or that some browsers will stop it. Find out for yourself and take appropriate action.
On SSL
If you submit sensitive data, you should use SSL! Anything else is a joke in comparison.
On CSRF
Forms that cause a state change in your site and use cookies to perpetuate sessions should be protected by CSRF tokens; the token must be part of the form submissions and are regenerated once used.
On SQL injection
Please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Tidbits (not actually related to security)
if(isset($_POST['submit'])) {
//Handle the form here with many $_POST['namehere']
}
This code assumes you will always have a submit button on your form that's called submit
. A more generic approach is to use this condition:
if ($_SERVER['HTTP_METHOD'] === 'POST') {
// something was submitted
if (isset($_POST['email'], $_POST['password']) {
// email and password submitted
// you may still wish to verify whether a "valid" email was given
}
}