search
Homephp教程php手册PHP学习笔记:环境变量

PHP学习笔记:环境变量

Jun 06, 2016 pm 07:58 PM
phpvariablestudyOverviewenvironmentnoteswant

1. 概述 PHP环境变量主要有: $GLOBALS[]:储存当前脚本中的所有全局变量,其KEY为变量名,VALUE为变量 $_SERVER[]:当前WEB服务器变量数组 $_GET[]:存储以GET方法提交表单中的数据 $_POST[]:存储以POST方法提交表单中的数据 $_COOKIE[]:取得或设置用户浏

1. 概述
    PHP环境变量主要有:
    $GLOBALS[]:储存当前脚本中的所有全局变量,其KEY为变量名,VALUE为变量值
    $_SERVER[]:当前WEB服务器变量数组
    $_GET[]:存储以GET方法提交表单中的数据
    $_POST[]:存储以POST方法提交表单中的数据
    $_COOKIE[]:取得或设置用户浏览器Cookies中存储的变量数组
    $_FILES[]:存储上传文件提交到当前脚本的数据
    $_ENV[]:存储当前WEB环境变量
    $_REQUEST[]:存储提交表单中所有请求数组,其中包括:$_GET、$_POST、$_COOKIE和$_SESSION中的所有内容
    $_SESSION[]:存储当前脚本的会话变量数组

2.  $GLOBALS[]
     GLOBALS是由已定义全局变量组成的数组,变量名就是数组的索引,eg:

$ENTER = "<br>";
$var1 = 5;  
$var2 = 10;  
print $GLOBALS['var1'] . $ENTER ;
print $GLOBALS['var2'] . $ENTER ;
     GLOBALS和global相似,但也有一些区别,参考:http://www.neatstudio.com/show-644-1.shtml
3.  $_SERVER[]
       _SERVER是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等信息的数组,eg:
$ENTER = "<br>";
print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER;

       参考:http://www.php.net/manual/zh/reserved.variables.server.php

4. $_GET和_POST
    $_GET和_POST用来接收请求数据,实现输入
    $_GET内容通过 URL 参数传递给当前脚本的变量的数组。    
    $_POST内容是由 HTTP POST方法发送的变量名称和值。 
    GET是HTTP中最原始的请求方式,在网页中点击一个超级链接或在地址栏输入一个URL都会发送一个GET请求。在GET请求中,数据是后缀在URL后面来发送的,like:http://192.168.21.133/test1.php?name_get=zxm&age_get=23
    POST方法的主要用途就是传递数据,它将数据放在所有请求标题的后面上传,这样一来,无论有多少数据上传都不成问题了
 eg:
HTML:

Name: Age:
Name: Age:
php:
//_GET
echo "name:" . $_GET["name_get"] . $ENTER ;
echo "age:" . $_GET["age_get"] . $ENTER;

//_POST
echo "name:" . $_POST["name_post"] . $ENTER ;
echo "age:" . $_POST["age_post"] . $ENTER;
?>

5. $_FILE  
      通过$_FILES,我们可以从客户计算机向远程服务器上传文件。
      上传文件表单:


PHP:
//_FILE
echo "Error: " . $_FILES["file"]["error"] . "<br>";
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
6. _COOKIE[]
    cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制, 常用于识别用户,每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie,通过$_COOKIE[],我们可以将cookie取出。
设置cookie:
<?php setcookie("user", "zxm");                                                                                                                                                             
?>  
读取cookie:
//cookie
if (isset($_COOKIE['user']))
  echo "Welcome " . $_COOKIE['user'] . "!<br>";
else
  echo "Welcome guest!<br>";
7. _SESSION[]
    _SESSION[]存储了有关用户会话的信息。
 eg:
<?php session_start();
$_SESSION['id'] = "100";
?>
php:
//session
if(isset($_SESSION['id']))
        echo "session id: " . $_SESSION['id'] . $ENTER;
else
        echo "without session" . $ENTER;

?>
7. _ENV[]
    php中的$_ENV存储了一些系统的环境变量,因环境不同而值不同.
8. _REQUEST[]
    $_REQUEST包含了$_GET、$_POST、$_COOKIE的所有内容
    $_GET、$_POST、$_COOKIE在$_REQUEST中都有一个副本。改变$_REQUEST的值不影响$_GET等,反之亦然。
    在同名的情况下,优先级:$_GET  EG:
//request
echo "request: " . $_REQUEST['name_get'] . "!<br>";
?>
8. 测试PHP程序:




";

//__GLOBALS[]
$var1 = 5;  
$var2 = 10;  
print $GLOBALS['var1'] . $ENTER ;
print $GLOBALS['var2'] . $ENTER ;

//_SERVER
print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER;

//_GET
echo "name:" . $_GET["name_get"] . $ENTER ;
echo "age:" . $_GET["age_get"] . $ENTER;

//_POST
echo "name:" . $_POST["name_post"] . $ENTER ;
echo "age:" . $_POST["age_post"] . $ENTER;

//_FILE
echo "Error: " . $_FILES["file"]["error"] . $ENTER;
echo "Upload: " . $_FILES["file"]["name"] . $ENTER;
echo "Type: " . $_FILES["file"]["type"] . $ENTER;
echo "Size: " . ($_FILES["file"]["size"] / 1024) . $ENTER;
echo "Stored in: " . $_FILES["file"]["tmp_name"] . $ENTER;

//cookie
if (isset($_COOKIE['user']))
	echo "Welcome: " . $_COOKIE['user'] . "!
"; else echo "Welcome guest!
"; //session if(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER; else echo "without session" . $ENTER; //request echo "request: " . $_REQUEST['name_get'] . "!<br>"; ?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment