Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Adobe Tutorialz Tutorials Expert Xtreme Pixel Player-Art 

Change Language

Who's online

There are currently 0 users and 7 guests online.

Why can't my last login date and time be displayed whenever I login?

I have problems with my last login date and time because whenever I login into my account, it doesn't retrieve my last login date and time. Here is the code and hope you can trace the error.

<?php
session_start
();
//include database configuration and connect
include 'config.php';
include 
'opendb.php';


   
   
if (isset(
$_POST))

{

//retrieve login form data
   
$user_email$_POST['user_email'];
   
$password sha1($_POST['password']);
   
$last_login date("d/m/y h:i:s");
   
   
//check for valid user 
   
$query"SELECT * FROM user WHERE user_email='$user_email' AND password='$password'";
   
$result mysql_query($query) or die(mysql_error());
   
   

    if (
mysql_num_rows($result)>0) {
   
       
$record mysql_fetch_assoc($result);
       
$_SESSION['user_email'] = $record['user_email'];
       
$_SESSION['user_image']=$record['user_image'];
       
$_SESSION['last_login']=$record['last_login'];
       
$_SESSION['admin'] = $record['admin'];
       
header('Refresh: 5; url=home.php');
       
       
$query1 = ("INSERT INTO user (last_login) VALUES('current_date')");
       
$result1 mysql_query($query) or die(mysql_error());
       
$query2 "UPDATE user SET last_login= '$last_login' WHERE user_email='$user_email'";
       
$result2 mysql_query($query) or die(mysql_error());
       
       
        echo 
"Login Successful.<font color='#98AFC7'>(You last logged in at '.$last_login'.)</font>";
       
        }
           
            else 
    {
       
//get user to login

   
echo "Wrong password or user email.<br />";

   
    }
   
}



include 
'closedb.php';
?>


Submitted by preengreen on December 3, 2008 - 9:44am.
printer friendly version

Using Replace Into

Since you are using MySQL why not use REPLACE?

<?php
$query 
"REPLACE INTO user SET last_login = '". . $last_login"', user_email = '"$user_email"'";
$result mysql_query($query) or die(mysql_error());
?>

Also, you might want to try the TIMESTAMP and DATETIME column types because they have options that will AUTO-UPDATE the time to the current time (NOW()).


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati