CodeIgniter Forums
Hit Counter in Post by ip address - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Hit Counter in Post by ip address (/thread-53815.html)



Hit Counter in Post by ip address - El Forum - 08-10-2012

[eluser]anyamanggar[/eluser]
hello guys, i have question, please help me yaa ..

this my view detail for news

Code:
<div class="tekscontent">
&lt;?php $count = mysql_fetch_row(mysql_query("SELECT count FROM news")); ?&gt;
<div class="imgplace"><img class="imgdetail" src="&lt;?=$news_item['img_path']?&gt;" width="500" height="295" />
&lt;?=$news_item['caption']?&gt;</div>
<h1 class="judulberitadepan">&lt;?=$news_item['title']?&gt;</h1>
<h1 class="dateberita">&lt;?php echo indonesian_date(($news_item['date'])); ?&gt; Dilihat : &lt;?php print "$count[0]"; ?&gt; Kali</h1>
&lt;?=$news_item['text']?&gt;

</div>

news model
Code:
function get_news($slug = FALSE)
{

if ($slug === FALSE)
{
  $this->db->order_by('id','desc');
  $query = $this->db->get('news');
  return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

database :
id
title
slug
intro
text
count

with that code i have been work but not really work. Every post have the same count vor view even i have create post with the time over 10 mineute. please help thanks



Hit Counter in Post by ip address - El Forum - 08-10-2012

[eluser]LuckyFella73[/eluser]
It's because you SELECT in your view pulls all records from
your news tabel

Quote:SELECT count FROM news

You have to filter the results according to what you want to display.

Just a hint: better do queries in a model a send returned values via controller
to your view file instead of fireing the query in your view file.


Hit Counter in Post by ip address - El Forum - 08-10-2012

[eluser]anyamanggar[/eluser]
[quote author="LuckyFella73" date="1344598105"]It's because you SELECT in your view pulls all records from
your news tabel

Quote:SELECT count FROM news

You have to filter the results according to what you want to display.

Just a hint: better do queries in a model a send returned values via controller
to your view file instead of fireing the query in your view file.[/quote]

wow, tq. and about for ip address. I want log ip address if someone view that post


Hit Counter in Post by ip address - El Forum - 08-10-2012

[eluser]LuckyFella73[/eluser]
To get the ip you can use this php function:

Code:
function get_client_ip()
{
        if (!empty($_SERVER['HTTP_CLIENT_IP']))
  {
   $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
  else
  {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

In the controller calling the view do

Code:
$ip = get_client_ip();
// then do something with that value



Hit Counter in Post by ip address - El Forum - 08-14-2012

[eluser]anyamanggar[/eluser]
dear LuckyFella73,

i have been modify my script, and this is my database

Code:
-- phpMyAdmin SQL Dump
-- version 2.10.3
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 15, 2012 at 07:54 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.6

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";# MySQL returned an empty result set (i.e. zero rows).


--
-- Database: `berita`
--

-- --------------------------------------------------------

--
-- Table structure for table `news`
--

CREATE TABLE `news` (
`news_id` int(11) NOT NULL auto_increment,
`title` varchar(250) NOT NULL,
`slug` varchar(250) NOT NULL,
`content` text NOT NULL,
`ip_address` int(20) NOT NULL,
PRIMARY KEY (`news_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;# MySQL returned an empty result set (i.e. zero rows).


--
-- Dumping data for table `news`
[...]

controller
Code:
&lt;?php
class News extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('news_model');
  
}

public function index()
{
$data['news'] = $this->news_model->get_news();
$this->load->view('frontend/news_view', $data);

}

public function view($slug)
{
$this->load->helper('url');
     $this->load->model('news_model');
     $data['news'] = $this->news_model->get_news();
$data['news_item'] = $this->news_model->get_news($slug);

if (empty($data['news_item']))
{
  show_404();
  exit('The error is here.');  
}
$title = $slug;
$url_title = url_title($title);

$this->load->view('frontend/detail_view', $data);

}
}

news_model
Code:
&lt;?php

class News_model extends CI_model{

function __construct()
{
  parent::__construct();
  
}


function create() {
        $data = array(
            'title' => $this->input->post('title'),
            'intro' => $this->input->post('intro'),
            'text' => $this->input->post('text'),
   'img_path' => $this->input->post('img_path'),
   'caption' => $this->input->post('caption')
        );
  $data['slug'] = url_title($data['title']) ;
  $data['date'] =  date('Y-m-d H:i:s');
       $this->db->insert('news',$data);
    }


// ini untuk mengambil //
function getall($limit,$offset){
$this->load->database();
$this->db->order_by('news_id','asc');
$query = $this->db->get('news',$limit,$offset);
return $query->result();
}

function get(){
  
$this->load->database();
$query = $this->db->get('news');
return $query->result();
}

function findById($id) {
        $this->db->where('news_id', $id);
       $query = $this->db->get('news');
        return $query->row_array();
    }

function update(){
$this->load->database();
$data = array(
'title'=>$this->input->post('title'),
'img_path'=>$this->input->post('img_path'),
'intro'=>$this->input->post('intro'),
'text'=>$this->input->post('text'),
'caption' => $this->input->post('caption'),
);
$this->db->where('news_id',$this->input->post('news_id'));
$data['date'] =  date('Y-m-d H:i:s');
$this->db->update('news',$data);
}

function delete($id){
$this->load->database();
$this->db->delete('news', array('news_id' => $id));
}

function cariberita($keyword) {
  $this->db->like('title',$keyword);
  return $this->db->get('news')->result();
}

public function num_rows_berita()
{
  return $this->db->get('news')->num_rows();
}

  function get_news($slug = FALSE)
{

if ($slug === FALSE)
{
  $this->db->order_by('news_id','desc');
  $query = $this->db->get('news');
  return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
mysql_query("UPDATE news SET ip_address = ip_address + 1 where slug = '$slug'");
   $count = mysql_fetch_row(mysql_query("SELECT ip_address FROM news"));
return $query->row_array();
}
}

news_view
Code:
&lt;?php foreach ($news as $news_item): ?&gt;
<ul class="listberitadepan">
<li>
<h2 class="judulberitadepan">&lt;?php echo anchor('news/read/' . $news_item['slug'], $news_item['title']); ?&gt;</h2>
&lt;?php echo $news_item['content'] ?&gt;
</li>
</ul>

&lt;?php endforeach ?&gt;

detail_view
Code:
<h1 class="judulberitadepan">&lt;?=$news_item['title']?&gt;</h1>
<h1 class="dateberita">Dilihat : &lt;?=$news_item['ip_address']?&gt; Kali</h1>
&lt;?=$news_item['content']?&gt;

route
Code:
$route['news/read/(:any)'] = 'news/view/$1';
$route['news'] = 'news';

the result
Code:
Perawatan Mobil Dengan Kit
[b]has been view : 9 time[/b]
Perawatan Mobil Dengan Kit

If i use thats script, the script is running well, everytime i refresh my browser, the post i click the view count has increase. Smile , the question is, how if visitor has been refresh the view count doesnt increase, view count will increase if different ip address.

i am sorry if my english doesnt good enough Smile thank you


Hit Counter in Post by ip address - El Forum - 08-15-2012

[eluser]anyamanggar[/eluser]
please hellp me.


Hit Counter in Post by ip address - El Forum - 08-15-2012

[eluser]LuckyFella73[/eluser]
You could use sessions. After tracking the first time you
can set a session flag to something like "allready tracked"
and only increase the tracking if the value is not set.
If the user closes the browser and visit you page again the
tracker would count again of course for a new sesion will be
generated.