r/PHPhelp • u/zephead98 • 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
2
u/stevenreyes20 20d ago
I'm surprised nobody mentioned filter_input()
8
u/magicmulder 20d ago
Have you tried a var_dump() on POST and GET to see what’s actually received?
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!
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:
The syntax here is:
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.
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');}