URL GET variable disappear - php

I have a simple page to print some data from database.
I use php GET variable, (I know its deprecated, but it's work on local network).
It's working perfectly but if 'valami' length is bigger the 'valami' variable disappearing. When the length is 1061 it does not work
In the php.ini:
max_input_vars=3000
Also the aid variable from the end of the Querystring does get seen by my code? So it does not appear to be a querystring length issue.
What is the problem?
PHP5
print_fizet.php?valami=28925|28926|28927|28928|28929|28930|28931|28932|28933|28934|28935|28936|28937|28938|28939|28940|28941|28942|28943|29055|29056|29057|29058|29059|29060|29061|29062|29073|29074|29075|29076|29077|29078|29079|29080|29092|29094|29095|29096|29097|29098|29099|29100|29101|29102|29103|29104|29105|29106|29107|29108|29109|29110|29111|29125|29142|29143|29144|29145|29146|29150|29151|29152|29168|29169|29170|29171|29172|29173|29174|29175|29176|29177|29178|29179|29180|29181|29182|29183|29184|29185|29186|29187|29188|29189|29190|29191|29192|29193|29194|29195|29196|29197|29198|29199|29200|29201|29202|29203|29204|29205|29206|29207|29208|29209|29210|29211|29212|29213|29230|29231|29232|29259|29260|29264|29265|29270|29281|29291|29292|29299|29300|29301|29303|29318|29339|29345|29346|29347|29348|29349|29350|29351|29352|29353|29360|29361|29367|29368|29369|29370|29371|29385|29386|29387|29388|29408|29409|29410|29411|29412|29413|29434|29435|29436|29490|29491|29508|29519|29523|29524|29525|29526|29527|29533|29534|29535|29545|29546|29547|29548|29549|29550|29551|29552|29554|29581&aid=23
UPDATE
When I do a
var_dump($_GET);
The output is
array(1) {
["aid"]=> string(2) "23"
}
All I get is the variable from the end of the querystring
When I make a shorter valami variable It works:
array(2) {
["valami"]=> string(509) "29197|...|29581"
["aid"]=> string(2) "23"
}

It appears you're hitting a limit for maximum length of a GET variable - possibly 1024. This is most likely due to the PHP version you use. I've found this note on php.net that may be of use to you:
https://www.php.net/manual/en/reserved.variables.get.php#101469

You can update max_input_vars length from 1000 to 3000 and so on as you want. Try below code to increase input vars length.put this code on starting of the script.
ini_set('max_input_vars', 3000);
I hope this will helpfull for you.

Related

$_GET variables being passed with empty string values

I have my local test server and my running server. On the test server, I use this url:
http://localhost/?&start[]=0&duration_[]=2&name[]=David&setting[]=26&start[]=6&duration[]=18&name[]=john&setting[]=26
From this URL, I use a for loop to run through the array:
$num = count($_GET['start']);
if (isset($_GET['start']) && count($_GET['start']))
{
for($i=0;$i<$num;$i++)
{
echo "Time: " . intval(trim($_GET['start'][$i]));
}
}
This works like a charm. However, when I run it on my actual server, I get all the variables returning as empty strings... Here is the var_dump:
["start"]=>
string(0) ""
["duration"]=>
string(0) ""
["name"]=>
string(0) ""
["setting"]=>
string(0) ""
Anyone have any idea what's happening here? I've put the same for-loop test code from above and I'm obviously not getting anything besides: "Time: "
The problem wasn't with the code or the $_GET method or the difference of servers (well a slight difference). I didn't realize that when I was using filter_var() on my main server, my arrays were being turned into strings. This bit of the sanitization code that caused the problem was not on my test server. As per the php documentation:
Value to filter. Note that scalar values are converted to string internally before they are filtered.
This is the first time I've made a function using the array capabilties of the $_GET variable so I didn't realize that when sanitizing, I was sanitizing each GET variable where in this new instance, I was sanitizing each GET array, rather than GET value.
The code I was using was:
$get_arr = $var_arr['_GET'];
$get_key = array_keys($get_arr);
$_GET[$get_key[$g_a]] = trim(filter_var($_GET[$get_key[$g_a]], FILTER_SANITIZE_SPECIAL_CHARS));
The code that I changed it to
$get_arr = $var_arr['_GET'];
$get_key = array_keys($get_arr);
$_GET[$get_key[$k]][$i] = trim(filter_var($_GET[$get_key[$k]][$i], FILTER_SANITIZE_SPECIAL_CHARS));
It's only a slight distinction, but enough of one to have me debugging this for a couple of days. Hopefully this helps someone else along the way.
Remove the [] after your vars.
http://localhost/?start=0
instead of
http://localhost/?start[]=0
your passing [] as your string so it spitting that out, remove the [].

var_dump(json_encode($data)) returns bool(false)

After getting result from Sql query, I stored my result in a $data array, which after var_dump($data) looks somewhat like this:
array(100) {
[0]=>
object(stdClass)#3 (3) {
["qid"]=>
string(2) "19"
["q_no_on_paper"]=>
string(1) "0"
["question_text"]=>
string(139) " Consider the following statements- 1- The Centre recently unveiled the expanded version of the. . ."
}
[1]=>
object(stdClass)#4 (3) {
["qid"]=>
string(2) "16"
["q_no_on_paper"]=>
string(1) "0"
["question_text"]=>
string(138) ". There is dispute over the Tipaimukh hydraulic project between India and A. Bhutan B. Nepal C.. . ."
} ...
The array is long, so this is just a part of it.
Now when I do json_encode($data); , and then var_dump it, I get bool(false).
I tried to use json_last_error() after following the Example #1 in this PHP documentation link , it shows - No errors
Where am I going wrong? Why is it not encoding it?
EDIT:
The ["question_text"] contains some html text.
This may help you out,
Although this is not documented on the version log , non-UTF8 handling behavior has changed in 5.5, in a way that can make debugging difficult.
Passing a non UTF-8 string to json_encode() will make the function return false in PHP 5.5, while it will only nullify this string (and only this one) in previous versions.
In a Latin-1 encoded file, write this:
<?php
$a = array('é', 1);
var_dump(json_encode($a));
?>
PHP < 5.4:
string(8) "[null,1]"
PHP >= 5.5:
bool(false)
PHP 5.5 has it right of course (if encoding fails, return false) but its likely to introduce errors when updating to 5.5 because previously you could get the rest of the JSON even when one string was not in UTF8 (if this string wasn't used, you'd never notify it's nulled)
So you may encountered the later example of PHP >= 5.5
See at php.net documentation http://php.net/manual/en/function.json-encode.php#115733
I see some white space/HTML characters (like \n, \t, \r) in var_dump($data) result. This makes JSON invalid and hence json_encode returns false. Check the following index in your array...
["question_text"]=> string(139) " Consider the following statements-

Different server, different datatype with the exact same code

I'm running a code getting data from a DB server, everything is working fine locally. But when I push it online, it's not working anymore.
I think I know where the problem comes from. Here is the data I get locally:
object(stdClass)#451 (14) {
["matter_created_actionstep_c"]=>
string(1) "1"
["trust_receipt_sent_c"]=>
string(1) "1"
["scope_complete_c"]=>
string(1) "0"
["transferred_trust_to_current_c"]=>
string(1) "0"
}
And when I push it on my server, here is the result I get:
object(stdClass)#451 (14) {
["matter_created_actionstep_c"]=>
int(1)
["trust_receipt_sent_c"]=>
int(1)
["scope_complete_c"]=>
int(0)
["transferred_trust_to_current_c"]=>
int(0)
}
Are you aware of any apache configuration that would lead to this typeset change?
The only project file that is different, is the conf file:
locally:
DB_HOST=1.1.1.1
on the server:
DB_HOST=localhost
Thanks in advance.
This can result from a lot of issues. Maybe a different PHP version, maybe the data comes from a database and the version of the database driver is different, …
I would say the application is to blame. PHP is untyped, meaning in most cases nobody cares for the actual type of a variable. In your case barely anybody cares if the value is 2 or "2" as long as it behaves the same (e.g. 2==2 equals 2=="2"). If the application requires certain types in some variables, it should assure the variables contain these types! This is not the case here.
Check the application and the part of the code which writes the value into the object. This part of code should cast the value to the desired type before putting it into the variable.
Nevertheless (or if the object comes from somewhere else) it may make sense to make the code more forgiving concerning types, i.e. do not rely on a special type as long as it is not necessary. In my experience there are only few cases when type of variables really matter.

