Jump to content

[SOLVED] User Login timestamp or datetime


giraffemedia

Recommended Posts

I've got a table on mysql setup with user information such as login name, password, first name, last name etc. I also have row call last_login using the timestamp function in mysql and i'm trying to update that everytime a user log's in (or out - i'm not fussy)  and i'm using sessions for the user section. I cannot get it working, does anyone have an idea  how I would implement this?

 

Would I need to use an INSERT query on the login success page to update the last_login row where the id = '$user_id';

 

Regards

 

James

Link to comment
Share on other sites

I'm not quite sure how to do this properly. I'm trying to set the timestamp according to the id of the person logged in but it's not working. Here is what I have so far...

 

<?php
//Start session
session_start();

include('../includes/config.php');
include('../includes/opendb.php');

//Sanitize the value received from login field
//to prevent SQL Injection
if(!get_magic_quotes_gpc()) {
	$login=mysql_real_escape_string($_POST['login']);
}else {
	$login=$_POST['login'];
}

//Create query
$query="SELECT member_id FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
$result=mysql_query($query);
//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['member_id'];
		session_write_close();

		$query="INSERT INTO members (last_visit) VALUES (CURRENT_TIMESTAMP()) WHERE login='$login' ";
			$result=mysql_query($query);


		header("location: /admin.php");
		exit();
	}

	else {
		//Login failed
		header("location: login_failed.php");
		exit();
	}
}else {
	die("Query failed");
}



?>

 

Anyone have an idea?

 

Regards

 

James

Link to comment
Share on other sites

I've done that now Adam (hello again) and now get the error...

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE member_id ='login'' at line 1

 

It looks like i've not got the correct info after WHERE but i'm not sure what info this would be here. I'm presuming the data i've just passed through the login form?

Link to comment
Share on other sites

Afraid not - blank pages.

 

Here is my login form which passes the login name and password (md5) to the processing form:

 

<?php

include (INCLUDES_DIR."config.php");

?>
<html>
<head>
<title>. : Cricket Club - Please Log-In : .</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>



<form id="loginForm" name="loginForm" method="post" action="library/login_exec.php">
  <table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
      <td width="112"><b>Login</b></td>
      <td width="188"><input name="login" type="text" class="textfield" id="login" /></td>
    </tr>
    <tr>
      <td><b>Password</b></td>
      <td><input name="password" type="password" class="textfield" id="password" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Login" /></td>
    </tr>
  </table>
</form>

			<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
</body>
</html>

 

 

the processing form is the one I have previously posted

Link to comment
Share on other sites

If I use this for the processing page I can echo the input fields:

 

<?php

//Start session
session_start();

include('../includes/config.php');
include('../includes/opendb.php');

//Sanitize the value received from login field


echo $_POST['login'];
echo $_POST['password'];

?>

Link to comment
Share on other sites

try this then in the form processing page

 

<?php
//Start session
session_start();

include('../includes/config.php');
include('../includes/opendb.php');

//Sanitize the value received from login field
//to prevent SQL Injection
if(!get_magic_quotes_gpc()) {
	$login=mysql_real_escape_string($_POST['login']);
}else {
	$login=$_POST['login'];
}

//Create query
$query="SELECT member_id FROM members WHERE login='$login' AND passwd=md5('$_POST['password']')";
$result=mysql_query($query);
//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['member_id'];
		session_write_close();

		$query="INSERT INTO members (last_visit) VALUES (CURRENT_TIMESTAMP()) WHERE login='$login' ";
			$result=mysql_query($query or die mysql_error());


		header("location: /admin.php");
		exit();
	}

	else {
		//Login failed
		header("location: login_failed.php");
		exit();
	}
}else {
	die("Query failed");
}



?>

Link to comment
Share on other sites

I get a blank page Adam. If I replace the line:

 

	$query="SELECT member_id FROM members WHERE login='$login' AND passwd=md5('$_POST['password']')";

 

with:

 

	$query="SELECT member_id FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";

 

I can log in.

Link to comment
Share on other sites

No. Another blank page i'm afraid. Is it a syntax error somewhere?

 

This is causing the problems:

 

		$query="INSERT INTO members (last_visit) VALUES (CURRENT_TIMESTAMP()) WHERE login='$login' ";
			$result=mysql_query($query or die mysql_error());

 

If I take it out I can log in ok

Link to comment
Share on other sites

Thats because you dont want to INSERT if the member id already exists, you want to UPDATE like I showed you above.

 

If this is the first time they have registered, then you can INSERT when the record is created.

 

Sounds like you are trying to just INSERT the new time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.