Forum Moderators: coopster
$username = preg_replace('/[^A-Za-z0-9\._-]/', '', $username);...what's the point of...
$username = htmlspecialchars("$username", ENT_QUOTES, 'utf-8');?
$username = preg_replace('/[^A-Za-z0-9\._-]/', '', $username); // Leave A-Z, a-z, 0-9, dots, spaces, underscores and hyphensIf you want to replace whitespace with the second line, you should probably include \s in the regular expression of the first line. Right now, it seems your second line would try to replace something (whitespace) that the first line does not allow (and has replaced).
$username = preg_replace('/ */', '-', $username); // Replace spaces with hypens
function san_data_un() {
GLOBAL $username;
4. I then put $var_1 into a hidden form field and use POST to send it to the next page.
is it possible for this data to be corrupted in any way by a hacker?
Didn't want to use a Global but when I tried to pass the variable like below I couldn't get it to work.
function san_data_un($username) {}
Will try that again as not sure what happened there?
function san_data_un($data) {
// Your regex magic goes here (which in this example you apply to $data, not to $username)
} $username = san_data_un($_POST['username']); due to not knowing very much about hacking
The most important thing to remember -- the golden rule of sanitization, if you will -- is that you can never trust the client.
[edited by: Orangutang at 4:06 pm (utc) on Oct 26, 2013]
$data = strip_tags($data);when it's followed by
$data = preg_replace('/[^A-Za-z0-9._-\s]/', '', $data); // Leave A-Z, a-z, 0-9, dots, underscores, hyphens and spaces,?
I thought anything within php tags was hidden.Well, yes, until you print it to the page via echo or other means. You see "userid[<?php echo $user_id;?>]" in your source code, but that's not what the client sees.
if (isset ($_POST['username']) and ! empty ($_POST['username'])){
The reason I'm sanitising the username is because I only want to allow certain chars, for instance I don't want someone to have a username of <b>un</b>
$username = isset($_POST['username']) ? $_POST['username'] : null;
if (isset($username)) {
if (preg_match('/[a-zA-Z_0-9]{4,20}/',$username)) {
/* Username looks OK */
} else {
/* Reject username */
}
} else {
/* Username has not been submitted */
}
The advice was by email and read, you will be made to look like a fool on WebmasterWorld.
The second part of your post about sending a complete form to my URL, wow I can't say I understand how that's done or how exactly it can compromise my code...
...I will add a string length check because I'm trying to make the application as secure as possible
(of course the chances of someone actually taking the time to do that is very small...
The appropriate HTTP request is sent directly to the URL that processes your form submissions.