How to save IP address in a registration system?

999 Views Asked by At

I have created a code that can register users into the database. But I want save the IP too. Can someone help me with my problem? Because I've never created code with IP, I already try search but nothing.

$podeCriarConta = True; 

    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
    { 
     $_SESSION['error'] = "<img src='images/picons/error.png'></img> E-mail inválido! Por favor introduza um válido.<br />";  
     $podeCriarConta = False;       
   } 

    if(empty($_POST['password'])) 
    { 
     $_SESSION['error'] = "<img src='images/picons/error.png'></img> Password inválida! Por favor introduza uma válida.<br />";
     $podeCriarConta = False;        
    } 


    if(empty($_POST['username'])) 
    { 
     $_SESSION['error'] = "<img src='images/picons/error.png'></img> Utilizador inválido! Por favor introduza um Utilizador válido.<br />"; 
     $podeCriarConta = False;       
   } 

    if(empty($_POST['nome'])) 
    { 
        $_SESSION['error'] = "<img src='images/picons/error.png'></img> Nome inválido! Por favor introduza um Nome válido.<br />";
        $podeCriarConta = False;                
    } 

    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            username = :username 
    "; 

    $query_params = array( 
        ':username' => $_POST['username'] 
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    {  
        die("Failed to run query line 68: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 

    if($row) 
    { 
     $_SESSION['error'] = "<img src='images/picons/error.png'></img> Nome de Utilizador já se encontra em uso!<br />"; 
    } 
    $query = " 
        SELECT 
            1 
        FROM users 
        WHERE 
            email = :email 
    "; 

    $query_params = array( 
        ':email' => $_POST['email'] 
    ); 

    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
       $_SESSION['error'] .= "Failed to run query line 103: " . $ex->getMessage(); 
    } 

    $row = $stmt->fetch(); 

    if($row) 
    { 
        $_SESSION['error'] .= "<img src='images/picons/error.png'></img>  E-mail inválido! Já se encontra em uso.<br />"; 
    } 
    $query = " 
        INSERT INTO users ( 
            nome,
            username, 
            password, 

    "; 
1

There are 1 best solutions below

3
On

Your question not shows your proper requirment but may be you are searching for this:

$Ip = $_SERVER['REMOTE_ADDR'];  //this will gives you IP address from which the user is viewing the current page.

then just save this "$Ip" to your db.