0

I'm attempting to create a log when a user logs in through a MySql table and PHP, but nothing is showing up in my table. Everything else in my code works fine. Specifically I am attempting to store the Date, Time, IP of the accessor, Machine name of the accessor, and the username

I'm using

$username = $member['username'];
            $ip = $_SERVER[REMOTE_ADDR];
            $date = date('Y-m-d H:i:s');
            $host = gethostbyaddr($ip);
            $query = "INSERT INTO IP_LOG (DateTime, MachineName, IPAddress, Username) VALUES ($date,$host,$ip,$username)";
            $result=mysql_query($qry);

to get the info, create the query and log the info.

The code for the whole process really relies here when it checks if the login was successful

//Check whether the query was successful or not
    if($result) {
        if(mysql_num_rows($result) > 0) {
            //Login Successful
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);
            $_SESSION['SESS_MEMBER_ID'] = $member['mem_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['username'];
            $_SESSION['SESS_LAST_NAME'] = $member['password'];

            $username = $member['username'];
            $ip = $_SERVER[REMOTE_ADDR];
            $date = date('Y-m-d H:i:s');
            $host = gethostbyaddr($ip);
            $query = "INSERT INTO IP_LOG (DateTime, MachineName, IPAddress, Username) VALUES ($date,$host,$ip,$username)";
            $result=mysql_query($qry);

            session_write_close();
            header("location: home.php");
            //TODO: Log all Information

            exit();
        }else {
            //Login failed
            $errmsg_arr[] = 'user name and password not found';
            $errflag = true;
            if($errflag) {
                $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
                session_write_close();
                header("location: index.php");
                exit();
            }
        }
    }

I know it can successfully validate the credentials, but I can't seem to get the logging to work.

todaroa
  • 329
  • 1
  • 4
  • 15

2 Answers2

0

In your query, You didn't quote the string. It should be:

INSERT INTO IP_LOG (DateTime, MachineName, IPAddress, Username) VALUES ('$date','$host','$ip','$username')

Plus, stop using deprecated mysql_* functions. use MySQLi or PDO instead.

Moreover, you should escape input string with escape string functions.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • I changed over the query, but it is still having issues. I know that I have access to the database since I use it at a different part. It's something I can work out later. – todaroa Oct 22 '13 at 04:32
  • "having issues" mean ...? any error ? what is the output of `var_dump($result)` ? – Raptor Oct 22 '13 at 04:42
0

I would complete @Shivan Raptor's answer by this one:

    $query = "INSERT INTO IP_LOG (DateTime, MachineName, IPAddress, Username) VALUES ($date,$host,$ip,$username)";

and

      $result=mysql_query($qry);

your variables do not have the same name ($query and $qry). you made this mistake twice.

Antoine
  • 94
  • 3