Home >Backend Development >PHP Tutorial >PHP predefined variables (2)_PHP tutorial

PHP predefined variables (2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:181028browse

PHP predefined variables (2)

4. Session variable ($_SESSION): The data generated by PHP's SESSION function are stored in the $_SESSION variable in the form of super global variables.
1. Introduction to Session
SESSION is also called a session, which is a small file stored on the server and used to store user-related information. Session identifiers are used to distinguish between sessions. Each SESSION has a unique identifier. You can also use SESSION to complete the same work as COOKIE. The session will automatically encode and decode the value to be set, so the session can support storing any data type, including arrays and objects.
2. Use the session_save_path() function to set the saving directory of the session file, such as:
session_save_path("D:/phpnow/Apache2/temp");
//Use the session_save_path() function to obtain the directory where the current session is saved
echo session_save_path()."
";
Note: After PHP installation is completed, since the saving path of SESSION is not set, if you use SESSION directly in the script, an error will occur because the path does not exist. Using the session_save_path() function, you can modify the save path of the SESSION file, but only for the current script. If a valid SESSION file saving directory has been set in the PHP configuration file, then this function does not need to be used in the script. At this time, all PHP programs share a directory to store SESSION files.
3. Register SESSION variable

Since $_SESSION is an array, you can use the array to add cells to add new variables to SESSION. Such as:

session_start();//Open SESSION, you must open it before using it

//Use the method of adding units to the array and add the SESSION variable

$_SESSION["username"] = "phpstuer";//Storage string

$sessionArr = array("1","2","3");
$_SESSION["arr"] = $sessionArr; //Storage array

NOTE: You can also use the session_register function to register the SESSION variable, but this function has been deprecated since PHP5.3, so I won’t give an example here.

4. Access SESSION variable
Since SESSION variables are stored in the $_SESSION global variable in the form of units, you can access the SESSION variable using the array access method. After the SESSION variable is registered, it needs to be initialized before it can be used. This is different from COOKIE. The function to initialize SESSION is session_start(). Such as:
//Use the session_start() function to start SESSION
session_start();
//Traverse the $_SESSION array
foreach($_SESSION as $key=>$value){
echo "$key=>$value
";
}
echo "Access SESSION variable individually:";
echo $_SESSION["username"]."
";
echo "Access SESSION variable alone:";
echo $_SESSION["arr"][2];

Note: When using the session_start() function, to prevent error messages from appearing, add code at the top of the page.

5. Delete SESSION variable
After using the SESSION variable, you need to delete the registered SESSION to reduce the use of server resources.
session_start();
//Before deletion:
echo "

";<br>
print_r($_SESSION);<br>
echo "
";
//Use the unset() function to delete a $_SESSION variable
unset($_SESSION["username"]);
//Use the session_unset() function to delete the $_SESSION variable of the current page
//session_unset();
//Use the session_destroy() function to delete the $_SESSION variable of the current page and delete the SESSION file
//session_destroy();

//After deletion:
echo "

";<br>
print_r($_SESSION);<br>
echo "
";

5. The Request variable ($_REQUEST) is a super global variable. $_REQUEST stores data including $_POST, $_GET, and $_COOKIE variables. By accessing $_REQUEST, you can also implement the functions of $_POST, $_GET, and $_COOKIE variables.
Although $_REQUEST is more convenient to use, the data it provides has certain risks. For example, when a program uses $_REQUEST to read the $_COOKIE value, an interested visitor can simulate the $_COOKIE value by providing a $_GET variable to obtain certain access rights.
Usage examples: omitted.

六、服务器变量($_SERVER)是由网络服务器创建的数组,其内容包括头信息、路径、脚本位置等。不同的网络服务器提供的信息有所出入,下面实例是以Apache服务器提供的作为标准。
echo "

";<br>
print_r($_SERVER);<br>
echo "
";
//解释如下:
echo "显示脚本文件的相对路径和文件名:\"".$_SERVER["PHP_SELF"]."\"
";
echo "显示服务器使用的CGI脚本规范:\"".$_SERVER["GATEWAY_INTERFACE"]."\"
";
echo "显示当前运行脚本所在服务器的IP地址:\"".$_SERVER["SERVER_ADDR"]."\"
";
echo "显示当前运行脚本服务器名称:\"".$_SERVER["SERVER_NAME"]."\"
";
echo "显示当前运行脚本服务器标识:\"".$_SERVER["SERVER_SOFTWARE"]."\"
";
echo "显示请求页面的通信协议的名称和版本:\"".$_SERVER["SERVER_PROTOCOL"]."\"
";
echo "显示访问页面的请求方法:\"".$_SERVER["REQUEST_METHOD"]."\"
";
echo "显示脚本开始运行时间:\"".$_SERVER["REQUEST_TIME"]."\"
";
echo "显示URL问号后的字符串:\"".$_SERVER["QUERY_STRING"]."\"
";
echo "显示当前运行脚本的文档根目录:\"".$_SERVER["DOCUMENT_ROOT"]."
";
echo "显示当前Accept请求的头信息:\"".$_SERVER["HTTP_ACCEPT"]."\"
";
echo "显示当前请求的字符信息:\"".$_SERVER["HTTP_ACCEPT_CHARSET"]."\"
";
echo "显示当前当前请求的Accept-Encoding头信息:\"".$_SERVER["HTTP_ACCEPT_ENCODING"]."\"
";
echo "显示当前请求的Accept-Language头信息:\"".$_SERVER["HTTP_ACCEPT_LANGUAGE"]."\"
";
echo "显示当前请求的Connection头信息:\"".$_SERVER["HTTP_CONNECTION"]."\"
";
echo "显示当前请求的Host头信息:\"".$_SERVER["HTTP_HOST"]."\"
";
echo "显示当前页面的前一个页面的URL地址:\"".$_SERVER["HTTP_REFERER"]."\"
";
echo "显示当前请求的User-Agent的头信息:\"".$_SERVER["HTTP_USER_AGENT"]."\"
";
echo "显示脚本是否可以通过HTTPS协议进行访问:\"".$_SERVER["HTTPS"]."\"
";
echo "显示浏览当前页面用户的IP地址:\"".$_SERVER["REMOTE_ADDR"]."\"
";
echo "显示浏览当前页面用户的主机名:\"".$_SERVER["REMOTE_HOST"]."\"
";
echo "显示用户连接到服务器时所使用的端口:\"".$_SERVER["REMOTE_PORT"]."\"
";
echo "显示当前执行脚本的绝对路径名:\"".$_SERVER["SCRIPT_FILENAME"]."\"
";
echo "显示Apache配置文件中的SERVER_ADMIN参数设置情况:\"".$_SERVER["SERVER_ADMIN"]."\"
";
echo "显示网络服务器使用的端口,默认为\"80\":\"".$_SERVER["SERVER_PORT"]."\"
";
echo "显示服务器版本和虚拟主机名的字符串:\"".$_SERVER["SERVER_SIGNATURE"]."\"
";
echo "显示脚本在文件系统中的基本路径:\"".$_SERVER["PATH_TRANSLATED"]."\"
";
echo "显示当前脚本的路径:\"".$_SERVER["SCRIPT_NAME"]."\"
";
echo "显示访问当前页面的URI:\"".$_SERVER["REQUEST_URI"]."\"
";
七、环境变量($_ENV)是预定义的一个数组,记录着系统路径等信息。
echo "
";<br>
print_r($_SERVER);<br>
echo "
";
//单独访问环境变量的数组成员,可以通过“$_ENV[‘成员变量名’]”的方式实现,如:
echo "服务器操作系统为:".$_ENV["OS"]."
";
八、 HTTP文件上传变量($_FILES):由HTML表单生成的文件上传变量,以数组的形式记录了上传文件的详细信息,其数组成员包括上传的文件名:name;文件类型:type;临时文件名:tmp_name;错误信息代码 :error;文件大小:size。下面介绍使用HTML表单,生成文件上传变量的方法,
要使表单产生文件变量,要满足三个条件:
1、HTML表单要使用POST方式传递数据。
2、表单的“enctype”参数要设置为“multipart/form-data” 。
3、表单中包含一个文件选择框。
通过$_FILES变量获取上传文件相关信息后,就可以配合其他文件函数实现文件的上传。
上传文件及$_FILES的用法实例:待整理
九、$GLOBALS变量以数组的形式,记录了所有已经定义的全局变量,而变量名就是这个数组的索引。即然$GLOBALS记录着所有的全局变量,那么理所当然的也可以访问全局变量里的信息。下面介绍使用$GLOBALS访问其他预定义变量的方法,如:
session_start();
$_SESSION["school"] = "大学";
echo $_SESSION["school"]."
"; //输出 大学
//使用$GLOBALS显示当前操作系统版本
echo $GLOBALS["_ENV"]["OS"]."
";
//使用$GLOBALS访问SESSION值
echo $GLOBALS["_SESSION"]["school"]."
"; //输出大学

echo "

";<br>
print_r($GLOBALS);<br>
echo "
";



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/905598.htmlTechArticlePHP predefined variables (2) 4. Session variable ($_SESSION): Data generated by PHP's SESSION function , are stored in the $_SESSION variable as super global variables. 1. Session introduction...
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