AJAX and PHP returning bool and string
<p>I’ve got an ajax based script that is designed purely to run a php script to authenticate a user. As there is with user authentication, there could be one of two outcomes: either they get authenticated or they don’t.<br>
To signify success or failure back to the page that called it is extremely easy, you just <code>echo ‘success’;</code> or ‘echo ‘failure’` accordingly and have the JS/jQuery script handle it by the response string. (Or at least I think that’s good practise..)</p>

<p>But apart from essentially returning true or false, if it returned false I would also like to give a message back as to why it failed. Did the user exist? Was the password incorrect? Or did the database access go wrong and need to spit out a technical error code. Since we are responding with true or false already, we can’t send back a message alongside with the false statement otherwise it technically isn’t false as there is more data.</p>

<p>Now I have had these ideas, but I feel like there is a better way to do it:</p>

<ul>
<li>Return an array <code>[true/false, “String to display”]</code> though this seems clunky within the PHP file and also parsing it on the page</li>
<li>Return <code>success</code> when we want to return true, and label anything else as a failure and assume it’s a message for failure</li>
</ul>

<p>But in all honesty I feel like this whole text response method is bad especially for something like user authentication as it could possibly be spoofed easily(?) so what would the recommended way to achieve something like this?</p>

<p>Thanks in advance!</p>