Skip Navigation

[Resolved] PHP error occurring on redirect after login form

This thread is resolved. Here is a description of the problem and solution.

Problem:

Use PHP function str_contains() in custom PHP codes.

Solution:

The PHP function str_contains() was introduced in PHP 8.0, but your webserver is using PHP 7.4.3, you can try the workaround here:

https://toolset.com/forums/topic/php-error-occurring-on-redirect-after-login-form/#post-2218165

Relevant Documentation:

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by simonM-5 2 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2217839

Hi Support

We are recently receiving the following PHP errors pertaining to the Toolset-provided code snippet "conditional-redirect-on-successful-login" in a previous ticket. This worked fine before. The error is preventing any new users from being logging in successfully on the site.

ERROR MESSAGE:

Fatal error: Uncaught Error: Call to undefined function str_contains() in /.../dev/wp-content/toolset-customizations/conditional-redirect-on-successful-login.php:64 Stack trace: #0 /.../dev/wp-includes/class-wp-hook.php(303): my_login_redirect('hidden link...', 'hidden link...', Object(WP_User)) #1 /.../dev/wp-includes/plugin.php(189): WP_Hook->apply_filters('hidden link...', Array) #2 /.../dev/wp-login.php(1178): apply_filters('login_redirect', 'hidden link...', 'hidden link...', Object(WP_User)) #3 {main} thrown in /.../dev/wp-content/toolset-customizations/conditional-redirect-on-successful-login.php on line 64

Our original ticket for the redirect code snippet was this one:
https://toolset.com/forums/topic/conditional-login-in-different-languages-with-wpml-enabled/

and a subsequent tickets addressed an issue while logging in in German:
https://toolset.com/forums/topic/conditional-redirects-not-working-for-all-roles-in-german/

whereafter it worked too. So I don't understand why it has stopped working all of a sudden as we didn't change that code since.

The issue does not occur for successfully registered users of role "native_nanny" or "family", so I suspect that the redirect issue is only for brand new users (roles "unverified_family" and "unverified_nanny"), who would be redirected to a different place, as given by the Toolset Code Snippet "conditional-redirect-on-successful-login".

STEPS TO REPRODUCE:

1) Click either "Sign Up" to register as a new Unverified Family or "Become a Native Nanny" to register as a new Unverified Nanny
2) Fill out the email, first name, last name, password and do reCaptcha if necessary.
3) You are redirected to the login form. Once you enter the email address and password just specified the error occurs.

I found during testing that if you follow the following steps, you will see that the login was successful, but the redirect is failing.

4) Immediately after you get the error, click Back on the browser
5) You are taken back to the login screen and it looks like you're not logged in, however if you simply click the WPML language flag top right, you will see that the login works and you ARE redireted to the login page in German, but you are acutally logged in, as can be seen in the menu top right. It has changed to the menu for logged in users.

Thanks and best regards
Simon

#2218165

Hello,

Please check PHP document:
hidden link
The PHP function str_contains() was introduced in PHP 8.0, but your webserver is using PHP 7.4.3
hidden link
So it is expected result to get the PHP errors you mentioned above.

I have done below modifications in your website:
Edit the custom codes "onditional-redirect-on-successful-login.php", lines 8~13, add below codes:

if (!function_exists('str_contains')) {
    function str_contains(string $haystack, string $needle): bool
    {
        return '' === $needle || false !== strpos($haystack, $needle);
    }
}

Please test again, check if it is fixed.

#2219289

Hi Luo

Thanks, that's working again, however according to Toolset website: https://toolset.com/toolset-requirements/, PHP 8 isn't supported (yet).

"Important notes:
PHP 8 is currently not supported due to some issues. We’re working on fixing this. In the meantime, please use PHP 7 version"

Is this still accurate? Is it recommended to upgrade to PHP 8?

Kind regards
Simon

#2220857

Yes, you are right, it is not to use PHP 8.0 with Toolset plugins yet.

And there is a misunderstanding, the custom codes I provided above is a custom PHP function, it should be able to works in both PHP 7.4 and 8.0, see the first line:
if (!function_exists('str_contains')) {

That means if you are using PHP 8.0, then these codes won't take effect, if it is PHP 7.4, then it takes effect.

#2221265

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.