Postingan lainnya
Error Trying to access array offset on value of type null bagian login
image.png
hasil error dari postman
image.pngbagian controller
image.pngbagian model user
image.pngbagian postman yang erornya
2 Jawaban:
<div>Saya tidak familiar dengan CodeIgniter, namun saya duga, CI_Model tidak<br>dapat mengakses objek REQUEST, sehingga code "$this->input->post(...)"<br>bernilai NULL.<br><br>kamu bisa mempassing parameter di method is_valid seperti contoh gambar yang saya berikan<br><br><figure data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2022-10-08 at 23.47.24.png","filesize":200116,"height":991,"url":"https://i.ibb.co/QCdhXCc/Screen-Shot-2022-10-08-at-23-47-24.png","width":1420}" data-trix-content-type="image/png" data-trix-attributes="{"presentation":"gallery"}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/QCdhXCc/Screen-Shot-2022-10-08-at-23-47-24.png" width="1420" height="991"><figcaption class="attachment__caption"><span class="attachment__name">Screen Shot 2022-10-08 at 23.47.24.png</span></figcaption></figure><br><br><figure data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2022-10-08 at 23.48.41.png","filesize":207648,"height":992,"url":"https://i.ibb.co/p0nPPFy/Screen-Shot-2022-10-08-at-23-48-41.png","width":1420}" data-trix-content-type="image/png" data-trix-attributes="{"presentation":"gallery"}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/p0nPPFy/Screen-Shot-2022-10-08-at-23-48-41.png" width="1420" height="992"><figcaption class="attachment__caption"><span class="attachment__name">Screen Shot 2022-10-08 at 23.48.41.png</span></figcaption></figure><br><br>Ini source code nya: UserController.php<br>------------------------------------------<br><br><?php</div><div><br></div><div>class UserController extends CI_Controller</div><div>{</div><div> public function __construct()</div><div> {</div><div> parent::__construct();</div><div><br></div><div> // load any model here if needed.</div><div> $this->load->model('User');</div><div> }</div><div><br></div><div> /</div><div> We receive payload data with json data type, so we want</div><div> * you to add a custom http-header to your request, like:</div><div> </div><div> X-Requested-With : XMLHttpRequest</div><div> * Content-Type: 'application/json'</div><div> </div><div> Method: POST</div><div> </div><div> with that header parameter, we know that the request uses JSON data</div><div> * type and we should return the same data type result.</div><div> /</div><div> public function login()</div><div> {</div><div> if (!$this->request->isAJAX()) {</div><div> return $this->response([</div><div> "success" => false,</div><div> "message" => "We only accept body parameter with JSON data type"</div><div> ]);</div><div> }</div><div><br></div><div> $email = $this->input->post('email');</div><div> $password = $this->input->post('password');</div><div><br></div><div> if ( !$this->User->is_valid($email, $password) )</div><div> return $this->response([</div><div> "success" => false,</div><div> "message" => "Your email or password does not match"</div><div> ]);</div><div><br><br></div><div> return $this->response([</div><div> "success" => true,</div><div> "message" => "Congratulations, you can proceed to the next step."</div><div> ]);</div><div> }</div><div>}<br><br><br>Ini source code nya: User.php (Model)<br><br><?php</div><div><br></div><div>class User extends CI_Model</div><div>{</div><div> /</div><div> * This method will check and validate all parameters of the incoming login request,</div><div> * to ensure verification matches between username and password.</div><div> </div><div> For some reason, you can use traits for helper methods</div><div> * which can be used as reuse and standalone code and are easy to manage.</div><div> </div><div> @param $email as string</div><div> * @param password as string</div><div> </div><div> /</div><div> public function is_valid( $email, $password )</div><div> {</div><div> /</div><div> * This is bail validation.</div><div> /</div><div> if (empty( $email ) || empty( $password )) return false;</div><div><br></div><div> $get_password_from_db = $this->get("email", $email);</div><div><br></div><div> /</div><div> * we check the result of the get() method, to make</div><div> * sure the result is what we expected.</div><div> /</div><div> if ( !is_array($get_password_from_db) ) return false;</div><div><br></div><div> /</div><div> * we check if the password data is in the result of method get().</div><div> * this action will handle your current error.</div><div> * "Error Trying to access array offset on value of type null"</div><div> */</div><div> if ( !array_key_exists("password", $get_password_from_db) ) return false;</div><div><br></div><div> $current_password = $get_password_from_db["password"];</div><div><br></div><div> if ( !password_verify($password, $current_password) ) return false;</div><div><br></div><div> return true;</div><div> }</div><div>}<br><br><br>Semoga bermanfaat. </div>
<div>terima kasih atas jawaban yang telah diberikan. </div>