-1

I'm making user activity logging, when I log in user login, have successfully entered database, but when I record user exit activity, user name is not recorded in the database. this my code.

<?php
include '../koneksi.php';
session_start();
$idPengguna = $_SESSION['username'];
$aktifitas = 'Logout';
$tgl = date('Y-m-d H:i:s');
mysql_query("INSERT INTO logfile (nik,aktifitas,tgl_aktif) VALUES ('$idPengguna','$aktifitas','$tgl')")or die(mysql_error());
session_destroy();
header("location:../index.php");
?>

enter image description here

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
  • show us how you connect to your database, you should also consider switching to MySQLi. – Niels Dec 02 '17 at 04:55
  • Use session_start(); at the top of your file above include. – Amit Gupta Dec 02 '17 at 05:24
  • first check echo $idPengguna = $_SESSION['username']; die; – Amit Gupta Dec 02 '17 at 05:25
  • if it comes then it will be surely added and then session_destroy will work. I think you can debut it yourself very easily. – Amit Gupta Dec 02 '17 at 05:26
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Dec 02 '17 at 16:47

1 Answers1

1

Just check if the session variable is working fine and then use this code to insert

$servername="localhost";
    $username="root";
    $password="";
    $db="login";//database name
    //connection
    $conn=new mysqli($servername,$username,$password,$db);
    //fetch from form

    $sql=$conn->query("INSERT INTO logfile (nik,aktifitas,tgl_aktif) VALUES ('$idPengguna','$aktifitas','$tgl')");

    }
Rohan
  • 60
  • 1
  • 5