Welcome Guest, Not a member yet? Register   Sign In
Error Message Handling / 404 Page
#1

[eluser]scm22ri[/eluser]
Hi Everyone,

I have a question regarding displaying errors while my website is active on the web.

Here's my scenario, if a visitor visits my below URL everything will be fine but if they accidentally or maliciously add various strings to my URL codeigniter will display the error message. Instead of displaying that error message to the visitor I want that visitor re-directed to the 404 page but I don't know how to do that. How would I do that?

Examples of what I was referring to above.
(This URL would work)
http://www.mydomain.com/cars-for-sale/2005-toyota-camry

(This URL wouldn't work and would display error messages)
http://www.mydomain.com/cars-for-sale/i-...yota-camry

This is what I want to do, how would I get the above URL(fruit and chicken) re-directed to the 404 page because currently that page is displaying error messages I don't want my visitors to see.

Thanks everyone
#2

[eluser]CroNiX[/eluser]
Check the segments, and if they are not an acceptable parameter (like it wasn't found in the db), then show_404(); else do regular...
#3

[eluser]scm22ri[/eluser]
Thanks for the reply and help. I understand what your saying but I'm having trouble with the actual process. Here's my dilemma.

The first parameter would be the a state. A state meaning California, Texas, Florida, New York etc ....

Notice, I'm checking (or at least I think I'm checking) the state parameter with my below function called "get_state_check". I then insert this function into my syntax and it's not working. What am I doing wrong? Thanks.

This function is in my model
Code:
function get_by_state($state){
  
    // note: this works if I manually type the state but if I type $state == $this->get_state_check($state) it's not working.  
    if($state == $this->get_state_check($state)){
    
    $this->db->distinct();
           $this->db->select('state, city, dealership, address, phone, zip, website');
    $this->db->group_by('city');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $this->db->where('approve','1');
    $query = $this->db->get();
       return $query->result();
    
    } else {
        show_404();
    }
  
}

This function is in my model
Code:
function get_state_check($state){
       $this->db->distinct();
    $this->db->select('state');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $query = $this->db->get();
    return $query->result();
}
#4

[eluser]CroNiX[/eluser]
In your query, you are returning an array of results, which may or may not contain data depending on if it was found.

Here, you aren't checking to see if the state name matches the $state, you are saying If $state == array, which it will never be.
Code:
if($state == $this->get_state_check($state)){
#5

[eluser]scm22ri[/eluser]
Thanks for the reply. I understand what your saying but I can't quite seem to write the proper syntax.

I've modified my functions a few times but I don't know what I'm specifically doing wrong. I think my problem is my query into my msql database. How would I account for each state. I thought I was doing that properly but it seems I'm not.Would anyone know what I'm specifically doing wrong? Thanks

Code:
function get_by_state($state){
  
/*  function get_state_check($state){
       $this->db->distinct();
    $this->db->select('state');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $this->db->where('approve','1');
    $query = $this->db->get();
    return $query->result();
}*/

// $state = $this->get_state_check($state);
  
    // note: this works if I manually type the state but if I type $state == $this->get_state_check($state) it's not working.  
    if($state == $this->get_state_check($state)){
    
    $this->db->distinct();
           $this->db->select('state, city, dealership, address, phone, zip, website');
    $this->db->group_by('city');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $this->db->where('approve','1');
    $query = $this->db->get();
       return $query->result();
    
    } else {
        show_404();
    }

Code:
function get_state_check($state){
  
   $query = $this->db->get_where(
   "dealers",
   array("state"=>$state)
   );
   return $query->result();
/*       $this->db->distinct();
    $this->db->select('state');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $this->db->where('approve','1');
    $query = $this->db->get();
    return $query->result();*/
}
#6

[eluser]scm22ri[/eluser]
Hi Everyone,

I think I might have just figured this out. My syntax is currently working. Please feel free if this is the "correct" way to fix my issue. Can I do anything better with my syntax? Am I doing anything wrong?

Note: this syntax only works for one variable. I'm currently going to be working on the syntax for two variables. Thanks everyone.

Code:
function get_by_state($state){

    if($state == $this->get_state_check($state)){
    
    $this->db->distinct();
       $this->db->select('state, city, dealership, address, phone, zip, website');
    $this->db->group_by('city');
    $this->db->from('dealers');
    $this->db->where('state', $state);
    $this->db->where('approve','1');
    $query = $this->db->get();
       return $query->result();
    
    } else {
        show_404();
    }
    
}

Code:
function get_state_check($state){
  
   $this->db->where('state',$state);
   $query = $this->db->get('dealers');
  
  if ($query->num_rows() > 0){
    foreach ($query->result() as $row){
       $state = $row->state;
      }
       return $state;
    
    }
    
}




Theme © iAndrew 2016 - Forum software by © MyBB