简体   繁体   中英

How to save datetime from MySQL into SESSION in php?

How can I save 'date_time' which is a datetime type into a session so I can display ex. "Registered 2013-04-09 15:45:20" on the page secret_place.php

I have this code in login.php

$query = mysql_query("SELECT `user`, `pass`, `date_time` FROM `database` WHERE user = '".$user."' AND pass = '".$pass."' LIMIT 1");
if (mysql_num_rows($query) > 0) {
$_SESSION['logged'] = true;
$_SESSION['user'] = $user;

header('Location: secret_place.php');
}

Give this a try,

$query = mysql_query("SELECT `user`, `pass`, `date_time` FROM `database` WHERE user = '".$user."' AND pass = '".$pass."' LIMIT 1");
if (mysql_num_rows($query) > 0) 
{
   $_SESSION['logged'] = true;
   $_SESSION['user'] = $user;
   while ($row = mysql_fetch_assoc($query)) 
   {
       $_SESSION['date_time'] = $row['date_time'];
   }
   header('Location: secret_place.php');
}

There are some serious issues, here: First of all, please, don't use mysql_* functions in new code . They are no longer maintained and are officially deprecated . See the red box ? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial . That way you'll also prevent SQL injection attacks , right now your code is wide open to them.

Also do not store plain password in the DB. Store it hashed, using some proper lib, like PasswordLib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM