Question

I have a string that contains the paid modules of a user. If the module is not part of the string, an error page should be shown.

The variable $paidModules contains reminder newsfeed system finance

This is the PHP part where I check if the user paid for the module. If that is not the case, the noAccess.php page should be shown.

if($paidModules != null & $paidModules != "" ) {
    if (strpos($paidModules,'reminder') == false) {
        include('noAccess.php'); 
        return;
    }
}

The problem is now, even though the variable $paidModules contains reminder, the noAccess.php is shown. What am I doing wrong?

Was it helpful?

Solution 2

strpos() may return a boolean false. It also may return 0 as the position found.

As such you need to use strict equality:

if (strpos($paidModules,'reminder') === false) {
  // code...
}

In your case because reminder is found in the zero position. Using ==, PHP type-juggles 0 == false as true.

OTHER TIPS

From the PHP Manual

This function may return Boolean FALSE, but may also return a 
non-Boolean value which evaluates to FALSE. 
Please read the section on Booleans for more information. 
Use the === operator for testing the return value of this function.

You want to equate using ===

strpos($paidModules,'reminder') === false

Check for exactly false so you don't get string position 0:

if (strpos($paidModules,'reminder') === false) {

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top