CodeIgniter custom 404 message - php

I am trying to display an error message on CodeIgniter error page. I am trying this:
Controller/entries.php
public function show_entry()
{
$id = $this->uri->segment(3);
if ($id !== FALSE)
{
..
}
else
{
log_message('error', 'The post ID is missing.');
}
Shouldnt this display my error message 'The post ID is missing' on the CodeIgniter's default 404 error message ie. "The page you requested was not found."

No. The logging class is for writing messages to a log file (ideally somewhere that the user can't read it as it can have information on the inner-workings of your cite). It is something which is really beneficial to you above all.
To display a custom error message, you'll need to either use show_error or, probably more likely in this case, show_404 (both methods documented here).

Related

Multiple file input and form validation?

I have multiple files and input text in my form. My code is :
if(formValidationIsGood) {
if(customeCheckFileErrorIsEmpty) {
//StartProcess
}
else {
//I load my view with my array which contains the different error related to the files.
}
else {
//I load my view with the form error (set up by : $this->form_validation->set_rules('variable', 'Variable', 'required');) But if there is an error for the files I cant display it.
}
With this code I can't show the form error and the files error at the same time.
For example the user, is uploading an csv file instead of a pdf file, and he forgot to write into a text input, the form error will be displayed but not the file error and vice versa.
Obviously I am using the helper file provide by code igniter.
Since you want to display error messages for either A OR B, you can try this:
if (!formValidationIsGood || !customeCheckFileErrorIsEmpty)
{
// load the view and display whatever errors you found
}
else
{
// form validation IS good and the files error is empty
}
The if clause above will evaluate to true if either formValidationIsGood ISN'T true (the ! prefix is key) OR the customeCheckFileErrorIsEmpty ISN't true (which would mean there is an error)

GET variable from url is ignored when after folder-like page

At work (well more like an apprenticeship, dunno) I'm making a wordpress plugin that allows users to submit their posts (it's for a website catalog) with the ability to pay for some premium features. For this I'm using PayU.
I have a shortcode which I put on a page where users end up after the payment, and the shortcode shows appropiate message depending on GET variables. Eg. "your post awaits moderation" with a free post, "payment was successful" when it was so etc.
So when payment in PayU fails it will stick &error=501 to the end of the url, so my code detects that and tells the user their payment failed and their post won't be submitted. And here's the thing.
When the confirmation page url is itself a variable, eg. ?post_id=71, everything works as expected. So for mywebsite.com/?post_id=71&posttype=paid&error=501 the error is read and shortcode acts appropiately. But when the address in folder-like (sorry, really dunno how to call that) eg. /confirmation, error variable gets unnoticed. So with an address like mywebsite.com/confirmation?posttype=paid&error=501 the result is a "payment successful" message. Order of variables doesn't seem to matter, posttype is always interpreted and error is always omitted.
I don't have access to the code right now but it's basically this:
function my_shortcode() {
if(!isset($_GET['posttype'])) {
// some generic text to handle this unexpected condition
}
else if($_GET['posttype'] == 'free') {
// nice message on how the post awaits moderation
}
else if($_GET['posttype'] == 'paid') {
if(isset($_GET['error'])) {
if($_GET['error'] == '501') {
// payment was unsuccessful
} else {
// some generic text cause this isn't supposed to happen too
}
} else {
// payment was successful
}
}
}
As I said, I'd expect the error value to modify the behavior accordingly, instead it only does so when page address is itself a query.

Display errors OpenCart

In PrestaShop I can display the errors with the function Tools::displayError() like this:
if (empty($email)) {
$this->errors[] = Tools::displayError('Email is empty.');
$this->doLog('ERROR: Email/username is empty');
} elseif (!Validate::isEmail($email)) {
$this->errors[] = Tools::displayError('Invalid email address.');
$this->doLog('ERROR: Invalid Email address');
}
Is there a similar way to do this in OpenCart? Is there a function i can use?
Thanks
To Turn on error reporting, please follow :
Go To Admin Panel
Go to System > Settings
Select your store from the list and click Edit
Go to the Server tab
To display errors, change Display Errors to Yes, If you want to log errors to a file, select Yes for Log Errors
Enter in an Error Log Filename
Click Save
Print Custom error message inside error.log
$logger = new Log('error.log'); //just pass the file name as error.log
$logger->write('Custom Error Message');
You will see the error file inside system-> storage-> logs-> error.log

Display custom error message in MVC view with PHP

I need to display user friendly error message in the view I am in, and wondering what will be the best solution. I can display and error page using error controller but this is not what i want to achieve. I need to handle all custom error messages in any model and display an error in the view you are in. For example:
I am in "user" controller. When creating new user, the PHP model code checks if same user name exist, if exist I want to display a message in the view or maybe have something like this in header: echo $error; which display any error message I have set to be displayed from any model if occurred.
Example error message in model:
if ($p0 > 0) {
$IsValid = false;
log::LOG_USER_ERROR("This user already exist!", $username);
exit("This user already exist! </br> ");
}
This code write the error in a log file successfully, however how do I display the error message in the same view I am in? exit() displays the message in a blank page. I need to display it as block in red in the same view and design.
exit() terminates the current script, so the code for your View is not executed.
Instead, part of your View should be an area to display messages. Then you can put the error message in a variable (probably an array of messages) that the View displays to the user in that area.

CodeIgniter 403 error instead of removing bad data

CodeIgniter 2.1.4
After doing a little research about CodeIgniter's XSS protections, I decided to quickly and crudely test this by typing some random HTML into any input field on my CodeIgniter forms.
When I typed in <script>, the page is redirected to the server's default 403 error ("Forbidden") page. It's not even a CodeIgniter error page.
I'm very glad that any input data containing <script> is stopped, however, I'm not understanding why this is generating a 403 error page instead of a validation error, or at least pass the data with the offending parts stripped out.
I'm using htmlentities() to convert the < and > but this makes no difference.
It doesn't even matter if implement the form validation. The input data of <script> will generate a 403 error even without it.
Can anyone explain what's happening here and if I need to be worried out how this is being handled/redirected? To me it just seems like I should be getting some sort of CodeIgniter validation error or stripped down data rather than a 403 error.
Here is a concise version of one of my Controllers. (It's happening on all Controllers with data input fields.)
public function search($search_slug = NULL)
{
$this->load->library('form_validation');
$this->form_validation->set_rules('search-terms', 'Search Terms', 'xss_clean');
if ($this->input->post('search-terms') && ($this->form_validation->run() !== FALSE))
{
$search_slug = url_title(htmlentities($this->input->post('search-terms')), 'dash', FALSE);
}
if ($search_slug !== NULL)
{
$search_terms = preg_replace('/-/', ' ', $search_slug);
$query = // get my results from model;
if ($query['count'] > 0)
{
$data['results'] = $query['results'];
}
else
{
$data['results'] = '<h3>Sorry, nothing found.</h3>';
}
}
else
{
$data['results'] = '<h3>Please enter your search terms.</h3>';
}
$this->template->load('default', 'search', $data);
}
After reading the comments on my OP and digging into it deeper, this 403 redirect doesn't seem to have anything to with CodeIgniter after all.
Also, after installing ZenPhoto on the same account, I see the 403 redirect when entering <script> into the ZenPhoto search box too.
Since it's shared hosting, I cannot say, with 100% certainty, that this is the result of something in Apache protecting itself, but all evidence seems to point there.
This character is not allowed.
check accepted answer # SO - characters allowed in a url
Your request wasn't reaching index.php
Apart from that have a look at application/config/config.php
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
These are the characters your application allows when a request reaches it.

Categories