0

I'm currently experimenting with PHP GET Requests, But stuck with this. Please remember i haven't really experimented with this. So it's rather now to me,

To the point..

I'm trying to return an invalid argument message if the user does not specify one of the following arguments

Been tinkering with this for quite a bit, But unable to really figure it out.

?param=first or ?param=second

But it returns it returns the valid parameter message anyway. Any suggestions to this? I'll drop my code below!

<?php

$param = $_GET['param'];

if (empty($_GET['param']))
  {
    print('<h2>Parameter param is not set!<h2>');
    print '<br>';
    print('<h5><font color="red">Proper usage: http://localhost/zoek.php/?param=first</font></h5>'); //first
    print('<h5><font color="red">Or second http://localhost/zoek.php/?param=second</font></h5>'); //second
    die();
  }


 ?>


<?php


if ($param == 'first')
  {
    print('Do something when url = http://localhost/zoek.php/?focus=first');
    //do more stuff
  }

else if ($param = 'second')
  {
    print('Do something when url = http://localhost/zoek.php/?focus=second');
    //do more stuff.
  }


//attempt to return error message when parameter $param is not first or second
else
  {
    print('The by you specified argument '. $_GET['param'] .' is invalid.');
    die();
  }


?>
  • `$param = 'second'` should be `==` because you're wanting to ***check*** the value *not* ***set*** the value. – Martin Jan 13 '18 at 13:49
  • Some points: **1)** For this sort of thing it's more efficient to use `switch` than `if` statements. – Martin Jan 13 '18 at 13:50
  • Some points: **2)** You should be using (redirecting to) `https` if possible. – Martin Jan 13 '18 at 13:50
  • Some points: **3)** `print` statements do not need to be enclosed in brackets and this will also slow down the processing. – Martin Jan 13 '18 at 13:51
  • Some points: **4)** You set `$param = $_GET['param'];` but you're referencing both values in your code, repetition is costly and inefficient. DRY: **D**on't **R**epeat **Y**ourself!! . Use one value (`$param`) throughout your code for consistency, readability and efficiency – Martin Jan 13 '18 at 13:53
  • ahhh duplicate,.,, I had it down as a typo.... – Martin Jan 13 '18 at 13:56

1 Answers1

0

The problem is with this line:

else if ($param = 'second')

It should be == instead of =.

hanzi
  • 2,877
  • 18
  • 26