MAIS

    Solucionando o problema de update nos hits do Joomla quando o cache do sistema está habilitado

    - Anúncio -
    - Anúncio -

    Importante: certifique-se que seu tema carrega a biblioteca jQuery.

    1° Criando o arquivo de update

    Dentro da pasta do seu template, crie uma pasta chamada hits, e dentro dela o arquivo update.php com o seguinte conteúdo:

    // Set flag that this is a parent file.
    define('_JEXEC', 1);
    define('DS', DIRECTORY_SEPARATOR);
    $path = "/var/www/public_html/";
    define('JPATH_BASE', $path);
    require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
    require_once JPATH_BASE . DS . 'includes' . DS . 'framework.php';
    $mainframe = JFactory::getApplication('site');
    
    $mainframe->initialise();
    
    $id= trim($_POST['id']);
    
    function anti_sql_injection($string) {
      $string = str_ireplace(" or ", "", $string);
      $string = str_ireplace("select ", "", $string);
      $string = str_ireplace("delete ", "", $string);
      $string = str_ireplace("create ", "", $string);
      $string = str_ireplace("drop ", "", $string);
      $string = str_ireplace("update ", "", $string);
      $string = str_ireplace("drop table", "", $string);
      $string = str_ireplace("show table", "", $string);
      $string = str_ireplace("'", "", $string);
      $string = str_replace("#", "", $string);
      $string = str_replace("=", "", $string);
      $string = str_replace("--", "", $string);
      $string = str_replace("*", "", $string);
      $string = strip_tags($string);
      $string = addslashes($string);
    
        return $string;
    }
    
    $id = anti_sql_injection($id); 
    
    
    
    $db = JFactory::getDBO();
    $db->setQuery(  'UPDATE #__content' .
                    ' SET hits = hits + 1' .
                    ' WHERE id = '.(int) $id
                 );
     $db->query();  
     
     
    

    2° Inserindo script no seu template / tema

    edite o arquivo footer.php do seu template e coloque o código abaixo:

    <?php 
    	$user = JFactory::getUser();   //pegamos usuario
    	$input = JFactory::getApplication()->input;
    	$id = $input->getInt('id');  //recuperamos o ID do artigo
    	
    	//checamos se o usuario está logado ou não, caso não esteja, o script é adicionado.
    	if($user->get('guest') && $id > 0):    
    ?>	
    		<script type="text/javascript">
    				function saveDataJquery(){
    					var response;
    					jQuery.ajax({
    					  type: "POST",
    						  url: "<?php echo JURI::base(). 'templates/' . TEMPLATE .'/hits/update.php';?>",
    						  data:{id:<?php echo $id;?>},
    						  success: function(result) {
    				            response = result;
    				       	 },
    						error: function(XMLHttpRequest, textStatus, errorThrown) {
    				            response = "err--" + XMLHttpRequest.status + " -- " + XMLHttpRequest.statusText;
    				        }
    					});
    				};
    				
    				jQuery(window).load(function () {
    					saveDataJquery();
    				});
    		</script>
    <?php 	
    	endif;
    ?>

     

    3° Testando

     

    salve os arquivos e faça o teste, lembrando que esse tutorial foi testado a partir das versões 3.x do Joomla.

    nas versões anteriores é bem provável que de conflito entre jQuery e Mootools.

     

     

     

     

    - Anúncio -

    Artigos recentes

    Continue lendo

    DEIXE UMA RESPOSTA

    Please enter your comment!
    Please enter your name here