Jump to content

How to stop users from editing a dropdown value

Mornincupofhate

So in HTML, If I'm not mistaken, the user can inspect element the input dropdown and change it to any value they'd like. How do I keep this from happening? Or am I mistaken and this doesn't work?

 

Please no javascript; PHP only.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Mornincupofhate said:

So in HTML, If I'm not mistaken, the user can inspect element the input dropdown and change it to any value they'd like. How do I keep this from happening? Or am I mistaken and this doesn't work?

 

Please no javascript; PHP only.

From my understanding, if you edit the html through inspect element its only client side. I could be wrong but this has been my experience. 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, HZapperz said:

From my understanding, if you edit the html through inspect element its only client side. I could be wrong but this has been my experience. 

Just tried it on my own page. Can confirm it does change the value to whatever you set it to be.

Link to comment
Share on other sites

Link to post
Share on other sites

Where ever you are POSTing the values to.. setup an IF check that will throw an error if the valid values that you want are not the ones submitted.

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, unijab said:

Where ever you are POSTing the values to.. setup an IF check that will throw an error if the valid values that you want are not the ones submitted.

So what do I do? Just something like this?

 

If ($_POST['someshit'] != "value1" or "value2" or value"3") { die(); }

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mornincupofhate said:

So what do I do? Just something like this?

 

If ($_POST['someshit'] != "value1" or "value2" or value"3") { die(); }

Also, what if I'm checking an entire array?

Link to comment
Share on other sites

Link to post
Share on other sites

Im too rusty with PHP to remember much more off the top of my head, haven't used it for about 2 years.

 

Good luck on your search.

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, unijab said:

Im too rusty with PHP to remember much more off the top of my head, haven't used it for about 2 years.

 

Good luck on your search.

Lol ty

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Mornincupofhate said:

What if I'm checking against an entire array?

loop the array, check if element is valid.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

You are looking for input validation, as the frontend can always be manipulated and all requests can be forged.

As a rule of thumb: never trust user data, never correct user data! So yes, frontend validation should be done but only to help a (normal) user correcting his input, never assume under any circumstance, that the data from the user can be trusted ;) 

 

So in your case, a very simple solution:

<?php
	/* For Try Catch Block */
    class ValidationException extends \Exception {}	

    try{
        /* Validation */
        $dropdown_input = $_POST['awesome_dropdown'];
        $possible_values = ['Value1','Value2'];
      
        if( ! in_array($dropdown_input,possible_values) ){
            throw new ValidationException('Dropdown value incorrect');
        }
    }
    catch(ValidationException $exception){
        /* Something is not valid! */
        die($exception->getMessage());
    }

    /* Your Input is now Valid! */
    // ...
	

	

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

$array = ['blue', 'orange', 'red'];

if (in_array($_POST['colors'], $array)){
	//code
}

Something more ugly but still works would look like.

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Cruorzy said:

$array = ['blue', 'orange', 'red'];

if (in_array($_POST['colors'], $array)){
	//code
}

Something more ugly but still works would look like.

i like this better is super clear what the code is meant to do. Plus no try / catch. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, vorticalbox said:

i like this better is super clear what the code is meant to do. Plus no try / catch. 

I disagree, I prefer using Try Catch block for validation because it breaks and moves to the catch statement. So you can handle more complex validations in specific methods and still have it clean. 

On the other hand, if else relies on you pretending when an exception has occurred. For one single validation I agree, If else is fine, but if you use more complex and bigger validations I would go with try catch...

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, leodaniel said:

I disagree, I prefer using Try Catch block for validation because it breaks and moves to the catch statement. So you can handle more complex validations in specific methods and still have it clean. 

On the other hand, if else relies on you pretending when an exception has occurred. For one single validation I agree, If else is fine, but if you use more complex and bigger validations I would go with try catch...

why? You do an Is and get a true or false. Catching tries code then fails. I would rather have code that doesn't rely on breaking to verify input. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

why? You do an Is and get a true or false. Catching tries code then fails. I would rather have code that doesn't rely on breaking to verify input. 

Because I don't want any further Execution if the validation fails... why? Because in my opinion you should never correct any user input... the input either comes in the desired form or you reject it and don't execute your code... in my opinion you expect the values to have a certain form and so it should throw an exception if they don't

 

The good thing about Exceptions is that they immediately stop further execution and kick off the error handling (up the call stack until they are catched)

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×