0

Hi
I'll get straight to the point. I'm using this code

$sex = @$_GET['v'] ;
  $sex= trim($sex); //trim whitespace from the stored variable
$sex = htmlentities($sex);

  $age= @$_GET['age'] ;
  $age = trim($age); //trim whitespace from the stored variable
$age = htmlentities($age);

... to get the age and sex credentials from the url address (example.com/page.php?age=22&sex=Female)
and this:

$query = "SELECT * FROM personas WHERE sex LIKE \"%$sex%\" AND age LIKE \"%$age%\" order by id DESC";

... to interogate the database and get the results displayed on page. It is working fine, but I know it is not secure, so my questions are:
- How to make it safer?
- How to make it do nothing if values don't exist in database - for example if there's nobody age 22, do nothing, or popup a message.
- How to make it check if age doesn't contain other characters beside numbers, and again, if it does, do nothing or display a message.
- Same as above, but to check sex for letters and popup the message if anything else is used as sex.
Thanks for your help!

5
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by Sorcher
0

Personally, I would use is_numeric() on the age as an extra check, as well as checking an upper and lower boundary (say 0-110). For sex I would explicitly check for your allowed values, or maybe default to one of them if the data is incorrect.

Edited by pritaeas: n/a

0

Sorry, I must gave the impression I'm an expert :)
Would you kindly write the code that will do the trick? I don't know much about php and mysql :(
Thank you!

0

Sorry, I must gave the impression I'm an expert :)
Would you kindly write the code that will do the trick? I don't know much about php and mysql :(
Thank you!

to secure a web app from XSS, validate input against whitelist and discard anything not in white list. Also escape output in case something was missed and got through your line of defense!
implementation differs and that is programming!

0

is_numeric() or maybe is_int() for integers. Each data item should be validated against the expected data type and constraints. You can check for complicated patterns with preg_match().

If using DBs, the standard cleaner is mysql_real_escape_string().
You can convert html to plain text (showing tags) with htmlentities.
Also you can use strip_tags() to remove all tags other than ones you wish to allow.

e.g.

$tagless = striptags($input,'<p><b><strong>');

This will allow only paragraph, and bold/strong tags to remain in $tagless - uselful for stripping <script> tags.

1

If using DBs, the standard cleaner is mysql_real_escape_string().

Moving to MySQLi or PDO and do parametric queries is the recommended way !

0

Agree Ev - I prefer PDO via binding parameters. But coders starting out usually begin with vanilla mysql.

0

Agree Ev - I prefer PDO via binding parameters. But coders starting out usually begin with vanilla mysql.

Which I always don't like as it becomes their behaviour :)

0

Well you could argue that, but PDO, as its OOP, can be a little difficult to grasp - especially if your trying to get to grips with basic SQL. However, I would recommend moving to PDO asap.

0

Well you could argue that, but PDO, as its OOP, can be a little difficult to grasp - especially if your trying to get to grips with basic SQL. However, I would recommend moving to PDO asap.

Yeah, PDO is OOP and OOP is not that hard ;)
If they find it hard, MySQLi have non OOP way also of doing param queries

0

Yep MySQLi has procedural way - but I'm a PDO convert now that I've got my head into OOP. And don't say OOP isn't hard! It took me 5 years to knock on the door! :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.