Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable direct access to callback functions?

<? if ( ! defined('BASEPATH')) exit();

    class Registration extends CI_Controller {

        public function __construct() {
            parent::__construct();
            $this->load->model('registration_model');
        }

        public function index() {
            $this->load->library('form_validation');

            $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email|callback_email_available');

            if($this->form_validation->run() == FALSE) {
                $this->load->view('registration');
            } else {
                $this->registration_model->add_user();
            }
        }

        # Check E-mail
        public function email_available($email) {
            $this->db->select('email');
            $this->db->where('email', $email);
            $query = $this->db->get('users');
            $result = $query->row();

            if(!empty($result)) {
                $this->form_validation->set_message('email_available', 'This e-mail belongs to another user.');
                return FALSE;
            } else {
                return TRUE;
            }
        }

    }
    ?>

I have a registration form with Form Validation. And I have a callback function to validate email uniqueness.

All code works fine, but I can directly access to callback function with errors

examle.com/registration/email_available

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Registration::email_available()
Filename: controllers/registration.php

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: email
Filename: controllers/registration.php

How can I deny direct access to callback function?

like image 680
Max Avatar asked Oct 02 '12 01:10

Max


People also ask

How do you pass a callback function in JavaScript?

Passing a function to another function or passing a function inside another function is known as a Callback Function. Syntax: function geekOne(z) { alert(z); } function geekTwo(a, callback) { callback(a); } prevfn(2, newfn); Above is an example of a callback variable in JavaScript function.

How does callback function work in MATLAB?

When a UI component executes a callback function, MATLAB automatically passes two input arguments to the function. These input arguments are often named src and event . The first argument is the UI component that triggered the callback. The second argument provides event data to the callback function.

What are callback functions used for?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Why do we need callback function in JavaScript?

Need of Callback Functions. We need callback functions because many JavaScript actions are asynchronous, which means they don't really stop the program (or a function) from running until they're completed, as you're probably used to. Instead, it will execute in the background while the rest of the code runs.


1 Answers

You can prefix the method name with an _ to deny access through HTTP request.

like image 144
Kemal Fadillah Avatar answered Sep 20 '22 15:09

Kemal Fadillah