CodeIgniter Forums
"This webpage has a redirect loop" - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: "This webpage has a redirect loop" (/showthread.php?tid=341)



"This webpage has a redirect loop" - luisgama - 11-23-2014

hi,

 I have this problem "This webpage has a redirect loop" at chrome.
 
 I have a table named USUARIOS, another ENDERECOS ( this table has 3 addresses per user ) and another one FAVORITA that store the favorite address from user.

 Let´s go

 This is the controller that list all addresses of the user:

 controller:
 
public function escolher_endereco(){
esta_logado();
set_tema('footerinc', load_js(array('jquery-1.10.2.min','jquery.dataTables','dataTables.foundation','table')),FALSE);

set_tema('titulo','Escolha de endereço');
set_tema('conteudo', load_modulo('usuarios','escolher_endereco'));

set_tema('template','pizza_view');
load_template();
}

 This is the view loaded to show addresses:

 view:
 
case 'escolher_endereco':
$iduser = $this->session->userdata('user_id');
?>
<div class="large-12 columns">
<?php
echo breadcrumb();
get_msg('msgok');
get_msg('msgerro');
?>
<table class="large-12 columns" id="data-table">
<thead>
<tr>
<th>Nome do Endereço</th>
<th>Rua</th>
<th>Cidade</th>
<th>Favorito</th>
</tr>
</thead>
<tbody>
<?php
$query_fav=$this->usuarios->get_byid('id_user',    $iduser, 'favorita')->row();  // pega endereco favorito
$query_end=$this->usuarios->get_byid('id_usuario', $iduser, 'endereco', 3)->result(); // pega todos enderecos

foreach ($query_end as $linha):
echo '<tr>';
printf('<td>%s</td>',$linha->nome_endereco);
printf('<td>%s</td>',$linha->rua);
printf('<td>%s</td>',$linha->cidade);
printf('<td>%s</td>',($linha->id_endereco==$query_fav->id_endereco)?'Sim':anchor("usuarios/loucura2/$linha->id_endereco", 'Não'));
echo '</tr>';
endforeach;

?>
</tbody>
</table>

</div>
<?php

break;

 This is another controller to show the new favorite address choose.

controller:
public function loucura2(){
esta_logado();

$endfavorito = $this->uri->segment(3);

$query_fav=$this->usuarios->get_byid('id_user', $this->session->userdata('user_id'), 'favorita')->row();  // pega endereco favorito

$dados= array('id_endereco' => $endfavorito);

set_tema('titulo','Favoritos');
set_tema('conteudo', load_modulo('usuarios','favorito3'));

set_tema('template','pizza_view');
load_template();

}


view:

case 'favorito3':
$iduser      = $this->session->userdata('user_id');
$novoendfavorito = $this->uri->segment(3);
   $query_fav=$this->usuarios->get_byid('id_user', $iduser, 'favorita')->row();  // pega endereco favorito
   $query_end=$this->usuarios->get_byid('id_endereco', $novoendfavorito, 'endereco')->row();  // pega endereco favorito
?>
<div class="large-12 columns">
<?php
echo breadcrumb();
get_msg('msgok');
get_msg('msgerro');
?>
<table class="large-12 columns" id="data-table">
<thead>
<tr>
<th>Nome do Endereço</th>
<th>Rua</th>
<th>Cidade</th>
<th>Favorito</th>
<th>Favorita</th>
</tr>
</thead>
<tbody>
<?php

echo '<tr>';
printf('<td>%s</td>',$query_end->nome_endereco);
printf('<td>%s</td>',$query_end->rua);
printf('<td>%s</td>',$query_end->cidade);
printf('<td>%s</td>',$query_end->id_endereco);
printf('<td>%s</td>',$query_fav->id_favorita);
echo '</tr>';

?>
</tbody>
</table>

</div>
<?php
echo anchor("usuarios/loucura3/$query_end->id_endereco",'Salvar Favorito',array('class'=>'button radius espaco'));

break;

 Here is my problem: this controller calls model to update database, but chrome starts a loop and abort execution with the message "This webpage has a redirect loop".

controller:

public function loucura3(){
$endfavorito = $this->uri->segment(3);

$query_fav=$this->usuarios->get_byid('id_user', $this->session->userdata('user_id'), 'favorita')->row();  // pega endereco favorito

$dados= array('id_endereco' => $endfavorito);

$this->usuarios->do_update( $dados,
  'favorita',
  array('id_favorita' => $query_fav->id_favorita));
redirect(base_url());

}

model:

