The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using US classes in AJAX call
#1
I'm building am "add to cart" function via AJAX. When I load ini.php in the PHP file I call to in AJAX, it loads successfully, but I can't use any of the classes. The first thing I try to do is make a query call to the users table, andd I get this error:

PHP Fatal error: Call to a member function get() on null in...

$cart_query = $db->get('users',['username','=',$user->data()->username]);

That is the line of code that's throwing the error. I know I'm loading init.php successfully because I can echo something onto the screen. I had some trouble initially installing US on my server due to security measures over the fact I had PHP lower than 5.6. Same thing was happing, I was loading init.php successfully, but it was as if I was loading a blank file, no classes were coming through I could use. I'm wondering if there a security boundary I'm hitting. I've done all I can think of, I've included both the javascript file that my AJAX call is being made from as well of the PHP file my AJAX is calling to the admin pages, they are both passing the "if (!securePage($_SERVER['PHP_SELF'])){die();}" test. Not sure what to do from here, any help would be appreciated.
  Reply
#2
Hmm. Are you using some sort of parser file?

You might need to do $db=DB::getInstance();
  Reply
#3
That did the trick! I'll have to look into what would've caused that on my end. Thanks for the help!
  Reply
#4
That AJAX parser file is sort of like writing a function in php. It doesn't have access to the DB unless you exclusively grant it. That's where the db instantiation comes in.

It's also a good idea if you do some other checks in that parser file before updating the database. I usually do something like

Code:
if(hasPerm([2,3,4],$user->data()->id){
//the user has a permission level of 2, 3, or 4
Code:
//your code
Code:
}else{
Code:
die("You should not be here");
Code:
}
  Reply
#5
Got it, thanks for the tip!
  Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)