尝试为已登录用户(管理员或简单用户)显示其他图片。这是我的功能
function isAdminPhotoChange() {
// check if user is admin or user
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin') {
if (file_exists("images/metcircle13.png")) {
$filename = "$metcircle13.png";
echo '<img src="images/<?php echo'. $filename.'?>" style="height: 50px;">';
} else {
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'user') {
$filename = "user_profile.jpg";
echo '<img src="images/<?php echo'. $filename.'?>" style="height: 50px;">';
}
}
}
}
这是另一个php文件中的调用
<?php
include("functions.php");
$comm = mysqli_query($db, "select name,comment,post_time from comments");
while($row=mysqli_fetch_array($comm)){
$name=$row['name'];
$comment=$row['comment'];
$time=$row['post_time'];
}
?>
<div class="sxolion">
<strong style="margin: 3px; color: #000; text-shadow: 2px 2px 5px #3d5c5c;"><?php isAdminPhotoChange(); ?><br><p style="margin: 4px;"><?=$name?></p></strong><p style="margin: 3px;"><?=$comment?></p><span class="time"><br><p style="margin: 4px;"><?=date("j/m/Y g:i:sa", strtotime($time))?></p></span>
谢谢!
答案 0 :(得分:0)
以最好的方式,isAdminPhotoChange()函数中存在很多逻辑/语法错误,这些错误已在注释中提及,还有更多-尽管不会引发错误。
这是我尝试修复的功能,其余的看起来还可以:
function isAdminPhotoChange()
{
// ENSURE SESSION HAS STARTED
if(session_status() === PHP_SESSION_NONE)
{
session_start();
}
$admin_img = "images/metcircle13.png";
$user_img = "images/user_profile.jpg";
// IS ADMIN
if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' && file_exists($admin_img))
{
echo '<img src="'.$admin_img.'" style="height: 50px;">';
}
// IS USER
else if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'user' && file_exists($user_img))
{
echo '<img src="'.$user_img.'" style="height: 50px;">';
}
}