我最近一直试图进入PHP和JavaScript,所以我只是试验他们的每个功能,通过制作一个特定脚本的小页面,如显示时间,浏览器,IP地址等。但我似乎有点难过:
<html>
<head>
<title>Scripting Demo</title>
<style type="text/css">
html, body, a {
margin: 0;
padding: 0;
text-decoration: none;
}
.globalContainer {
width: 100%;
height: 100%;
text-align: center;
font-family: Arial;
background-image: linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0.00000105000042, rgba(255,248,133,255)), color-stop(0.5000002, rgba(255,244,73,255)), color-stop(1, rgba(255,241,13,255)));
background-image: -moz-linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
background-image: -o-linear-gradient(top, rgba(255,248,133,255) 0%, rgba(255,244,73,255) 50%, rgba(255,241,13,255) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fff885', endColorstr='#fff449'endColorstr='#fff10d');
}
.table {
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="globalContainer">
<table class="table" border="0">
<th>Current Time</th>
<tr>
<td align="center" id="currentTime">
</td>
<script type="text/javascript">
<!--
var cTime = document.getElementById("currentTime");
cTime.innerHTML="<b>Date:</b> "+Date();
cTime.style.backgroundColor = "rgb(188,212,230)";
cTime.style.paddingLeft = "5px";
cTime.style.paddingRight = "5px";
cTime.style.border = "solid";
cTime.style.borderRadius = "2em";
cTime.style.MozBorderRadius = "2em";
cTime.style.WebkitBorderRadius = "2em";
cTime.style.borderColor = "rgb(188,212,230)";
cTime.style.fontFamily = "Arial";
cTime.style.fontSize = "15px";
//-->
</script>
</tr>
<th>Current Browser</th>
<tr>
<td align="center" id="currentBrowser">
</td>
<script type="text/javascript">
var cBrow = document.getElementById("currentBrowser");
cBrow.innerHTML="<b>Browser:</b> "+navigator.appName;
cBrow.style.backgroundColor = "rgb(188,212,230)";
cBrow.style.paddingLeft = "5px";
cBrow.style.paddingRight = "5px";
cBrow.style.border = "solid";
cBrow.style.borderRadius = "2em";
cBrow.style.MozBorderRadius = "2em";
cBrow.style.WebkitBorderRadius = "2em";
cBrow.style.borderColor = "rgb(188,212,230)";
cBrow.style.fontFamily = "Arial";
cBrow.style.fontSize = "15px";
</script>
</tr>
<th>IP Address</th>
<tr>
<td align="center" id="ipAddress">
<b>IP:</b> <?php $ip=@$REMOTE_ADDR; echo $ip; ?>
</td>
<script type="text/javascript">
var ip = document.getElementById("ipAddress");
ip.style.backgroundColor = "rgb(188,212,230)";
ip.style.paddingLeft = "5px";
ip.style.paddingRight = "5px";
ip.style.border = "solid";
ip.style.borderRadius = "2em";
ip.style.MozBorderRadius = "2em";
ip.style.WebkitBorderRadius = "2em";
ip.style.borderColor = "rgb(188,212,230)";
ip.style.fontFamily = "Arial";
ip.style.fontSize = "15px";
</script>
</tr>
</table>
</div>
</body>
</html>
我意识到使用JavaScript只是为了实现CSS和所有爵士乐是非常低效的,但我只是为了练习而做的就是这样。
注意第七十六行:
<b>IP:</b> <?php $ip=@$REMOTE_ADDR; echo $ip; ?>
这是我尝试使用PHP并获取用户的IP地址的地方,但实际上并没有成功。有人会向我解释我做错了什么吗?我很想学习PHP和JavaScript,但我似乎真的很难过。 :o
答案 0 :(得分:3)
首先,要意识到@
会使错误和警告无声,这些会帮助您解决问题。
我认为您正在寻找$_SERVER['REMOTE_ADDR']
而$REMOTE_ADDR
是对手册的错误或误解。
http://php.net/manual/en/reserved.variables.server.php
$ _ SERVER
<强>指数强>
您可能在$ _SERVER中找到或未找到以下任何元素。 请注意,很少有这些(如果有的话)可用(或者确实有任何 如果在命令行上运行PHP。
'REMOTE_ADDR'
用户正在查看当前页面的IP地址。
答案 1 :(得分:3)
<b>IP:</b> <?php $ip=$_SERVER['REMOTE_ADDR']; echo $ip; ?>
这对我有用!
答案 2 :(得分:2)
$_SERVER
超全局数组包含有关远程连接的各种信息,包括其IP地址。使用phpinfo()
查看您可以使用的各种位。
并且不使用@
;
答案 3 :(得分:1)
使用此代替您拥有的内容:
$_SERVER['REMOTE_ADDR']