Jump to content

Validating using class - not working :(


Drongo_III
 Share

Recommended Posts

Hi Guys

 

Can anyone tell me why this doesn't work?

 

<?php

$form = "<form action='test.php' method='POST'><input type='text' name='test' /> <input type='submit'>";

echo $form;

$name = $_POST['test'];



class validate
{

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

}


$z = new validate();
echo $z->check_input($name);


?>

 

 

Also, when it comes to validating POST data that's input via a form and retrieving it is it secure to set a variable for the Post i.e.

$name = $_POST['test'];

or is more secure to pass

 $_POST['test'] 

straight into the validation? It just occurs to me if you pull the post into a variable then you're inviting insecure code into your script. Though i am a complete noob at this so might be talking rubbish!

 

Any light you can shed on why my little script isn't working would be appreciated and any tips on the best method for validating data securely would also be welcome :)

 

Thank you

 

Drongo

Link to comment
Share on other sites

You never terminate the <form> with a </form>, so your form may not work in all browsers.

 

Also, the $_POST array is only set once the form is submitted, so you need some logic in you code to only do the validation if the form was submitted.

 

Ken

Link to comment
Share on other sites

Hi Ken

 

Thanks for the reply.

 

Ooops didn't spot the closing form tag. Been staring at the screen too long.

 

This is just a test page at the moment.

 

The thing is this function just refuses to work and i can't figure out why.

 

Even when i use just a variable it doesn't seem to be getting passed into the function

 

<?php

$form = "<form action='test2.php' method='POST'><input type='text' name='test' /> <input type='submit'></form>";

echo $form;

$name = $_POST['test'];

$name2 = '<?php echo "hello";';

class validate
{

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

}


$z = new validate();
echo $z->check_input($name2);


?>

 

V confused as i can't see what is wrong :/

Link to comment
Share on other sites

try to keep your function var consistent with your post var. So if your post var is $name, then your function var will be $name

class validate
{

function check_input($name)
{
    $data = trim($name);
    $data = stripslashes($name);
    $data = htmlspecialchars($name);
    return $name;
}

}

Link to comment
Share on other sites

Thanks Fugix

 

I tried that but still not luck :/ I think i am cursed as nothing i seem to try works at the moment!

 

Also, is that a rule about param names? I thought just $data would represent whatever param i supplied - i.e. so i could reuse the function on multiple types of form input. Or am i wrong?

 

 

 

 

try to keep your function var consistent with your post var. So if your post var is $name, then your function var will be $name

class validate
{

function check_input($name)
{
    $data = trim($name);
    $data = stripslashes($name);
    $data = htmlspecialchars($name);
    return $name;
}

}

Link to comment
Share on other sites

How is it NOT working?  How are you determining that it is NOT working?  Not working can mean many things!

 

 

I bet if you would look at "page source" you would find that your string is indeed, translated to html chars.

Link to comment
Share on other sites

I tried using the same example i found  for validation and it also did not work. The function works if you leave the form blank and returns the error message, but the other parts did work. I tested mine by redirecting to a different page and echo the $_SESSION variable. If I typed in <123456>, this is what echoed on the second page. It did not strip the markup characters.

 

Here is what I tried.

 

<?php
session_start();
if (isset($_POST['submit']))
{
$patient_id = check_input($_POST['patient_num'], "Enter Patient ID");
$_SESSION['patientid'] = $patient_id;
header('Location: pt_found.php');
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Patient Call Form</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>

<body>
<form name="patientfindform" action="#" method="post">
<div>
<label>Patient ID:</label>
<input type="text" name="patient_num" /><br />
</div>

<div>
<label> </label>
<input type="submit" name="submit" value="Find Patient" /><br />

</div>
</form>
<script>
type="text/javascript">
document.patientfindform.patient_num.focus();
</script>
</div>
</body>
</html>
<?php
function check_input ($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data)== 0)
{
die($problem);
}
return $data;
}
?> 

Link to comment
Share on other sites

