r/PHPhelp • • 20d ago

Solved Help with handling POST parameters

I have a common file that I use for a lot of portable things. One is to grab the username and password from the form on the page of the person logging in. That code is this:

function get_param($param_name)
{
    $param_value = "";
    if (isset($_POST[$param_name])) {
        $param_value = $_POST[$param_name];
    } elseif (isset($_GET[$param_name])) {
        $param_value = $_GET[$param_name];
    }
    return $param_value;
}

Then at the login script we check what we have. It always gives an error. Here's where we try to grab that username and password:

// Gather form posts
$username = (string)get_param("username");
$password = (string)get_param("password");

In the receiving script I have tried to echo the values with a quick "exit" so I could see the values, but nothing seems to come up.

Am I missing something really basic?

5 Upvotes

21 comments sorted by

2

u/stevenreyes20 20d ago

I'm surprised nobody mentioned filter_input()

1

u/zephead98 20d ago

Hey filter_input does an awful lot that the "tosql" function was doing. Thanks for that!

2

u/colshrapnel 20d ago

I am afraid you are confusing filter_input() with something else. While "tosql" function is something you should never use. But well, I suppose you are not interested in learning programming but just want to fix some old codebase and move on, so I won't bother you with details.

1

u/zephead98 20d ago

Thank you for this. I'm just taking baby steps! I walked away from this code base many moons ago, and recently three clients contacted me with requests for updates. I couldn't believe anyone was still using this program!

So here I am, well past youthful programming age, dipping my toes back into PHP and mySQL and all that. I'm viewing it as a hobby and definitely a chance to learn.

8

u/magicmulder 20d ago

Have you tried a var_dump() on POST and GET to see what’s actually received?

3

u/swolicheert 17d ago

Always the best sanity check before digging into the helper functions

2

u/themonkeysfist 20d ago

Agree, dump is a great way to see what actually exists

7

u/martinbean 20d ago

Why don’t you just grab the POST parameter if that’s what you want? You don’t want login credentials being retrieved via GET parameters. That’s a security vulnerability and data protection breach given they can end up in plain text in log files, etc.

1

u/zephead98 20d ago

What happened was it would grap the POST or GET data, and run it into a function called 'tosql'. This function tries to strip out bad things and then query the DB for username and password validation. Like I said, the function had magic quotes and all that, which is great in PHP 20 years ago, but today guarantees failure.

Thank you again! I will be back with more questions, I am sure!

5

u/allen_jb 20d ago

Modifying values to make them safe for SQL is error-prone and unnecessary.

You should instead use prepared queries, which separate query from input data and ensures proper escaping / quoting of values.

See:

Prepared statements prevent SQL injection issues as well as query / data errors caused by incorrect escaping.

As an additional aside, checking both GET and POST data should generally be unnecessary, particularly for forms where you control the request method.

Further using GET for credentials is not recommended because they can be retained in server logs unexpectedly.

1

u/zephead98 20d ago

Thanks everyone. There was ANOTHER helper function in the common file that was using php magic quotes, which I am sure wasn't doing me any favors. I am going to try just retrieving the data instead of the helper script, but also commenting out the function that was using magic quotes (since it's part of a file that is included in more than a few scripts in this program).

1

u/flyingron 20d ago

Looks fine to me. Are you sure the form is populating those fields in the post. As stated, throwing a var_dump in there should help. Also make sure you've turned on error reporting at the top of your php page (in case they weren't on already).

You do know about the $_REQUEST superglobal right?

1

u/HolyGonzo 20d ago edited 20d ago

Use this approach:

$username = $_REQUEST['username'] ?? '';

The syntax here is:

$var = value ?? default;

The default kicks in if value isn't set / is null, and $_REQUEST is basically a merged version of $_POST and $_GET with POST taking priority by default.

2

u/FreeLogicGate 20d ago edited 20d ago

While absolutely correct, I agree with the prior advice from u/martinbean that getting values like this optionally from a get request is something to be avoided.

1

u/HolyGonzo 20d ago

Typically, yes. I was speaking mostly about the pattern.

1

u/Utaxxx1 16d ago

Since you found a magic-quotes helper in the common file: if it calls get_magic_quotes_gpc(), that function no longer exists on PHP 8, so any script that includes the file stops with a fatal error. That's worth checking first, since it can look like the form values never arrive. Turn on display_errors while you test. And since this is a login, check how passwords are stored. If they're plain text or md5, switch to password_hash() when you save them and password_verify() when you check them. You can rehash each user on their next successful login, so nobody has to reset anything.

1

u/GoldsteinEmmanuel 20d ago

You can use $_REQUEST['username'] for example to get the parameter regardless of the request method (POST or GET). By doing so you can eliminate the helper function and use $_REQUEST directly in expressions, e.g.

$username = isset($_REQUEST['username'] ) ? (string)$_REQUEST['username'] : false;

$password = isset($_REQUEST['password'] ) ? (string)$_REQUEST['password'] : false;

print_r($_REQUEST);

if($username && $password) {

exit('SUCCESS');

} else {

exit('FAILURE');

}

3

u/colshrapnel 20d ago edited 20d ago

Speaking of best practices we don't consider direct casing any good anymore. In case $_REQUEST['username'] is not string but array, is there even a slightest reason in casting it into string, getting the word "Array" and proceeding further? When $_REQUEST['username'] is not string, processing should be aborted right away.

if (!isset($_REQUEST['username']) || !is_string($_REQUEST['username']) || mb_strlen($_REQUEST['username']) > User::MAX_USERNAME_LEN) {
    http_response_code(400);
    exit('Invalid username');
}

would be much cleaner

Edit: Lol, dude banned me. O tempora, o mores. Well, just for sake of it:

A form wouldn't pass an array

Following this logic, why you're bothering with casting then?

My point was that one needn't bother to test whether the input arrived by $_GET or $_POST because $_REQUEST contains the data either way.

That's another bad practice to note. You should be certain, what method your data is getting through.

1

u/GoldsteinEmmanuel 20d ago

A form wouldn't pass an array where a string was expected by accident, and there are other methods for validating arrays, but you do you.

My point was that one needn't bother to test whether the input arrived by $_GET or $_POST because $_REQUEST contains the data either way.

2

u/davitech73 18d ago

> form wouldn't pass an array where a string was expected by accident

absolutely true. but anyone can post anything to your endpoint and it can be unexpected as well as badly formatted. this is exactly how many sqli and xss vulnerabilities are exploited. you don't want a hacking attempt to cause problems with your form handling code. so -always- validate and sanitize inputs. if it doesn't pass validation, exit. but never expect it to be properly formatted

and i personally think the request array should never be used. if your form is using get, check the get array. if your form is using post, check the post array. a hacker will put data in either or both and your form handling code should never trust it. always look where it's supposed to be instead of taking the lazy way out and looking in the request array

2

u/eurosat7 20d ago

Don't help with php 3 style code, please.

And I do not mean the mentioning of $_REQUEST.