PHP源码分析之session.auto_start配置分析

WBOY
Release: 2016-06-23 13:47:25
Original
1171 people have browsed it

  作者:zhanhailiang 日期:2014-10-20
Copy after login

应用分析

日常开发中,php.ini配置session.auto_start=0默认关闭会话时如果想开启会话需要调用session_start:

<?php session_start();//...
Copy after login

内核分析

通过查到源码,可知session_start定义如下:

1881 /* {{{ proto bool session_start(void) 1882 Begin session - reinitializes freezed variables, registers browsers etc */1883 static PHP_FUNCTION(session_start)1884 {1885         /* skipping check for non-zero args for performance reasons here ?*/1886         php_session_start(TSRMLS_C);1887 1888         if (PS(session_status) != php_session_active) {1889                 RETURN_FALSE;1890         }1891         RETURN_TRUE;1892 }
Copy after login

可以看到session_start本质是通过调用php_session_start来开启会话。

如果修改php.ini中session.auto_start=1默认开启session时通过源码可知在RINIT(即请求初始化)中自动调用php_session_start来开启会话:

2129 static PHP_RINIT_FUNCTION(session) /* {{{ */2130 {2131         php_rinit_session_globals(TSRMLS_C);2132 2133 +-----  8 lines: if (PS(mod) == NULL) {--------------------------------------------------------------------------------------------------------------------------------2141                 2142 +-----  8 lines: if (PS(serializer) == NULL) {-------------------------------------------------------------------------------------------------------------------------2150                 2151 +-----  5 lines: if (PS(mod) == NULL || PS(serializer) == NULL) {------------------------------------------------------------------------------------------------------2156                         2157         if (PS(auto_start)) {2158                 php_session_start(TSRMLS_C);2159         }2160 2161         return SUCCESS;2162 }2163 /* }}} */
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template