3

I am trying to get site visitors ip address & want to store that in table. This is my code to store data

public function store(Request $request)
{
    $secretcode=str_random(10);
    while(1)
    {
        $txnid=substr(hash('sha256', mt_rand() . microtime()), 0, 20);
        $txnstatus=Ticket::where('transactionid',$txnid)->get();
        if($txnstatus->isEmpty())
            break;


    }
    $request['ipaddress']=$request->ip();
    $request['transactionid']=$txnid;
    $request['secretcode']=$secretcode;


     $input=$request->all();
     Ticket::create($input);
     return view('payment');
}

I am using this line to get ip address

 $request['ipaddress']=$request->ip();

When this code inserts data into database table, it shows like this enter image description here

Someone please help me to get this work done. I also used Request::ip() but even that didn't work.

Important is that I am working in localhost environment with active internet connection.

dollar
  • 483
  • 3
  • 11
  • 27
  • `Request::ip();` should work. See [this answer](http://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5-1) that shows how `Request::ip();` works internally. – camelCase Dec 07 '15 at 05:42
  • Yes firstly I referred to that article and tried to use Request::IP() but its giving error message that this cannot be used staticly – dollar Dec 07 '15 at 07:11

1 Answers1

3

Your code is working fine. $request->ip() it should work and i tried it by myself to make sure your code is correct. ::1 this result is normal if you run in local environment. try it in hosting server to see your actual IP

martiendt
  • 1,909
  • 1
  • 17
  • 27
  • Yes you are right this ::1 was seen only because i was working on localhost, when hosted on server it's working fine. Thanks. – dollar Dec 08 '15 at 03:28
  • Just to clarify: `::1` is an IPv6 address, that's why it's in a different format than the usual IP addresses. `::1` is equal to `127.0.0.1`, the `localhost`. – Tim Visée Aug 17 '17 at 23:16