tags:

views:

382

answers:

7

Here's a php code

if(isset($_GET['id'])) {
    //do something
} else {
    redirect('index.php'); //redirect is a function
}

Now if id is set (ex: index.php?id=12) then the action is performed but if id is not set (ex: index.php?id=) this shows an error, how to overcome this...??

How to determine id is an integer and it's not empty and then perform the specific action....

Edited

Thank you all for your answers but i am still getting that error...

if(isset($_GET['id'])) { // I implemented all these codes but still....
    $user= User::find_by_id($_GET['id']);
   // Database Classes Fetches user info from database
}
else {
    redirect('index.php'); //redirect function
}

If the id is 1 or greater than 1 then the script executes perfectly. ( index.php?id=1 )

But id i set id as noting i get an error i..e index.php?id=

The code should automatically redirect user to the index.php page instead of showing an error.....

ERROR : Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

A: 
if (!empty($_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT))

or

if (!empty($_GET['id']) && ctype_digit($_GET['id']))
zerkms
Note that integer `0` and string `"0"` are considered to be "empty" by PHP.
Daniel Vandersluis
@Daniel: yeah, i remembered that while writing answer but I bet that he is referring to some primary key which is definitely positive integer
zerkms
@zerkms it's entirely possible that a primary key might be zero or negative.
timdev
@timdev A primary key for a user I doubt is negative or zero. It is possible though... Bad design nevertheless.
Chad Scira
@Chad Scira Yes, probably bad design (basically, it suggests that negative PK values are somehow special, which is kind of like the magic number antipattern.
timdev
+2  A: 

What does the error tell you?

But this would check if it's set, and if it's an integer

if(isset($_GET['id']) && ctype_digit($_GET['id']) && intval($_GET['id']) > 0)

is_numeric @ php.net

Or check out ctype_digit

Terw
let's suppose ?id=1.5
zerkms
`is_numeric` really doesn't cut it.
Linked to the ctype_digit function wich would just check if there are integers, updated the example to use this
Terw
But if i keep url as ( index.php?id= ) it shows an error....., it should redirect to index.php
Roccos
@MrXexxed: And @_GET is allways a string, right?
Terw
What is the error it is showing?
Joseph
Roccos: Could you post the error?
Terw
i have updated the question, please check it...
Roccos
Terw
Parse error: syntax error, unexpected '[' in H:\Dev\Websites\www.demo.com\user-info.php on line 4
Roccos
Just copied and pasted my code? Have an error, use $ insted of the @ in my code above :P
Terw
+2  A: 

You should check both the set-status and the content:

if ( isset( $_GET['id']] ) && !empty( $_GET['id'] ) )
    // ...

Note that you should not use the value simply as it is and work with it. You should make sure that it only contains values you expect. To check for an integer, and even better to work with an integer from that on, do something like that:

$id = ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) ? intval( $_GET['id'] ) : 0;

if ( $id != 0 )
    // id is an int != 0
else
    redirect('index.php');
poke
is_int will never be true, from php.net: `Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). `
Terw
But if i keep url as ( index.php?id= ) it shows an error....., it should redirect to index.php
Roccos
@Terw: Oops, that's what happens, when I try to remember some function I don't use often.. Thanks!
poke
@Roccos: What error does it show?
poke
A: 

How to determine id is an integer and it's not empty and then perform the specific action....

if (!empty($_GET['id']) && (intval($_GET['id']) == $_GET['id'])) {
    //do something
} else {
    redirect('index.php'); //redirect is a function
}

The above may not be the best solution, but it should work for what you need. It will not allow the id to be 0, and will require it to be an integer.

Joseph
Will not work as you see on php.net, the @_GEt never return integers (if you doesn't specify it your self) it returns a string.
Terw
Thanks for pointing that out. I corrected the code so that it will work for what he needs.
Joseph
Sadly it didn't work for what i need...., It's should be simple ifid= (space) or id=0 it should automatically redirect user to index.php page without going further.., but it's not working... :(
Roccos
Are you doing anything with `$_GET['id']` before the if statement?
Joseph
A: 

Well... If you're setting it up as an integer type variable, then you could do also this:

if($_GET['id'] && gettype($_GET['id']) == 'integer'){
    #do something
}else{
    #do something else
}
Tom
This won't work as `gettype` will always report a string type, because the GET parameters are usually not typed. Also calling `$_GET['id']` will raise an index warning when it is not set.
poke
A: 
 if(isset($_GET['id']) && ctype_digit($_GET['id']) && is_numeric($_GET['id']) && $_GET['id']>0) {
  $user= User::find_by_id($_GET['id']);
  }
else {
  redirect('index.php'); //redirect function
  }

Try this and this code full pill your requirement.

KMK
+1  A: 

It seems like you're just having an issue with the ?id throwing a true when you want to check if its set. Thats because it is set, but not set to a value that you would like to use.

I'm assuming the userids have to be > 0 so just do this

if (isset($_GET['id']) && $_GET['id'] > 0) {
    $user= User::find_by_id($_GET['id']);
} else {
    redirect('index.php'); 
}

Also you're prolly best cleaning the id before processing it so

$userID = isset($_GET['id']) && $_GET['id'] > 0 ? (int) $_GET['id'] : 0;

if ($userID) {
    $user= User::find_by_id($userID);
} else {
    redirect('index.php'); 
}

Theres a lot of ways to do it, personally I prefer this. It makes your code look a bit cleaner.

Chad Scira