PHP-Echo在if语句中不执行任何操作

PHP-Echo在if语句中不执行任何操作,php,Php,这就是我到目前为止的代码,我想如果IPs匹配,那么就回显一个带有详细信息的表单,如果不回显一个错误消息。但由于某种原因,我不能让它工作 (需要添加更多详细信息,如果需要,请忽略此项) 基本上,脚本会获取客户端的IP,如果IP与文件中的IP匹配,则会显示一些凭据,如果没有,则会回显错误消息,或者只是将一些文本回显到文本字段中 <?php $ip = $_SERVER['REMOTE_ADDR']; ?> <?php /** * Created by PhpStorm

这就是我到目前为止的代码,我想如果IPs匹配,那么就回显一个带有详细信息的表单,如果不回显一个错误消息。但由于某种原因,我不能让它工作

(需要添加更多详细信息,如果需要,请忽略此项) 基本上,脚本会获取客户端的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>

登录
切换导航
您不能使用“启动回音,然后在HTML中使用它们。这会创建字符串的结尾。以”启动回音,并保留双引号。另外,黑曜石关于“或”是正确的。


用户名

首先检查
$ip

然后试试这种格式

if($ip=='ip1'){
//code here
}else if($ip=='ip2'){
//code here
}else{
//code here
}

我想如果我是你,我会使用一个本地函数,比如在一个列表中搜索ip。如果你在一个
if
子句中有一堆
|
,那就太难看了。此外,你可以将ip存储在
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
}

您永远不会得到名为
ip1
ip2
的IP地址。此外,PHP中的“or”条件是
|
如果($IP==“ip1”|$IP==“ip2”){
我知道我只是将它们设置为ip1或ip2,因为我想隐藏我拥有的原始IP,它们只是字符串。@Obsidiagei刚刚更新了回音,以',但我不明白你说的是什么意思,并将双引号保留为is@common从某种意义上说,实际上这是代码中的几个错误之一。也许你应该告诉我们ed你花了很多时间在我的建议上,以便为初学者提供解决方案。第一个版本实际上是我代码的最初修订版之一,但我更改了它,因为我认为它是错误的,因为if($ip==“ip1”或“ip2”)不起作用,或者/| |都不起作用“有什么想法吗?谢谢!我完全忘记了其他的想法如果老实说我对其中的一半不太了解,想解释一下吗?如果你有一个“特殊”IP的列表,你可以在_array()中使用
在列表中搜索。假设你有10个ip地址,就像其他人建议的那样,你需要做大量的
if
或一个大的
switch
语句来检查ip。这是大量的硬编码。如果你有一个列表,你只需检查当前ip是否在列表中。这样你就可以d添加到列表或从列表中减去,您不必更改任何逻辑,只需删除/添加到列表。
["73.121.122.131","213.22.132.11","91.89.222.32"]
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
}