Jump to content

[SOLVED] I am trying to record date/time of each logon event


cluce

Recommended Posts

OK I got rid of my previous warnings from previous question. 

 

can someone show me how to record the date/time ofeach login event???

 

here is my code..

<?php
session_start();

//check for required fields from the form
if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) {
	header("Location: user_logon.html");
exit;
}

//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "", "test");

//create and issue the query
$sql = "SELECT f_name, l_name FROM auth_users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";

$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

//get the number of rows in the result set; should be 1 if a match
if (mysqli_num_rows($result) == 1) {

//if authorized, get the values of f_name l_name
while ($info = mysqli_fetch_array($result)) {
	$f_name = stripslashes($info['f_name']);
	$l_name = stripslashes($info['l_name']);
}

//set authorization cookie
setcookie("auth", "1", 0, "/", "yourdomain.com", 0);
$_SESSION['usersname'] = $f_name . " " . $l_name;

//directs authorized user
header("Location: logon.php");

//record logon event
    //$sql = "INSERT INTO Events (user, Date) VALUES ('".$_POST['username']."',?date/timestamp?)";

} else {
//redirect back to login form if not authorized
$_SESSION['error'] =  "<font color='red'>invalid username and/or password combination</font>"; 
header("Location: user_logon.php");
exit;
}
?>

OK this is what I have so far but no data is being inserted into the events table. can someone tell me what I am missing??

 

<?php
session_start();

//check for required fields from the form
if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) {
	header("Location: user_logon.html");
exit;
}

//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "", "test");

//create and issue the query
$sql = "SELECT f_name, l_name FROM auth_users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";

$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

//get the number of rows in the result set; should be 1 if a match
if (mysqli_num_rows($result) == 1) {

//if authorized, get the values of f_name l_name
while ($info = mysqli_fetch_array($result)) {
	$f_name = stripslashes($info['f_name']);
	$l_name = stripslashes($info['l_name']);
}

//set authorization cookie
setcookie("auth", "1", 0, "/", "yourdomain.com", 0);
$_SESSION['usersname'] = $f_name . " " . $l_name;


//record logon event
    $sql = mysqli_query($mysqli,"INSERT INTO events (user) VALUES ('".$_POST['username']."'");

//directs authorized user
header("Location: logon.php");


} else {
//redirect back to login form if not authorized
$_SESSION['error'] =  "<font color='red'>invalid username and/or password combination</font>"; 
header("Location: user_logon.php");
exit;
}
?>

Try changing this line:

 

$sql = mysqli_query($mysqli,"INSERT INTO events (user) VALUES ('".$_POST['username']."'");

 

to this

 

mysqli_query($mysqli,"INSERT INTO events (user) VALUES ('".$_POST['username']."'") or die(mysql_error());

 

You might be interested in the date() function within PHP.

 

-Kalivos

<? 
$cryptedpwd = crypt($password,$salt); 
//match username and password from user database 
if ($login=="login"){
$db = mysql_connect("localhost", "YourUserName", "password") or die("Could not connect");
$provridb = mysql_select_db("inspection",$db);
$query = "SELECT * FROM auth WHERE username = '$username' AND password = '$cryptedpwd' LIMIT 1"; // modify to your configuration
$result = mysql_query($query)or die ("Error in query: $query. "); 
$row= mysql_fetch_array($result);
$settime = date (Ymdhis, mktime()); 
if ($num_rows==1) 
{ 
mysql_free_result ($result);

if ($identifier=="zoning")   { /*  // modify this or delete this sequence.  I use a hidden identifier along with there password to allow access to only the files they are authorizied to modify   */  
$HTTP_SESSION_VARS['loggedin']= $row["id"] ;
$loggedin = $row["id"] ;
session_register('loggedin'); 
?>
<?php {
mysql_query( "UPDATE auth SET  count = '$cnt', lastlogin ='$settime'  WHERE id= $loggedin");
} ?>

 

I hope you can extract what you need from this... if not respond to the post and I will try and help a specific configuration.

 

Best  :)

Try this!

 

<?php
session_start();

//check for required fields from the form
if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) {
	header("Location: user_logon.html");
exit;
}

//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "", "test");

// Make sure to protect against SQL Injection/I haven't
$username = $_POST["username"];
$password = $_POST["password"];

//create and issue the query
$sql = "SELECT f_name, l_name FROM auth_users WHERE username = '$username' 
		AND password =password('$password')";

$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));

//get the number of rows in the result set; should be 1 if a match
if (mysqli_num_rows($result) == 1) {

//if authorized, get the values of f_name l_name
while ($info = mysqli_fetch_array($result)) {
	$f_name = stripslashes($info['f_name']);
	$l_name = stripslashes($info['l_name']);
}

//set authorization cookie
setcookie("auth", "1", 0, "/", "yourdomain.com", 0);
$_SESSION['usersname'] = $f_name . " " . $l_name;


//record logon event	
//use the session variable to hold the previous last visit date.	
$_SESSION['last']=$last;   

  //Create a new column in the database events table called 
  //last_login datetime not NULL default'0000-00-00'
  $sql2="UPDATE events SET last_login=NOW() WHERE username='$username'";   //now only update it
  $result2=mysqli_query($mysqli,$sql2);  
   

//directs authorized user
header("Location: logon.php");


} else {
//redirect back to login form if not authorized
$_SESSION['error'] =  "<font color='red'>invalid username and/or password combination</font>"; 
header("Location: user_logon.php");
exit;
}
?>

 

Have not been tested!

here was my solution........

 

<?php
session_start();

//check for required fields from the form
if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) {
	header("Location: user_logon.html");
exit;
}

//connect to server and select database
$mysqli = mysqli_connect("localhost", "root", "", "test");

//create and issue the query
$sql = "SELECT username, f_name, l_name FROM auth_users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')";

$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));


//checks if user is disabled


//get the number of rows in the result set; should be 1 if a match
if (mysqli_num_rows($result) == 1) {

//if authorized, get the values of f_name l_name
while ($info = mysqli_fetch_array($result)) {
	$f_name = stripslashes($info['f_name']);
	$l_name = stripslashes($info['l_name']);
}

//set authorization cookie
setcookie("auth", "1", 0, "/", "yourdomain.com", 0);
$_SESSION['usersname'] = $f_name . " " . $l_name;


//record logons
    $sql2 = "INSERT INTO events (user, date) VALUES ('".$_POST['username']."', NOW())";
    
//$sql2="UPDATE auth_users SET last_login=NOW() WHERE username = '".$_POST["username"]."'";   
     mysqli_query($mysqli,$sql2);

//or die(mysql_error());

//directs authorized user
header("Location: logon.php");


} else {
//redirect back to login form if not authorized
$_SESSION['error'] =  "<font color='red'>invalid username and/or password combination</font>"; 
header("Location: user_logon.php");
exit;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.