/    Sign up×
Community /Pin to ProfileBookmark

How To Check Url Params With Min Code Possible ?

Folks,

My original url is this:
https://localhost/Templates/Pagination_Template.php

You make a METHOD=GET form submission to do a keyword search on my database and you get forwarded to:
https://localhost/Templates/Pagination_Template.php?find=keyword&table=links&column=keyword&max=25

Now, I have code to check if the url contains the “table” and “column” params or not.
I am checking like this the lengthy way ….,

[code]
//SECTION: WHITE-LISTS.
//Valid list of Mysql Tables.
$tables_white_list = array(‘sale’,’sold’,’links’);
//Valid list of Mysql Table Columns.
$columns_white_list = array(’email’,’domain’,’url’,’anchor’,’description’,’keyword’);

//SECTION: VALIDATE SERP URL.
//Check if “table” exists or not in Url’s Query String.
if(!empty(trim($_REQUEST[‘table’])) && is_string(trim($_REQUEST[‘table’])))
{
if(in_array(trim($_REQUEST[‘table’]),$tables_white_list)) //MySql Tbl to Search.
{
$table = trim($_REQUEST[‘table’]);
}
else
{
die(‘Invalid Table!’);
}
}
else
{
die(‘Select Table!’);
}

//Check if “column” exists or not in Url’s Query String.
if(!empty(trim($_REQUEST[‘column’])) && is_string(trim($_REQUEST[‘column’])))
{
if(in_array(trim($_REQUEST[‘column’]),$columns_white_list)) //MySql Tbl Col to search.
{
$column = trim($_REQUEST[‘column’]);
}
else
{
die(‘Invalid Column!’);
}
}
else
{
die(‘Select Column!’);
}
[/code]

Can you see the two sets of IFs doing the checking here ? This is too much code. Let’s try cutting down on it by adding the “table” and “column” onto an array and then looping through the array to check if the array values (eg. “table”, “column”) exists in the url or not. How to do this your shortened way ?
I am trying to shorten it and if I manage it then I will mention here but you are a pro and can shorten it to the min and I want to learn your way. So, what’s your shortened way ?
Maybe we make use of parse_str() and/or http_build_query() ? I am new to these two functions and are experimenting to learn them. Still struggling. hence, you don;t see any codes here so far. But when I manage to draw up any then I will post here. But in the meanwhile, can you beat me to it ?

Maybe, I get rid of these …
**&& is_string(trim($_REQUEST[‘table’]))**

**&& is_string(trim($_REQUEST[‘column’]))**

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 25.2021 — Probably all you need:
[code=php]
$errors = [];
foreach(['table', 'column'] as $key) {
if(!isset($_REQUEST[$key]) or trim($_REQUEST[$key]) === '') {
$errors[] = "Invalid ".ucfirst($key)."!";
}
}
if(!empty($errors)) {
die("<p class='error'>".implode("<br>", $errors)."</p>");
}
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorJul 25.2021 — @NogDog#1634711

Should not ucfirst() be here also ...
<i>
</i>if(!isset(ucfirst($_REQUEST[$key])) or trim(ucfirst($_REQUEST[$key]))=== '')


Q1. Yes or no ?

And did we not talk earlier that it's no good checking for both ISSET and empty() value ? Hence, should we shorten it further to:
<i>
</i>$errors = [];
foreach(['table', 'column'] as $key) {
if(trim($_REQUEST[$key]) === '') {
$errors[] = "Invalid ".ucfirst($key)."!";
}
}
if(!empty($errors)) {
die("&lt;p class='error'&gt;".implode("&lt;br&gt;", $errors)."&lt;/p&gt;");
}


Q2. Yep or nope ? If yep, then would not be better or not ...
<i>
</i>$errors = [];
foreach(['table', 'column'] as $key) {
if(trim($_REQUEST[$key]) === FALSE) {
$errors[] = "Invalid ".ucfirst($key)."!";
}
}
if(!empty($errors)) {
die("&lt;p class='error'&gt;".implode("&lt;br&gt;", $errors)."&lt;/p&gt;");
}


Q3. Yessy or noowee ?

I need 3 answers from you here, NogDog. That should remove all confusion from my mind.

Q1. Yes or No.

Q2. Yep or Nope.

Q3. Yessy or noowee.
Copy linkTweet thisAlerts:
@NogDogJul 26.2021 — > @developer_web#1634725 Should not ucfirst() be here also

