Forum

Thread tagged as: Question, Addons, Api

Application file only available when logged in

I have an application I built that handles saving form data to the perch DB and displaying it in the admin area. Other than a process.php file which handles my form, the app is admin only.

I've noticed that when I'm logged out of perch, my AJAX post request to that process.php file returns a 403 forbidden error. Is there some sort of setting I'm missing that I need to enable to make process.php accessible for posting when not logged in?

Mark Wojtul

Mark Wojtul 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's your process.php file doing? If you've hooked it up like a control panel page, it'll try to authenticate you. You need to treat it the same as your runtime.php

In process.php I'm just including the perch API, making a class that extends PerchAPI_Factory, and then I have a method that uses perchDB to save the form data to the DB.

Here's my code:

include('../../../core/inc/api.php');
$API  = new PerchAPI(1.0, 'contact_form');

class Process extends PerchAPI_Factory
{
    protected $table     = 'contact_form';
    protected $pk        = 'formID';        

    public function newSubmission(){
        $incoming = $_POST;
        $json = PerchUtil::json_safe_encode($incoming);
        $data = [];
        $data['label'] = 'contact_form';
        $data['data'] = $json;
        $data['timestamp'] = date('Y-m-d H:i:s');
        $response = new stdClass();
        $response->meta = new stdClass();

        try{
            $newID = $this->db->insert('perch2_contact_form', $data);
            $response->meta->status = 'success';
        } catch (Exception $e) {
            $response->meta->status = 'fail';
        }
        return $response;
    }   
}

$process = new Process();
$response = $process->newSubmission();
print_r(json_encode($response));

Am I hooking into the API the wrong way by including core/inc/api.php?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, include the perch/runtime.php file instead.

Thanks!