Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET variable and IF statement

I have got problem with this simple if statement:

$type = $_GET['type'];
if ($type !== 1 || $type !== 2) {
    header('Location: payment.php');
    exit;
}

Only type 1 and 2 are allowed, but...

  1. www.example/succeed.php?type=1 - redirects back to payment.php
  2. www.example/succeed.php?type=2 - redirects back to payment.php
  3. www.example/succeed.php?type=3 - redirects back to payment.php

Last example is OK, but I don't know why it redirects too in first and second example.

like image 822
user2406937 Avatar asked Feb 01 '26 21:02

user2406937


2 Answers

!== is the identity operator; so it checks for type too.

But data in $_GET, $_POST, ... arrays are strings. So you need also to check against a string:

if ($type !== "1" && $type !== "2") /* ... */

Also checking if $a !== $x && $a !== $y will be always true (if $x !== $y). So use || here.

like image 177
bwoebi Avatar answered Feb 04 '26 09:02

bwoebi


Try this:

if (!($type == 1 || $type == 2)) {
    header('Location: payment.php');
    exit;
}

This can be spoken as Anything other than type is 1 or 2

like image 25
abstr Avatar answered Feb 04 '26 10:02

abstr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!