<?php
session_start();
function session_login_time() {
if(!isset($_SESSION['start'])&&!isset($_SESSION['result'])){
$_SESSION['start'] = time();
$_SESSION['result'] = 0;
}
}
function session_logout_time() {
if(!isset($_SESSION['end'])){
$_SESSION['end']=time();
$_SESSION['result']=$_SESSION['end']-$_SESSION['start'];
return $_SESSION['result'];
}
}
function clear_sessions() {
unset($_SESSION['start']);
unset($_SESSION['end']);
unset($_SESSION['result']);
}
if(!empty($_POST['username'])){
$name = $_POST['username'];
$pwd = $_POST['userpwd'];
if($name = 'admin' && $pwd = 'admin'){
session_login_time();
echo "<script>alert('登陆成功!');location='login_in_out.php'</script>";
}else{
echo "<script>alert('登陆失败!');location='login.php'</script>";
}
}
if($_GET['logout']){
?>
<!doctype>
<div style='width:300px;line-height:30px;margin:auto;border:1px solid #ccc;text-align:center;color:white;font-size:14px;background:#ccc;'>统计用户在线时长</div>
</div>
<div style="width:300px;line-height:30px;margin:auto;border:1px solid #ccc;text-align:center;color:blue;font-size:14px;background:white;">
<?php
$re = session_logout_time();
echo "注销登陆前您一共在线<b><font color='red'>".($re)."</font></b>秒!";
clear_sessions();
?>
</div>
<?php
}
?>
if($_GET['logout']){
?>
它后面有?>,是不是表示php文件结束。但if里面没有语句哦
$_GET['logout'] 获取到GET提交方式下的'logout参数的值
isset() 函数的作用是检测变量是否设置
最好这样判断比较全面:
if(isset($_GET['logout']) && $_GET['logout'] != ''){
}
$_GET中php系统常量,用来获取地址栏的传值
如a.com?logout=logout
那么获取的就是logout
if是用来判断$_GET['logout']是否存在。追问
if($_GET['logout']){
?>
它后面有?>,是不是表示php文件结束。但if里面没有语句哦。
这是后台代码与html结合的一种写法
如
1){ ?>
a大于1; 这里就是判断A>1时运行的内容
a小于或等于1 这里就是判断A不大于1时运行的内容