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

avatar Annisa44
@Annisa44

2 Kontribusi 0 Poin

Diperbarui 1 tahun yang lalu

2 Jawaban:

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

avatar Yuant
@Yuant

4 Kontribusi 4 Poin

Dipost 1 tahun yang lalu

<div>terima kasih atas jawaban yang telah diberikan.&nbsp;</div>

avatar Annisa44
@Annisa44

2 Kontribusi 0 Poin

Dipost 1 tahun yang lalu

Login untuk ikut Jawaban