【问题标题】:image doesn't want to show after an URL change网址更改后不想显示图片
【发布时间】:2025-12-10 11:15:01
【问题描述】:

在我使用此 URL (www.website/all_file/profil.php?u=username) 进入个人资料之前,所有信息都完美显示,包括个人资料图片。

但是当我更改 (www.website/all_file/profil.php/username) 的 URL 以进入个人资料时,它会显示信息,但不会显示用户的个人资料图片

这是网址的代码(www.website/all_file/profil.php?u=username):

$uss = $_GET['u'] ;


$m = "SELECT username,image_user,about,contact FROM database.users WHERE username= '$uss' "; 
       $de = mysqli_query($database_connection,$m);



      while($rows = mysqli_fetch_assoc($de)){

       $cnt = $rows['contact'];
       $abt = $rows['about'];
       $usern = $rows['username'];
       $img = $rows['image_user'];


 echo $cnt;
 echo $usern;      
 echo <img width='100' height='100' src='image_users/".$img." '>;   

在 url (www.website/all_file/profil.php/username) 代码中,我只是将 $uss = $_GET['u'] 更改为 $uss = 'username';

存储用户个人资料图片的文件是 (images_users),profil.php 在 images_users 文件之前。问题不在于数据库,因为图像已经存在。 谢谢

【问题讨论】:

  • 您的代码易受 SQL 注入攻击,您需要修复此问题。
  • 我该如何解决?
  • 我知道必须加上 $uss = mysql_real_escape_string($_GET['u']);但我没有 $uss = $_GET ['u'] 了,我有 $uss = 'username ' 从 URL 取而代之 (www.website/all_file/profil.php/username) 。

标签: php image url


【解决方案1】:

更改 URL 后,相对 URL 不再指向与以前相同的位置。

以前,当 URL 为 /all_file/profil.php 时,image_users/$img 指向 /all_file/image_users/$img - 因为您的浏览器知道 profile.php 是文件名(最后一个 / 之后的所有内容)。

更改 URL 路径的使用方式后,新路径为/all_file/profil.php/username。当您提供一个以此作为基本路径的相对 URL 时,您的浏览器会认为 profil.php目录路径 的一部分。因此,浏览器会尝试从 /all_file/profile.php/image_users/$img 加载您的图像 - 但失败了,因为这意味着您正在尝试加载名为 image_users/$img 的用户的配置文件。

将您正在使用的路径替换为图像的绝对路径而不是使用相对图像,并且图像应该再次加载:

 echo "<img width='100' height='100' src='/all_file/image_users/".$img." '>"; 

【讨论】: