我一直在编写一些代码并在本地主机上对其进行了测试,一切正常,但是当我将其上传到我的服务器时,它无法超出我的 php 标记。也没有显示任何错误。我检查了两个 php 版本,在本地主机上运行版本 5.4.7,在服务器上运行版本 5.3.21。也许这就是造成问题的原因?我应该在 phpinfo() 中寻找什么吗?我的代码中遗漏了什么吗? 这是代码:
<!DOCTYPE html>
<?php phpinfo(); ?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<meta charset="utf-8">
<style>
body { background:black;}
.toggle { display:none; }
p {
width:570px;
text-align:justify;
color:#686868;
font-family:Georgia, serif;
}
h2 { color:black; }
button { background:#686868;
border:none;
font-family:Georgia, serif;
width:570px;
}
</style>
</head>
<body>
<?php
include('sql.php');
$i = 0;
while($i < count($rows)) {
echo "
<div>
<button class='trigger'>
<table width='100%'>
<tr>
<td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />
</td>
<td width='80%'><h2>{$rows[$i]['titel']} </h2></td>
</tr>
</table>
</button>
<div class='toggle'>
<p>
{$rows[$i]['info']}
</p>
</div>
</div>";
$i++;
}?>
</body>
<script>
$('.trigger').click(function() {
$(this).siblings('.toggle').slideToggle('fast');
});
</script>
</html>
当我运行它时,它显示黑色背景(正如它应该的那样),但是除了我的 php 起始标记之外的所有内容都被切断。 我还尝试强制 while 循环循环 10 次,并删除了从数据库获取数据的部分,以查看是否是 mysql 相关的问题。我可以得出结论,事实并非如此。
最佳答案
以下行是问题所在:
<img src='http://localhost/app-side/Icons/bar_icon.png
无法加载图像,因为 localhost
指的是客户端的本地计算机(运行浏览器的位置)而不是您的服务器。通常网站中的此类 URL 被视为恶意 ;)
使其在 localhost
和 server
上工作的解决方法是使用相对路径。这可以与您的文档相关,也可以与服务器的 DOCUMENT_ROOT
相关。我使用了第二种方法,因为我不知道服务器上 php 文件的位置。
包含相对于 DOCUMENT_ROOT
的链接的解决方案:
替换:
<img src='http://localhost/app-side/Icons/bar_icon.png
由
<img src='/app-side/Icons/bar_icon.png
关于PHP 在本地主机上工作但不在服务器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16373699/