在用户成功上传头像以后,用户登录系统,应该能够看到自己的头像,本节演示如何在这个地方:
添加用户头像。
1.用DIV做:
border-radius:50%
background:url(xxx.jpg)
2.用img标签做:
border-radius:50%
src
共同点:定位
为了演示,先把图片的地址写死。
<img width="64px" height="64px" src="imageUpload\upload/small_Penguins.jpg" />
然后做具体的用户头像查询。
在login2.php中:
当用户登录成功后,我们已经把username存放到了session中。所以,只要用户不去关闭浏览器,用户名username就会保存在session中,又因为username唯一,所以我们在main.php查询头像的时候,不妨根据session中的username去查询。
代码变更:
<img class='header_pic' width="64px" height="64px" src="imageUpload<?php echo $header; ?>" />
php: (在文件的顶部)
<?php
session_start();
//在这里查询一下当前用户的头像
$username = $_SESSION["username"];
//链接数据库
$conn = mysql_connect("localhost","root","");
$db = mysql_select_db("test",$conn);
mysql_query("set names utf8");
$sql = "select header from tm_users where username = '$username';";
$rs = mysql_query($sql);
$header = null; //现在外面定义一下头像变量,否则在while循环体中就是局部变量了,外面访问不到。
while($row = mysql_fetch_array($rs)){
$header = $row["header"];
}
?>
设置默认头像
在实际的开发中,如果当前用户还没有设置头像,那么系统一般会给这个用户设置一个默认头像。
在本系统中,我们就判断select header from tm_users where username = '$username';查询出来的头像是否为空,如果为空,就给他一个默认的头像:
地址为:imageUpload\images\header.png
网友评论