0

This is not working,

<?php 
if(!isset($_GET['id'])){
    header('location: index.php');
}
$id = $_GET['id'];
if(!is_int($id)){
    header('location: index.php');
    }


 ?>

It's always redirecting me although the url is http://localhost/JMToday/classes/images.php?id=53

Sam Gabriel
  • 327
  • 1
  • 6
  • 15
  • Just wanted to point out it doesn't matter where your data comes from - an address bar, a form, a AJAX call, whatever. All data sent to your server can be hijacked and abused, so you must check ALL incoming data. – James Jan 08 '11 at 09:37
  • 4
    SAM! You have completely edited this question so half the answers don't make sense any more. Really bad form! Make a new question! – James Jan 08 '11 at 12:03
  • @James who put you on charge? Editing questions is perfectly legitimate here – Your Common Sense Jan 08 '11 at 12:29

4 Answers4

1

Another point would be to use PDO with Prepared Statements. You get a certain level of DB abstraction with PDO and Prepared Statements drop the necessety of using mysql_real_escape_string or similar.

Another solution is an ORM, that might be a big overkill and they would most likely use PDO under the hood but if the requirements match it, why not...

DrColossos
  • 12,656
  • 3
  • 46
  • 67
0

check that $id is an integer

if (!is_int($_GET['id']){
echo 'bad id';
}
0

A nonsense question and a bunch of nonsense answers again.

  1. Your question has nothing to do with injections. It's parameter validation.
    If you think that parameter is invalid, not a redirect but 404 status should be returned.

  2. Outside variables are always of string type. So, you have to either do some regexp or is_numeric.

  3. Still it has nothing to do with injections. Your database code should be able do deal with any types of data. There are very simple mechanisms to handle it. You can refer to this answer for the details

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Because Sam completely edited his question. The answers did make sense on the 1st question he was asking. – James Jan 08 '11 at 12:04
  • No, Seth's answer was wrong. But Sam's 1st question was basically user passes in a GET parameter, how can I make sure it's a int. Classic SQL Injection question. – James Jan 08 '11 at 12:12
  • @James yup. and no one to answer that "classic" question. Because noone understands what is it and how to protect. – Your Common Sense Jan 08 '11 at 12:14
0

You can use two things either use sprinf for setting queries Like This

$professionalQry = sprintf("SELECT * FROM `professional` WHERE `user_id`=%d",$userId);

OR

Use mysql_real_escape_string

Like this

$professionalQry = sprintf("SELECT * FROM `professional` WHERE `user_id`='%s'",
                           mysql_real_escape_string($userId));
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Harish
  • 2,311
  • 4
  • 23
  • 28