所以这是我到目前为止的代码,我记得如果IP匹配,则回显带有详细信息的表单,如果不回显错误消息。但由于某种原因,我无法让它工作
(需要添加更多细节,所以如果你愿意,可以忽略它) 基本上,脚本获取客户端的IP,如果IP匹配文件中的IP然后显示一些凭据,如果它没有回显错误消息或只是将一些文本回显到文本字段
<?php
$ip = $_SERVER['REMOTE_ADDR'];
?>
<?php
/**
* Created by PhpStorm.
* User: test
* Date: 02/03/2017
* Time: 03:15 ص
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Login</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Password</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="index.php">Passwords</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
<form method="post" style="margin-top:100px;">
<?php
if($ip == "ip1" or "ip2"){
$user = "someuser";
$pass = "somepass";
$pin = "somepin";
echo "
<div class=\"form-group\">
<label for=\"exampleInputEmail1\">Username</label>
<input type=\"email\" name=\"email\" class=\"form-control\" placeholder=\"Email\" value=\"$user \">
</div>
<div class=\"form-group\">
<label for=\"exampleInputPassword1\">Password</label>
<input type=\"text\" name=\"password\" class=\"form-control\" placeholder=\"Password\" value=\"$pass \">
</div>
<div class=\"form-group\">
<label for=\"exampleInputPassword1\">Pin</label>
<input type=\"text\" name=\"password\" class=\"form-control\" placeholder=\"Password\" value=\"$pin \">
</div>
";
}
else{
echo "
<div class=\"form-group\">
<label for=\"exampleInputEmail1\">Username</label>
<input type=\"email\" name=\"email\" class=\"form-control\" placeholder=\"Email\" value=\" LOL \">
</div>
<div class=\"form-group\">
<label for=\"exampleInputPassword1\">Password</label>
<input type=\"text\" name=\"password\" class=\"form-control\" placeholder=\"Password\" value=\" LOL \">
</div>
<div class=\"form-group\">
<label for=\"exampleInputPassword1\">Pin</label>
<input type=\"text\" name=\"password\" class=\"form-control\" placeholder=\"Password\" value=\" LOL \">
</div>
";
}
?>
</form>
</div><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
答案 0 :(得分:0)
<?php
if($ip == "ip1" or $ip =="ip2"){
$user = "someuser";
$pass = "somepass";
$pin = "somepin";
}
else
{
$user = "LOL";
$pass = "LOL";
$pin = "LOL";
}
?>
<div class="form-group">
<label for="exampleInputEmail1">Username</label>
<input type="email" name="email" class="form-control" placeholder="Email" value="<?php echo $user; ?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="text" name="password" class="form-control" placeholder="Password" value="<?php echo $pass;?>">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Pin</label>
<input type="text" name="password" class="form-control" placeholder="Password" value="<?php echo $pin;?>">
</div>
或
<?php
if($ip == "ip1" or $ip =="ip2"){
$user = "someuser";
$pass = "somepass";
$pin = "somepin";
}
else
{
$user = "LOL";
$pass = "LOL";
$pin = "LOL";
}
echo '<div class="form-group">
<label for="exampleInputEmail1">Username</label>
<input type="email" name="email" class="form-control" placeholder="Email" value="'.$user.'">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="text" name="password" class="form-control" placeholder="Password" value="'.$pass.'">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Pin</label>
<input type="text" name="password" class="form-control" placeholder="Password" value="'.$pin.'">
</div>';
?>
答案 1 :(得分:0)
首先检查$ip
值,
然后尝试这种格式
if($ip=='ip1'){
//code here
}else if($ip=='ip2'){
//code here
}else{
//code here
}
答案 2 :(得分:0)
我想如果我是你,我会使用像in_array()这样的原生函数在列表中搜索ip。如果你在||
条款中有一堆or
/ if
,那就太丑了。此外,您可以将ips存储在xml
,json
或其他外部源(如数据库)中,这样您就不必在php文件中对这些存储的值进行硬编码。
# Get the ip
$myIp = $_SERVER['REMOTE_ADDR'];
# In-php file list of ips
$listOfIps = array(
'73.121.122.131',
'213.22.132.11',
'91.89.222.32'
);
# Check in the list for the user's ip
if(in_array($myIp,$listOfIps)) {
// Do something here
}
要从外部文件中提取,它看起来像这样。我添加了一些类,使其更具人性化:
<强> /ips.json 强>
["73.121.122.131","213.22.132.11","91.89.222.32"]
<强>的index.php 强>
class FileHandler
{
public function getIpList()
{
return json_decode(file_get_contents(__DIR__.'/ips.json'),true);
}
}
class Admin
{
private $FileHandler;
public function __construct(\FileHandler $FileHandler)
{
$this->FileHandler = $FileHandler;
}
public function ipAllowed()
{
$ips = $this->FileHandler->getIpList();
$ip = $_SERVER['REMOTE_ADDR'];
return in_array($ip,$ips);
}
}
$Admin = new Admin(new FileHandler());
if($Admin->ipAllowed()) {
// Do special stuff here
}
else {
// Do regular stuff
}
答案 3 :(得分:-1)
你不能使用&#34;开始你的回声,然后在HTML中使用它们。这会创建一个字符串的结尾。用&#39;开始你的回声然后保留双引号。另外,黑曜石对于&#34;或&#34;是正确的。是||。