-3

I was running a PHP test development server on localhost and I stumbled across this warning that breaks the site entirely, the error is

PHP Warning: Message: strpos(): Empty needle

and the snippet causing this error is

if ($CI->config->item('csrf_protection') === TRUE AND ! (strpos($action, $CI->config->base_url()) === FALSE OR strpos($form, 'method="get"')))

I tried googling for the error but none gave me a solid answer about it.

ADyson
  • 57,178
  • 14
  • 51
  • 63
phperson
  • 21
  • 7
  • So, most likely `$CI->config->base_url()` is empty. We need more details on what values are stored in those computed params to pinpoint the issue! – nice_dev Jul 05 '23 at 09:48
  • the base_url() referred here is stored in config.php and routes to the CodeIgniter root. (note here: the value that I put in base_url() is 'http://locahost:8000/project-root' I guess that I set it incorrectly, correct me if im wrong) – phperson Jul 05 '23 at 09:49
  • 1
    Well, strpos appears to be complaining that it's empty. So you'd better debug it and check what it actually contains when you run this particular snippet – ADyson Jul 05 '23 at 09:51
  • the problem isn't with the spesific snippet rather base_url() not referring to the root of CodeIgniter, see above. (Just to my guesses) – phperson Jul 05 '23 at 09:54
  • 2
    str_pos wouldn't care whether your base_url was pointing to the root directory, or some other path. The only thing it cares about is that the needle is _not_ empty, but here in this case apparently it was. So go and _debug_, instead of making guesses. – CBroe Jul 05 '23 at 09:59
  • `base_url() not referring to the root of CodeIgniter`... could be. But there's no point in guessing - success in programming requires facts and specifics. You need to test what that function _actually_ returns at the point where the error occurs. It seems, from the error, that it's empty (rather than just pointing to any particular location). Once you confirm that, you can start to think about _why_ it's empty, which as you say is not really anything to do with the code above - but then we couldn't help because we don't have any info in the question about it – ADyson Jul 05 '23 at 10:00
  • I'll update you once I finish debugging. – phperson Jul 05 '23 at 10:04
  • No problem. A simple call to var_dump ought to suffice to test the value, you can stick it just before the `if` statement you showed us above. e.g. `var_dump($CI->config->base_url());` – ADyson Jul 05 '23 at 10:05
  • it returns string(0) "" when I put it right before the if statement, does that mean it's empty? @ADyson – phperson Jul 05 '23 at 10:09
  • Isn't it simply `base_url()` instead of `$CI->config->base_url()`? – brombeer Jul 05 '23 at 10:10
  • aren't the configs should be in CI's app folder? (that's why I referred to ($CI->config->base_url()) @brombeer – phperson Jul 05 '23 at 10:11
  • `does that mean it's empty`...yes, it means the value is a string with 0 characters in it. Make sure you understand how to interpret the output of var_dump...that is a vital debugging skill! https://www.phptutorial.net/php-tutorial/php-var_dump/ explains a bit – ADyson Jul 05 '23 at 10:14
  • https://stackoverflow.com/a/6449396/5947043 may be relevant to getting base_url() working. See also https://codeigniter.com/user_guide/helpers/url_helper.html . I don't use CI but from docs and a quick search I don't see any evidence that it should be called via `$CI->config`, none of the examples use that. – ADyson Jul 05 '23 at 10:17
  • 1
    I can clearly see the logic that it's empty when it prints out 0 right there, not that I have lack of skill but I want to not get out of your guidance and ask you for a second confirmation instead of being a know-it-all, so excuses for being a PHP newbie and asking for insurance. (thanks for the info above, just saw it.) – phperson Jul 05 '23 at 10:17
  • Not a CI user, but I couldn't find anything that uses `$CI->config->base_url()`. https://codeigniter.com/userguide3/helpers/url_helper.html mentions `base_url()` "_is an alias for CI_Config::base_url()_" but that's about it. Which version of CI are you using? – brombeer Jul 05 '23 at 10:22
  • im using an older version as this is a fairly old codebase but I can't remember the version number, will inform once checked. – phperson Jul 05 '23 at 10:26

2 Answers2

2

Solution found, URL helper wasn't loading, had to load it manually in any form using $this->load->helper('url');, still don't know why autoload doesn't work but I guess i'll find a solution to it, thanks to all fellas especially @ADyson and @nice_dev.

phperson
  • 21
  • 7
0

you can run a debugger like xdebug and put break points in the code and stop there and inspect the values or you can log them to the CI files

log_message('debug', "action ".var_export($action, TRUE));
log_message('debug', "base url".var_export($CI->config->base_url(), TRUE));
log_message('debug', "form ".var_export($form, TRUE));
Simon
  • 66
  • 1
  • 7