// check that the session exists first
if(isset($_SESSION['username'])) {
// you should put your db connection in a config.php file and use mysqli or PDO - what you're using is depreciated
mysql_connect("localhost", "root", "") or die("cannot connect");
mysql_select_db("logtime") or die("cannot select DB");
// don't think I'd store password in a session...
// also, is username UNIQUE in your database?
// also, also, ALWAYS escape (sanitize) your database input to prevent agains SQL injection
$sql = "SELECT username, password
FROM
users
WHERE
username = '".mysql_real_escape_string($_SESSION['username'])."'
AND
password = '".mysql_real_escape_string($_SESSION['password'])."'";
$result = mysql_query($sql) or die('sql: '.mysql_error());
if(mysql_num_rows($result) > 0) {
$sql_2 = "INSERT INTO exit1(username, outtime) VALUES ('".mysql_real_escape_string($_SESSION['username'])."', NOW())";
mysql_query($sql_2) or die('sql2: '.mysql_error());
session_destroy();
header("location: index.php");
} else {
echo 'There was an error. You have not been logged out.';
}
}
这里的问题是,当我点击注销按钮时,时间没有保存在数据库中,屏幕变成白色。有什么想法吗?
打开错误报告。另外,您在session_start()
之前调用session_destroy()
和$_SESSION[]
。请查看此处的示例。
不要使用mysql_*
函数,它们已被弃用。使用PDO
或mysqli_*
。
不要在数据库中存储原始密码。使用password_hash()
和password_verify()
函数(md5
不用于密码哈希(。
此代码用于session.php
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect("localhost", "root", "");
// Selecting Database
$db = mysqli_select_db($connection, "logtime");
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysqli_query($connection, "select username from users where username='$user_check'");
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>
该代码用于在mysql 中捕获注销时间
<?php
include('session.php');
mysqli_query($connection,"INSERT INTO exit1(id, username, outtime) VALUES ('', '$login_session', NOW())");
header('Location: index.php');
session_destroy();
?>