在PHP中使用$ _SERVER

$_SERVER is one of the PHP global variables—termed Superglobals—which contain information about server and execution environments. These are pre-defined variables so they are always accessible from any class, function or file.

$ _SERVER是PHP全局变量之一,称为Superglobals,其中包含有关服务器和执行环境的信息。 这些是预定义的变量,因此始终可以从任何类,函数或文件访问它们。

The entries here are recognized by web servers, but there is no guarantee that each web server recognizes every Superglobal. These three PHP $_SERVER arrays all behave in similar ways—they return information about the file in use. When exposed to different scenarios, in some cases they behave differently. These examples may help you decide which is best for what you need. A full list of $_SERVER arrays is available at the PHP website.

Web服务器可以识别此处的条目,但不能保证每个Web服务器都可以识别每个Superglobal。 这三个PHP $ _SERVER数组的行为均相似,它们返回有关正在使用的文件的信息。 在不同情况下,它们在某些情况下的行为会有所不同。 这些示例可以帮助您确定最适合您的需求的示例。 $ _SERVER数组的完整列表可在PHP 网站上找到

$ _SERVER ['PHP_SELF'] ( $_SERVER['PHP_SELF'] )

PHP_SELF is the name of the currently executing script.

PHP_SELF是当前正在执行的脚本的名称。

  • http://www.yoursite.com/example/ -- --> /example/index.php

    http://www.yoursite.com/example/--> /example/index.php

  • http://www.yoursite.com/example/index.php -- --> /example/index.php

    http://www.yoursite.com/example/index.php--> /example/index.php

  • http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php

    http://www.yoursite.com/example/index.php?a=test--> /example/index.php

  • http://www.yoursite.com/example/index.php/dir/test -- --> /dir/test

    http://www.yoursite.com/example/index.php/dir/test--> / dir / test

When you use $_SERVER[’PHP_SELF’], it returns the file name /example/index.php both with and without the file name typed in the URL. When variables are appended at the end, they were truncated and again /example/index.php was returned. The only version that produced a different result has directories appended after the file name. In that case, it returned those directories.

当您使用$ _SERVER ['PHP_SELF']时,无论是否在URL中键入文件名,它都将返回文件名/example/index.php。 在末尾附加变量时,变量将被截断并再次返回/example/index.php。 唯一产生不同结果的版本在文件名后附加了目录。 在这种情况下,它将返回这些目录。

$ _SERVER ['REQUEST_URI'] ( $_SERVER['REQUEST_URI'] )

REQUEST_URI refers to the URI given to access a page.

REQUEST_URI是指用于访问页面的URI。

  • http://www.yoursite.com/example/ -- --> /

    http://www.yoursite.com/example/--> /

  • http://www.yoursite.com/example/index.php -- --> /example/index.php

    http://www.yoursite.com/example/index.php--> /example/index.php

  • http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php?a=test

    http://www.yoursite.com/example/index.php?a=test--> /example/index.php?a=test

  • http://www.yoursite.com/example/index.php/dir/test -- --> /example/index.php/dir/test

    http://www.yoursite.com/example/index.php/dir/test--> /example/index.php/dir/test

All of these examples returned exactly what was entered for the URL. It returned a plain /, the file name, the variables, and the appended directories, all just as they were entered.

所有这些示例完全返回为URL输入的内容。 它返回一个普通的/,文件名,变量和附加目录,所有这些都与输入时相同。

$ _SERVER ['SCRIPT_NAME'] ( $_SERVER['SCRIPT_NAME'] )

SCRIPT_NAME is the current script's path. This comes in handy for pages that need to point to themselves.

SCRIPT_NAME是当前脚本的路径。 这对于需要指向自己的页面非常有用。

  • http://www.yoursite.com/example/ -- --> /example/index.php

    http://www.yoursite.com/example/--> /example/index.php

  • http://www.yoursite.com/example/index.php -- --> /example/index.php

    http://www.yoursite.com/example/index.php--> /example/index.php

  • http://www.yoursite.com/example/index.php?a=test -- --> /example/index.php

    http://www.yoursite.com/example/index.php?a=test--> /example/index.php

  • http://www.yoursite.com/example/index.php/dir/test -- --> /example/index.php

    http://www.yoursite.com/example/index.php/dir/test--> /example/index.php

All cases here returned only the file name /example/index.php regardless of whether it was typed, not typed, or anything was appended to it.

此处的所有情况仅返回文件名/example/index.php,无论是键入,未键入还是附加任何内容。

翻译自: https://www.thoughtco.com/using-server-in-php-2693940

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值