Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-自我表单提交:$#服务器[';PHP#自我';]或操作="&引用;?_Php_Self_Form Submit - Fatal编程技术网

PHP-自我表单提交:$#服务器[';PHP#自我';]或操作="&引用;?

PHP-自我表单提交:$#服务器[';PHP#自我';]或操作="&引用;?,php,self,form-submit,Php,Self,Form Submit,我只是意识到,在一些奇怪的情况下,我在做我认为是自我提交的事情,而没有在action form属性上引用PHP_self 我很困惑,我们能不能 <?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?> ? 如果不是,在什么情况下我们应该考虑这样或那样 提前感谢,, MEM如果我没有弄错的话,Safari在后者上有问题,因此我放弃了使用它。您可以使用(PHP_SELF或空字符串)。但是为什么要使用F

我只是意识到,在一些奇怪的情况下,我在做我认为是自我提交的事情,而没有在action form属性上引用PHP_self

我很困惑,我们能不能

<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>
?

如果不是,在什么情况下我们应该考虑这样或那样

提前感谢,,
MEM

如果我没有弄错的话,Safari在后者上有问题,因此我放弃了使用它。

您可以使用(PHP_SELF或空字符串)。但是为什么要使用FILTER\u SANITIZE\u字符串进行此操作?在这种情况下,如果您的路径包含过滤字符(例如,
请不要使用PHP\u SELF,因为这也可能是
/index.PHP/“>警报(1)/
),则最好使用而不是filter\u var

它通常用于XSS攻击

改为使用索引脚本名称!脚本名称将始终指向实际的PHP文件,而不是用户输入

问候

编辑:

有两个人指出,使用mod_rewrite时,脚本名称不起作用。这是错误的,我认为这些人应该在投票否决答案之前阅读

这里有一个测试场景供您使用***:

$ cat .htaccess 
RewriteEngine On
RewriteRule testme/ /testmenot.php

$ cat testmenot.php 
<? echo $_SERVER['SCRIPT_NAME']; ?>

$ GET hostname/testme/
/testmenot.php
$cat.htaccess
重新启动发动机
重写规则testme//testmenot.php
$cat testmenot.php
$GET主机名/testme/
/testmenot.php
$\u服务器['REQUEST\u URI']
保存着“/testme/”,我猜这些人会在SCRIPT\u NAME中看到它,但在PHP\u SELF中也找不到

/我交叉手指
:E


location.href=“index.php”

为什么一个而不是另一个,你能详细解释一下吗?非常感谢。至少像“这个答案缺少
ENT_QUOTES
”这样的字符。我真的不明白这一点-为什么不使用空字符串呢?对所有可能的输入进行转义并不简单,但使用空字符串是。@eis空字符串保留了的查询字符串参数默认的标志参数包含<代码> EntEngCuTab< /COD>转换双引号。只有在使用单引号进行属性时,才需要<代码> EntType引号。是的,这意味着这个答案将中断任何单引号,这将是不可接受的。URL S的查询参数应该与页面显示有关,所以我会认为它们比不需要的情况更可能。@ NICKIC -非常感谢。@所有人都可以确认上面是否仍然适用?或者如果是常见的情况……我需要更多的证据来决定是否使用它。ort,这个错误在2009年解决了。-1,脚本名称对重写的URL不起作用。如果你检查得好,可以使用PHP SELF。@Jan:过滤器变量或htmlentities不处理这个问题吗?
$\u服务器['SCRIPT\u NAME']
不适用于重写的URL:s。如果接收页面在解析请求后重定向,则XSS不是问题。Jan,我曾经遇到过这样的情况:资源只能通过重写的URL访问;直接请求文件会产生404页面,而不进一步处理请求。由于各种原因,无法显示该页面真正的文件路径是直接的。不,简,你的建议打破了我所知道的大约80%的标准框架,而不仅仅是Lekensteyn的宠物项目。在例如/index.php中有一个单一的入口点是很常见的。但是,你是对的,应该避免XSS攻击,但是一个简单的
htmlspecialchars($var,entu QUOTES)
相应地处理这些问题,或者只列出URL和值的白名单,并检查这些URL和值以获得更细粒度的控制。
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
</form>
$ cat .htaccess 
RewriteEngine On
RewriteRule testme/ /testmenot.php

$ cat testmenot.php 
<? echo $_SERVER['SCRIPT_NAME']; ?>

$ GET hostname/testme/
/testmenot.php
     <?php
     session_start();
        $msg = '';

        if (isset($_POST['login']) && !empty($_POST['username']) 
           && !empty($_POST['password'])) {

           if ($_POST['username'] == 'abc' && 
              $_POST['password'] == 'xyz') {
              $_SESSION['valid'] = true;
              $_SESSION['timeout'] = time();
              $_SESSION['username'] = 'abc';
              ?>

              <script type="text/javascript">

          location.href="index.php"

        </script>
              <?php 
           }
           else 
           {
              $msg ='Invalid username or password';
           }
        }
     ?>
       <form
        action ="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); 
        ?>" method = "post">
              <input type = "text" class = "form-control" 
           name = "username" placeholder = "username" 
           required autofocus ></br> 
          <input type = "password" class = "form-control"
           name = "password" placeholder = "password" required>

      <input class="button" type = "submit"  name = "login" value="Log in"/>