gpt4 book ai didi

PHP默认头像设置

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:33 25 4
gpt4 key购买 nike

我在创建 if 语句以在用户未上传自己的个人资料图片的情况下显示默认个人资料图片时遇到问题。我在网上尝试了很多示例,但它们似乎都没有对我的特定代码起作用,并且总体上没有回复工作。我删除了解决问题的尝试并提供了原始代码。这是我当前的代码:

<?php
session_start();

if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}

if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
$username = $_SESSION['username'];
$file = "extra/" . $username . ".png";
?>
<html>
<title> Home Page </title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<header>
<div class="container">
<nav>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="downloads.php">Downloads</a></li>
<li><a href="chat.php">Chat</a></li>
<li><a href="profile.php">Profile</a></li>
<li class="logout"><a href="index.php?logout='1'">Logout</a></li>
</ul>
</nav>
</div>
</header>
<body>
<div class="profileIMG">
<img src=<?php echo $file; ?> width="100" height="100">
</div>
<div class="profileNAME">
<<?php echo $username ?>
</div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>

CSS

ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 13%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
font-size: 20px;
font-family: arial;
overflow: hidden;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}

li a.active {
background-color: #4CAF50;
color: white;
}

li a:hover:not(.active) {
background-color: #555;
color: white;
}

li a:target {
background-color: #4CAF50;
color: white;
}

li button {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}

li button.active {
background-color: #4CAF50;
color: white;
}

li button:hover:not(.active) {
background-color: #555;
color: white;
}
body {
background-image: url(extra/background.png);
margin: 0;
overflow: hidden;
}
h5 {
color: green;
margin-left: 6%;
font-family: arial;
font-size: 15px;
}
input[type=text] {
width: 100%;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 15px;
}
#player {
width: 200px;
height: 50px;
position: relative;
margin-left: 24px;
top: 18px;
}
#player i {
position: absolute;
margin-top: -6px;
color: #666;
}
#player i.fa-volume-down {
margin-left: -8px;
}
#player i.fa-volume-up {
margin-right: -8px;
right: 0;
}

#volume {
position: absolute;
left: 24px;
margin: 0 auto;
height: 5px;
width: 150px;
background: #555;
border-radius: 15px;
}
#volume .ui-slider-range-min {
height: 5px;
width: 300px;
position: absolute;
background: #2ecc71;
border: none;
border-radius: 10px;
outline: none;
}
#volume .ui-slider-handle {
width: 20px;
height: 20px;
border-radius: 20px;
background: #FFF;
position: absolute;
margin-left: -8px;
margin-top: -8px;
cursor: pointer;
outline: none;
z-index: 1;
}
.logStatus {
position: absolute;
bottom: 0;
}

CSS 不是必需的,而是为了使查看更令人愉悦。此外,文件夹 extra/ 包含图像,默认照片的名称为 defaultProfile.png

最佳答案

在初始化 $file 变量的脚本顶部进行这个简单的更改,

$file = 'extra/' . $username . '.png';

if (!file_exists($file))
$file = 'path-to-default-image';

公平地说,如果你想让你的代码保持“逻辑”,你可以翻转上面的检查,所以它首先将 $file 设置为默认图像位置,然后如果用户图像存在然后切换到那个:

$file = 'path-to-default-image';

if (file_exists('extra/' . $username . '.png'))
$file = 'extra/' . $username . '.png';

阅读 Material

file_exists

关于PHP默认头像设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49756419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com