Some PHP Function Flagged as Error in VSCode - php

I am a beginner and am developing a specific web system using PHP 8.2, Codeigniter 4 and XAMPP in VSCode. However, in my Controller, some PHP functions are marked as errors, such as password_verify(), strpos() etc. After I run it, the program runs fine. Where is the error? Is it in the VS Code program?
The problem is:
Expected type 'string'. Found 'array|null'.intelephense(1006)
For additional information, I am using Intelephense PHP extension 1.9.2. I also opened the code in PHPStorm and the error is not there. Thank you for any provided information.
When I uninstalled / disabled the Intelephense PHP, of course the error was not found. Whe I use PHP IntelliSense, it's did not even mark the function as an error. However, when Intelephense was re-enabled the PHP function was still marked as an error.

I see that $request->getPost() has a signature of mixed|null.
And password_verify a a signature of string.
You can cast it to string and the warning will dissapear.
$password = (string) $this->request->getPost('password')

Related

Mcrypt issue with PHP, Nginix, Ubuntu

I am working on an app where we are sharing login/session across the sub domains. To do this, we are generating some encrypted information but we are facing issue with mcrypt. Our code stuck on a point and never comes back unless the time out occurs.
To encrypt data we use following function
$cipher = new Cipher(env('CYPHER_KEY'));
return $cipher->encrypt($str);
And on encrypt function of Cipher class, the very first line is creating problem that is:
$iv = mcrypt_create_iv($this->iv_size);
This is a php function of mcrypt library. $this->iv_size contains 16. When execution comes to this line, it never comes back and it doesn't throw any error, exception in any log. It just stuck here unless ngnix throws time out error.
I am using Laravel 5.2 (Php5, php-fpm), ngnix and Ubuntu 14.04. I have already installed and enable mcrypt extension and I can see it when I do php -m.
Can any body please guide me what I am doing wrong here or how can I even get some error string to debug/fix.

Is PHP ArgumentCountError server specific?

