首页登录后怎么在首页显示用户名以及不显示登录框?

danshends 2019-05-19 03:53:51
像很多网站首页一样,如discuz论坛或百度首页,没登录前显示登录输入框或登录按钮,登录后返回首页,但不显示登录框了 ,而显示用户名?

未登录前:


登录后:


我写的两个文件,求大神解答
index.html:
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="doaction.php">
<table>
<tr>
<td align="right">用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value=" 登 录 "></td>
<td>没有账号?<a href="reg.html">立即注册</a></td>
</tr>
</table>
</form>
</body>
</html>


doaction.php:
<?php
header("Content-type:text/html;charset=utf-8");

$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) || empty($password)){
echo "<script>alert('用户名或密码不能为空!'); history.go(-1);</script>";
}else{
include "conn.php";
$sql = "SELECT username, password FROM user WHERE username='$username' && password='$password'";
$res = $conn->query($sql);
$no = $res->num_rows;
if(!$no){
echo "<script>alert('用户名或密码错误'); history.go(-1);</script>";
}else{
echo "<script>alert('登录成功!'); location.href='index.html';</script>";
}
}
...全文
365 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
y_w_x_k 2019-05-21
  • 打赏
  • 举报
回复
是的,登陆操作都是用的session,session存在服务器端,cookie存在浏览器上,当然session安全些
y_w_x_k 2019-05-20
  • 打赏
  • 举报
回复
登陆后用session记录登陆账户,然后在首页判断session是否存在就是了;
danshends 2019-05-20
  • 打赏
  • 举报
回复
引用 1 楼 y_w_x_k 的回复:
登陆后用session记录登陆账户,然后在首页判断session是否存在就是了;

谢谢~!另外想问下,实际开发中是不是都用session呢?那cookie只用来记录用户名就行了?我在网上看别人说session比cookie安全,一般用session居多

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