CodeIgniter Forums
Post Visit CounterWith Ip - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Post Visit CounterWith Ip (/thread-78419.html)



Post Visit CounterWith Ip - thedragon655 - 01-17-2021

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


RE: Post Visit CounterWith Ip - InsiteFX - 01-18-2021

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


RE: Post Visit CounterWith Ip - AndresHDZ - 01-18-2021

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');
        



RE: Post Visit CounterWith Ip - thedragon655 - 01-19-2021

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


RE: Post Visit CounterWith Ip - AndresHDZ - 01-19-2021

(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.


RE: Post Visit CounterWith Ip - paliz - 01-19-2021

(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);

        }