Are you looking at your browser window, or at the page source file for the 'It did not strip the markup characters'.  You do know that the browser will display the markup char, by their given entities?

Link to comment
Share on other sites

Do a "show source", there you will see that the characters are replaced. If they hadn't been, you would have seen nothing, since the browser would have eaten any thing the looks like an HTML tag.

 

Ken

Link to comment
Share on other sites

Are you looking at your browser window, or at the page source file for the 'It did not strip the markup characters'.  You do know that the browser will display the markup char, by their given entities?

 

JCBones,

 

In all honestly, I did not know that nor much about all of this. I am an old ICU pharmacist trying to create a better hospital pharmacy simulation for my pharmacy students. Six months ago I had no clue what HTML, CSS, and  PHP even stood and  I am slowly plugging away, but it is hard to teach an old dog a new trick. The last time I took a computer class, I had to carry stacks of punch cards to the computer room!

 

Thanks for everyone's suggestions. 

Link to comment
Share on other sites

If you are using PHP5, please try the following:

 

class validate
{
function __construct() {}

function check_input($name)
       {
           $data = trim($name);
           $data = stripslashes($name);
           $data = htmlspecialchars($name);
           return $name;
       }	
}

 

Thanks!

Link to comment
Share on other sites

Ahh yes you were right chaps. It was just the output to the browser. The source shows them encoded. I didn't realise it would do that - noob mistake.

 

So that just leaves two questions:

 

1) Does that function does the necessaries in the correct order

 

2) Is it secure to pass the Post values to a variable and then clean them or should you clean straight from accessing the POST array?

 

Thanks,

 

Drongo

Link to comment
Share on other sites

Not a n00b mistake at all, it catches all of us from time to time.  Sometimes staring at the monitor for to long, just gets to ya.

 

1.) Yes, the function is fine, but I would add mysql_real_escape_string() to the variable, if I was inserting to a mysql database.

 

2.) Yes, perfectly secure.  Although it is less coding to run it straight through.

Link to comment
Share on other sites

Thanks JC! That helps a lot.

 

Well I think the case of the phantom validator is solved. I shall have Watson chronicle this in my memoires :) Right onwards and upwards.

 

Thanks chaps!

 

 

 

Not a n00b mistake at all, it catches all of us from time to time.  Sometimes staring at the monitor for to long, just gets to ya.

 

1.) Yes, the function is fine, but I would add mysql_real_escape_string() to the variable, if I was inserting to a mysql database.

 

2.) Yes, perfectly secure.  Although it is less coding to run it straight through.

Link to comment
Share on other sites

Drongo_III - At some point someone suggested changing your method to this:

 

function check_input($name)
       {
           $data = trim($name);
           $data = stripslashes($name);
           $data = htmlspecialchars($name);
           return $name;
       }

 

If you take a close look at it, that method DOES NOTHING. It returns EXACTLY WHAT WAS PASSED IN. All of the operations in it are performed against $name and assigned to $data, but $name is returned UNMODIFIED.

 

Make sure you fix this method. Your original method was fine - I don't know why it was suggested to change the parameter name from $data to $name, but you either need to change all of the $name variables to $data or change all of the $data variables to $name.

 

Link to comment
Share on other sites

Thanks David

 

I changed it back to the original as when i checked the source i realised nothing was happening when $name as the parameter and now you've pointed out the logic i can see why.  Wrapping your head around the logical way the system deals with input is starting to click into place - slowly!

 

Thanks for the help! :)

 

Drongo_III - At some point someone suggested changing your method to this:

 

function check_input($name)
       {
           $data = trim($name);
           $data = stripslashes($name);
           $data = htmlspecialchars($name);
           return $name;
       }

 

If you take a close look at it, that method DOES NOTHING. It returns EXACTLY WHAT WAS PASSED IN. All of the operations in it are performed against $name and assigned to $data, but $name is returned UNMODIFIED.

 

Make sure you fix this method. Your original method was fine - I don't know why it was suggested to change the parameter name from $data to $name, but you either need to change all of the $name variables to $data or change all of the $data variables to $name.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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