Nope, I was just using it to make the error message format match what you used originally.

> @developer_web#1634725 And did we not talk earlier that it's no good checking for both ISSET and empty() value

emtpy() gets messy in certain situations:
<i>
</i>$ php -a
Interactive shell

php &gt; $foo = '0';
php &gt; $result = empty($foo);
php &gt; var_export($result);
true
php &gt; $foo = '1';
php &gt; $result = empty($foo);
php &gt; var_export($result);
false

So empty() may not be appropriate if it's possible for a valid input to be considered "falsey" by PHP.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog#1634711

Ok. Thanks.

Look what I did to your code:
<i>
</i>$errors = [];
foreach(['table','column','find'] as $key)
{
if(!isset($_REQUEST[$key]) or trim($_REQUEST[$key]) === '')
{
$errors[] = "Invalid ".ucfirst($key)."!";
}
else
{
${$_REQUEST[$key]} = $_REQUEST[$key];
echo '&lt;b&gt;' .__LINE__ .'&lt;/b&gt;'; echo '&lt;br&gt;';
echo $table; //See Error: Notice: Undefined variable: table in
echo $column; //See Error: Notice: Undefined variable: column in
echo $find; //See Error: Notice: Undefined variable: find in
}
}
if(!empty($errors))
{
die("&lt;p class='error'&gt;".implode("&lt;br&gt;", $errors)."&lt;/p&gt;");
}


Why I get the errors ?

**Notice: Undefined variable: table in C:xampphtdocsTemplatesPagination_Template.php on line 136

Notice: Undefined variable: column in C:xampphtdocsTemplatesPagination_Template.php on line 137

Notice: Undefined variable: find in C:xampphtdocsTemplatesPagination_Template.php on line 138**

This line:

${$_REQUEST[$key]} = $_REQUEST[$key];

should've created the 3 variables: $table, $column, $find.

Why did not it ?
Copy linkTweet thisAlerts:
@NogDogJul 30.2021 — > @developer_web#1634887 should've created the 3 variables: $table, $column, $find.

Nope. It would create a single variable named with the _value_ of that array element. If you want to use the array key, just do $$key = $_REQUEST[$key]; However, because you're looping through one key at a time, all three would not be ready for output until after the loop is completed -- and only if all were found. (Plus, assigning each of those array elements to new variables seems like an unnecessary duplication of data in your script, and a potential for confusion later.)
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog#1634890

Mmm. Then do you smell there is a fix to this which you deem would be worthy for me to learn from you ?
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog

This seems to be working but I'd rather learn your recommended code, if you got any.
<i>
</i>
//SECTION: WHITE-LISTS.
//Valid list of Mysql Tables.
$tables_white_list = array('sale','sold','links');
//Valid list of Mysql Table Columns.
$columns_white_list = array('email','domain','url','anchor','description','keyword');
//Banned Words List. Users cannot search these keywords.
$blacklisted_words = array('prick','dick');
$params_white_list = array('table','column','find');

$errors = [];
foreach($params_white_list as $key)
{
if(!isset($_REQUEST[$key]) or trim($_REQUEST[$key]) === '')
{
$errors[] = "Invalid ".ucfirst($key)."!";
}
else
{
$$key = $_REQUEST[$key]; echo '&lt;br&gt;';
echo '&lt;b&gt;' .__LINE__ .'&lt;/b&gt;'; echo '&lt;br&gt;';
}
}

echo $table;
echo $column;
echo $find;

if(!empty($errors))
{
die("&lt;p class='error'&gt;".implode("&lt;br&gt;", $errors)."&lt;/p&gt;");
}
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog

You used the foreach loop. But, is there a way to further shorten the code by using something else other than the foreach loop ? If I come up with any code then I'll mention it here, if I remember this post. But if you come-with any do then remember this thread.
Copy linkTweet thisAlerts:
@developer_webauthorJul 30.2021 — @NogDog

I learnt this from your code. Did not know you can add array like this inside the foreach loop:

**['table','column','find']**
<i>
</i>foreach(['table','column','find'] as $key)


I used to do like ...
<i>
</i>foreach($keys as $key)


Now you understand why I pester you folks for code samples ?

Now, do you want me to end my pestering or do you want me to aggravate it even further if "aggravate" is the right word ?
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...