Jump to content

[SOLVED] User Login timestamp or datetime


giraffemedia

Recommended Posts

Please have a check with the following code

<?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="update members set last_visit = NOW() WHERE member_id ='$member[member_id]'";
			$result=mysql_query($query);


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

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



?>

 

--

Thanks,

Tapos Pal

http://tapos.wordpress.com

Wrong one, we want the update query.

 


$query="UPDATE members SET last_visits = (NOW()) WHERE member_id ='".$_SESSION['SESS_MEMBER_ID'])"' ";
   
echo $query;
exit;
$result = mysql_query($query);

 

Get a blank page again.

Please have a check with the following code

<?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="update members set last_visit = NOW() WHERE member_id ='$member[member_id]'";
			$result=mysql_query($query);


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

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



?>

 

--

Thanks,

Tapos Pal

http://tapos.wordpress.com

 

HOORAY - It works!!!!

 

Thanks Tapos

Thanks for all your help guys. I understand I need to use the UPDATE query instead of INSERT, but why did:

 

WHERE member_id ='$member[member_id]'";

 

work instead of:

 

WHERE member_id ='".$_SESSION['SESS_MEMBER_ID'])"' ";

 

Does this mean the session is not being set correctly?

i think its because you are treating the session like a string with the  "  " around it.

 

To see if the session is set just echo it out

 

echo $_SESSION['SESS_MEMBER_ID']

 

I did try it without the " " but got a blank page.

 

echo $_SESSION['SESS_MEMBER_ID'];

 

That works fine and the session is being set.

 

I guess I need to learn more about debugging. I'm ordering the VTC training cd on php so hopefully i'll be able to learn a lot more soon.

 

Thanks for all you help.

 

James

 

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.