"Computed" key from Sql server scalar functions

I have a project that uses MSSQL over pdo_dblib and freetds. MS scalar functions always returned their data in this format:
array(1) { [0]=> array(1) { ["computed"]=> string(3) "922" } }.
But now, on one of the servers the format suddenly is:
array(1) { [0]=> array(1) { [""]=> string(3) "922" } }.
So the key in the array became empty instead of "computed".
I know that I can change that key in my select statements by adding "as" clause. Still, the question is, what controls the default key?
Both servers use the same database.
As far as I know, this "computed" key is something that's added by pdo_dblib.
PHP version is different between servers, the one with computed has old 5.3, while the one with empty key has 5.5. But I think that this server had 5.5 for quite some time, while the computed key disappeared just yesterday. Not 100% sure though...
In the end I found out that this happens because there was a change in this commit to pdo_dblib. The motivation for this change was that it was clogging up memory.
It should be noted that the version that you get from pecl is weird. It has "computed" in dblib_stmt.c source, but still does not use it.
The version that works, is the one bundled with php sources. I was able to take sources from php-5.3.29\ext\pdo_dblib\ and build them against 5.6.4.

PHP, pass parameters from command line to a PHP script

I want to pass parameters from PHP Command Line Interface, and then read in the values using PHP script, something like this:
<?php
$name1 = $argv[1];
echo $name1;
?>
I pass the variable from CLI like this:
C:\xampp\php\php.exe name.php Robby
The above works, I get Robby as the output.
But I want to do something like this:
C:\xampp\php\php.exe name.php -inputFirstName="Robby"
So that the user is well informed to enter the correct parameters in the correct places. What is the appropriate way to parse these parameters?
When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:
<?php
var_dump($argc); //number of arguments passed
var_dump($argv); //the arguments passed
?>
Like this:-
php script.php arg1 arg2 arg3
Will give the following output
int(4)
array(4) {
[0]=>
string(21) "d:\Scripts\script.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
See $argv and $argc for further details.
To do what you want, lets say
php script.php arg1=4
You would need to explode the argument on the equals sign:-
list($key, $val) = explode('=', $argv[1]);
var_dump(array($key=>$val));
That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.
I use this fairly concise method:
if($argc>1)
parse_str(implode('&',array_slice($argv, 1)), $_GET);
Which would handle a call such as:
php script.php item1=4 item2=300
By sending it into $_GET you automatically handle web or CLI access.
For commentary, this is doing the following:
If the count of arguments is greater than one (as first item is the name of the script) the proceed
Grab the arguments array excluding first item
Turn it into a standard query string format with ampersands
use parse_str to extract to the $_GET array
While the answer is correct and you could do the parsing by hand, PHP also offers the getopt() function that might actually provide useful here.
There's also object-oriented alternatives (written in PHP, available in a number of libraries) that might turn out to be what you need. Googling for "php getopt" will yield helpful results.
you can send parameters as one argument then parse that argument like a $_GET array
C:\xampp\php\php.exe name.php "inputFirstName=Robby&LastName=John"
and in your PHP file
if (!empty($argv[1])) {
parse_str($argv[1], $_GET);
}
you'll get arguments in $_GET array like usual
The getopt() function is probably the most correct answer in the case of the question. Especially since it was made platform independent with PHP 5.3. In the particular case of this question and parsing multiple parameters, one way to leverage this function would be as follows:
$defaultValues = array("inputFirstName" => "");
$givenArguments = getopt("", array("inputFirstName:"));
$options = array_merge($defaultValues, $givenArguments);
$inputFirstName = $options['inputFirstName'];
The call to set $inputFirstName with the value "Robby" would be:
> php script.php --inputFirstName="Robby"
Explanation
Default values for all expected parameters are set in the $defaultValues array. Input sent through via command line arguments are collected by PHP's getopt function and stored by the $givenArguments. Note that the colon (:) at the end of the "inputFirstName:" string indicates that this is a required argument. Without a colon here, only the presence of the argument would be detected, not the actual value (more information in the PHP Manual). Merging these two arrays together on the third line results in array with all expected parameters being set with either default values or arguments provided from the command line/terminal if they are available.
I don't know if at the time this question has being asked what i going to answer to it was available but if you call php-cgi -f myfile.php var=something you can retrieved whit $var=$_GET['var']; from the command line then you don't have to change your code to call it from the web browser or the command line
If you don't mind using a library, I suggest you take a look at The Console Component by Symfony.
It can be used to create command line applications and supports the use of Arguments & Options.
The documentation page contains a couple of excellent examples to get you started.
Of course under the hood it uses the same techniques as explained by vascowhite.
your best hope is to use
exec("php -f php.file.php example=js.json > ech0");
echo file_get_contents("ech0");
unlink("ech0");
That's what I use in PipesJS
You can parse the user input on your program looking for specific strings such as -inputFirstName="x" (using regular expressions, for example) and then set the correct variable to x.

Categories