I am facing very weird problem since last two days, I have two different servers one is windows(php v5.3) and second is linux(php v7.1 ngnix). I have a project(written by someone) which is already running on windows server, now I have to move this project on linux server (with no code change). My issue, There is a login function -
Function userAuthentication($username,$password,$locationId,$url,$source)
{
....
}
And It is calling as (On windows server)
userAuthentication($username, $password,'',$url) ;
I know parameters are mismatched in this case but as I tested, this code is actually giving me result and working fine on windows but that same code is giving me obvious error on Linux server-
ArgumentCountError: Too few arguments to function
Authentication::userAuthentication(), 4 passed in
It can be resolve by changing that code but I don't want (can to be many function with same error).
I already googled but not found anything. I am very curious to know Why this is not happening on windows. Is that ArgumentCountError server specific or windows server not taking it as critical as linux, or php version issue or any other config.? what I suppose to fix to resolve this issue.?
Anyone have any idea about that please help me out, Thanks
The difference isn't in the Environment (windows vs. linux) rather the PHP version.
As you can see in the migration from PHP 7.0 to 7.1 (link: http://php.net/manual/en/migration71.incompatible.php)
Throw on passing too few function arguments
Previously, a warning would be emitted for invoking user-defined
functions with too few arguments. Now, this warning has been promoted
to an Error exception. This change only applies to user-defined
functions, not internal functions. For example:
<?php
function test($param){}
test();
The above example will output something similar to:
Fatal error: Uncaught ArgumentCountError: Too few arguments to
function test(), 0 passed in %s on line %d and exactly 1 expected in
%s:%d
How to solve it?
You'll have to define all the parameters in the function. It might be "uncomfortable" but it's a better practice as a developer. Don't be lazy regarding this issue.
This has everything to do with the newer PHP versions that are (as they should) be more strict about calling. If you want this to work again make the additional parameters optional by giving them default values.
You should update to 7.x locally to prevent this sort of issues.
Like:
Function userAuthentication($username,$password,$locationId,$url,$source = '')

is there a difference in php with defines from version 5 to version 7?

I migrated to godaddy for a perma-host. Now much of my code is not working.
For years I have used the following code in my gaming site. but now I want to move it to a more permanent host. I've been using php v5 with no issues, never had a reason to change. However I did finally migrate to PDO. With that said, here is the .ini file I use:(keep in mind, it is a giagantic ini file, but the following are what is important that is NOT working..
SQL_TYPE=mysql
HOST=localhost
USER=incognito
PASS=topsecret
DATABASE=mycooldb
to pull it for defines:
$defines = parse_ini_file('defines.ini');
foreach($defines as $field=>$data)
define($field, $data);
Now there is an error when I use godaddy (which i believe is up to v7 php).
If I run
die('host = '.HOST) // gives HOST
On my local server
die('host = '.HOST) // gives localhost - as it should
Why is this happening? I supposed I could line by line the defines, but they have worked for years the way I have it.
Thanks for any input.
Since it appears I have 2 problems, I will answer what addresses my first issue for this topic and close it:
There was not a problem with the DEFINES, but instead, my .ini files that stored the information for the variables that create the DEFINES.
In this ini file, I originally had "#" for comments as was accepted at the time, (which was what I was used to see: https://en.wikipedia.org/wiki/INI_file#Format)
However, not catching some of the "depreciation errors" I got over the years from PHP v5.3 - as I was stubborn changing - "#" was among those warnings. When PHP 7 finally came, the "#" no longer worked as comments, and thus my DEFINES variable ceased to work, as the script was stuck at the first commented line.
Conclusion: As far as my error, there is NO difference in the defines between PHP: v7 and v5, however the use of "#" as comments caused the error.
Thank you #Magnus Eriksson and #Calimero for the tip to display errors to catch it.

PHP version 5.6.17 unexpected T function/anonymous function error

I have a local Drupal site running with MAMP on Win 7 with php 5.6.13. My live site has php 5.6.17. I have no problems on the local site, but on the live site I get unexpected T function error with the following code which I downloaded from drupal.org.
function redhen_activity_message_types() {
$message_types = array_keys(message_type_load());
$filtered = array_filter($message_types, function($var) {
return strpos($var, 'redhen_') !== FALSE;
});
return $filtered;
}
The line beginning with $filtered is the one that is pointed to by the error message.
I know little about php, but have been searching the web and found something called anonymous functions not working on older versions of php. Is the slightly different versions of php causing this problem? How do I fix it?
Thanks!
The anonymous function is the function that's defined starting in that line of code:
function($var) {
return strpos($var, 'redhen_') !== FALSE;
}
I don't see anything obviously wrong with that function, and Redhen CRM gets enough use that others are likely to have run into problems already if it were clearly incorrect. And while it's always good to test under the same version as your production environment it's unlikely that there was bug introduced to PHP between 5.6.13 and 5.6.17 that's causing this problem.
To find the root cause try a few things:
Check to make sure the version of PHP you think is running in production is actually running. I've seen several environments recently where people had a different version running under php-cli than on the web server, meaning that running php --version on the command line gave the wrong answer. Use a phpinfo() call to verify the version.
Check the Redhen issue queue for other people with the same problem.
Update your test machine to match production. Once you're sure production is the version you think it is, set your dev environment to match and see if the problem comes back. If it does, try to move forward to a more current minor version and see if the error goes away again.

PHP error after new install of xampp

I was recently switched computers at work, and am trying to get all my projects up and running again. I am working on testing some php/html/javascript pages for a website using a local xampp server. I had everything working properly on the only computer, but when I try to load the pages now I get the following error:
Uncaught SyntaxError: Unexpected token <
I have tried a php-only test page, so I know the sever is capable of parsing php.
I also know it is not the short tag issue, as all of the php code is wrapped in
<?php...?>
Are there any other configuration settings that would be causing this issue?
The error message doesn't look like a PHP error. Check this discussion maybe you can find the solution there.
It seems the error is reported by Google Chrome when it expects to parse a .js file and it finds HTML (or PHP?) instead.
I think it is a JavaScript error, you should check your js code, not php.
The following reasons might cause this error:
href attribute value "javascript: void ()", no brackets add "0"
Button // error
Button // correct
Non-compliant JSON string
etc.

Categories