public function do_update($dados=NULL, $mytable, $condicao=NULL, $redir=TRUE){
if($dados !=NULL && is_array($condicao) ):
$this->db->update($mytable, $dados, $condicao);
if($this->db->affected_rows()>0):
set_msg('msgok','Alteração efetuada com sucesso','sucesso');
else:
set_msg('msgerro','Erro ao atualizar dados','erro');
endif;

 This is my last try....I know this is a lot of controllers/views.
 
 The problem is on update. I don´t know why?
 
If anyone can help me, thank you so much!

Guapo


RE: "This webpage has a redirect loop" - Rufnex - 11-23-2014

Can you wrap you posting cod into a php code block please so its a bit hard to read ^^.


RE: "This webpage has a redirect loop" - luisgama - 11-23-2014

I´m sorry....

This is the controller
PHP Code:
class Usuarios extends CI_Controller {

    
/**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */

    
public function __construct(){
        
parent::__construct();
        
init_painel();

    }


    public function 
escolher_endereco(){
        
esta_logado();
        
set_tema('footerinc'load_js(array('jquery-1.10.2.min','jquery.dataTables','dataTables.foundation','table')),FALSE);
        
        
set_tema('titulo','Escolha de endereço');
        
set_tema('conteudo'load_modulo('usuarios','escolher_endereco'));
        
        
set_tema('template','pizza_view');
        
load_template();
    }

    public function 
loucura3(){
        
$endfavorito $this->uri->segment(3);

        if(
$endfavorito!=NULL){

            
$query_fav=$this->usuarios->get_byid('id_user'$this->session->userdata('user_id'), 'favorita');  // pega endereco favorito

            
if($query_fav->num_rows==1){
                
$query_fav $query_fav->row();
                
$dados= array('id_endereco' => $endfavorito);

                 
$this->usuarios->do_update(    $dados,
 
                                             'favorita'
 
                                             array('id_favorita'    =>    $query_fav->id_favorita));
                
redirect(base_url());
            }
            else
                
set_msg('msgerro','Favorito não encontrado!','erro');
        }
        else
            
set_msg('msgerro','Usuário não encontrado!','erro');


    } 

This is the view

PHP Code:
switch ($tela):

        case 
'escolher_endereco':
            
$iduser $this->session->userdata('user_id');
            
?>
            <div class="large-12 columns">
                <?php
                    
echo breadcrumb();
                    
get_msg('msgok');
                    
get_msg('msgerro'); 
                
?>
                <table class="large-12 columns" id="data-table">
                    <thead>
                        <tr>
                            <th>Nome do Endereço</th>
                            <th>Rua</th>
                            <th>Cidade</th>
                            <th>Favorito</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $query_fav
=$this->usuarios->get_byid('id_user'   $iduser'favorita')->row();  // pega endereco favorito
                        
$query_end=$this->usuarios->get_byid('id_usuario'$iduser'endereco'3)->result();    // pega todos enderecos

                        
foreach ($query_end as $linha):
                            echo 
'<tr>';
                            
printf('<td>%s</td>',$linha->nome_endereco);
                            
printf('<td>%s</td>',$linha->rua);
                            
printf('<td>%s</td>',$linha->cidade);
                            
printf('<td>%s</td>',($linha->id_endereco==$query_fav->id_endereco)?'Sim':anchor("usuarios/loucura2/$linha->id_endereco"'Não'));
                            echo 
'</tr>';
                        endforeach;
                                
                        
?>
                    </tbody>
                </table>
            
            </div>
            <?php             
                                    
        
break;

        case 
'favorito3':
            
$iduser      $this->session->userdata('user_id');
            
$novoendfavorito $this->uri->segment(3);
         
   $query_fav=$this->usuarios->get_byid('id_user'$iduser'favorita')->row();  // pega endereco favorito
         
   $query_end=$this->usuarios->get_byid('id_endereco'$novoendfavorito'endereco')->row();  // pega endereco favorito
            
?>
            <div class="large-12 columns">
                <?php
                    
echo breadcrumb();
                    
get_msg('msgok');
                    
get_msg('msgerro'); 
                
?>
                <table class="large-12 columns" id="data-table">
                    <thead>
                        <tr>
                            <th>Nome do Endereço</th>
                            <th>Rua</th>
                            <th>Cidade</th>
                            <th>Favorito</th>
                            <th>Favorita</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php

                            
echo '<tr>';
                            
printf('<td>%s</td>',$query_end->nome_endereco);
                            
printf('<td>%s</td>',$query_end->rua);
                            
printf('<td>%s</td>',$query_end->cidade);
                            
printf('<td>%s</td>',$query_end->id_endereco);
                            
printf('<td>%s</td>',$query_fav->id_favorita);
                            echo 
'</tr>';
                                
                        
?>
                    </tbody>
                </table>

            </div>
            <?php             
            
echo anchor("usuarios/loucura3/$query_end->id_endereco",'Salvar Favorito',array('class'=>'button radius espaco'));
                                    
        break; 

This is the model

PHP Code:
    public function do_update($dados=NULL$mytable='usuarios'$condicao=NULL$redir=TRUE){

        if(
$dados !=NULL && is_array($condicao) ):
            
$this->db->update($mytable$dados$condicao);
            if(
$this->db->affected_rows()>0):

                
set_msg('msgok','Alteração efetuada com sucesso','sucesso');
            else:
                
set_msg('msgerro','Erro ao atualizar dados','erro');
            endif;
            if (
$redir)
                
redirect(current_url());
        endif;
    }

    public function 
get_byid($campo=NULL$id=NULL$mytable='usuarios'$limit='1'){
        if(
$campo==NULL)
            return 
FALSE;

        if(
$id!=NULL):
            return 
$this->db->get_where($mytable, array($campo => $id), $limit);
        else:
            return 
FALSE;
        endif;
    
    } 


Thanks


RE: "This webpage has a redirect loop" - Rufnex - 11-23-2014

Your problem should be the double redirect "redirect(base_url());" in your controller and in your model. Remove the one in your model and give back a return value if you want do the redirect.


RE: "This webpage has a redirect loop" - sv3tli0 - 11-23-2014

I think that the problem is in the model...
Code:
if ($redir)
                redirect(current_url());

This code is terrible wrong specially at your case $redir is TRUE by default...
NEVER put redirects in your MODEL !!! There is the Logic not the Control !!!


RE: "This webpage has a redirect loop" - luisgama - 11-24-2014

(11-23-2014, 02:38 PM)sv3tli0 Wrote: I think that the problem is in the model...

Code:
           if ($redir)
               redirect(current_url());

This code is terrible wrong specially at your case $redir is TRUE by default...
NEVER put redirects in your MODEL !!! There is the Logic not the Control !!!


Guys, you are all right!!!!!!!!!!! Problem solved!

I´m using this model from a course of CodeIgniter....this is not my style, and I´m agree with you 100%. I prefer things explicits.

Now, I already cut this "redirect" from the model, and from now on I´ll not use inside procedure!!!!

Thank you very much