Welcome Guest, Not a member yet? Register   Sign In
Post Visit CounterWith Ip
#1

does anyone knows how to implement  an IP based view counter when someone clicks my posts? how do we make this in ci4 thank you
Reply
#2

1) Create a database page_visits table.
2) Use CodeIgniters incoming request class to get the users IP address.
3) Check database to see if the IP exists.
4) If the IP exists then increment counter and save to database
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Something like this:

PHP Code:
        $ipTableModel = new App\Models\IpTableModel();
        $ip_address $this->request->getIPAddress();
        $countResult $ipTableModel->where('ip_address'$ip_address)
            ->countAllResults();
        if($countResult == 0){
            $ipTableModel->insert([
                'ip_address' => $ip_address,
                'visits' => 1
            
]);
        }else{
            $ipTableModel->where('ip_address'$ip_address)
                ->increment('visits');
        
Reply
#4

will try this out does the model returntype have to be an array? thanks
Reply
#5

(01-19-2021, 07:29 AM)thedragon655 Wrote: will try this out does the model returntype have to be an array? thanks

I like to work with objects, but I think you wont have any problem using array.
Reply
#6

(01-17-2021, 03:44 PM)thedragon655 Wrote: does anyone knows how to implement  an IP based view counter when someone clicks my posts? how do we make this in ci4 thank you
the  code needs get user location  use _helper ci4 then call funtion in controller  all you need 
PHP Code:
function getCountryByIp($ip)

    {
        try {
            $xml file_get_contents(
                "http://www.geoplugin.net/json.gp?ip=" $ip);

        } catch (Exception $exception) {
            $xml null;

        }

        if (isset($xml)) {
            $ipdat = @json_decode($xml);
        } else {
            $xml null;
        }




        if ($xml != null and isset($ipdat->geoplugin_countryName)) {
            return array('country' => $ipdat->geoplugin_countryName,
                'code' => $ipdat->geoplugin_currencyCode,
                'city' => $ipdat->geoplugin_city,
                'lat' => $ipdat->geoplugin_latitude,
                'lang' => $ipdat->geoplugin_longitude'flag' => true);
        } else {
            return array('country' => '',
                'code' => '',
                'city' => '',
                'lat' => '',
                'lang' => '''flag' => false);

        }

    
Enlightenment  Is  Freedom
Reply




Theme © iAndrew 2016 - Forum software by © MyBB