PHP Session_Start()函数

In PHP, information designated for use across several web pages can be stored in a session. A session is similar to a cookie, but the information contained in the session is not stored on the visitor's computer. A key to open the session—but not the information contained within—is stored on a visitor's computer.

在PHP中,可以在会话中存储指定用于多个网页的信息。 会话类似于cookie,但是会话中包含的信息未存储在访问者的计算机上。 打开会话的密钥(而不包含其中的信息)存储在访问者的计算机上。

When that visitor next logs in, the key opens the session. Then when a session is opened on another page, it scans the computer for the key. If there is a match, it accesses that session, if not it starts a new session. With sessions, you can build customized applications and increase the usefulness of the site to its visitors. 

当该访客下次登录时,该键将打开会话。 然后,在另一个页面上打开会话时,它将在计算机上扫描密钥。 如果匹配,它将访问该会话,否则将启动一个新会话。 通过会话,您可以构建自定义的应用程序并提高网站对访问者的有用性。

Every page that will use the session information on the website must be identified by the session_start() function. This initiates a session on each PHP page. The session_start function must be the first thing sent to the browser or it won't work properly. It must precede any HTML tags. Usually, the best place to position it is right after the <?php tag. It must be on every page you intend to use.

必须使用session_start()函数来标识将在网站上使用会话信息的每个页面。 这将在每个PHP页面上启动一个会话。 session_start函数必须是发送到浏览器的第一件事,否则它将无法正常工作。 它必须在任何HTML标记之前。 通常,放置它的最佳位置是在<?php标记之后。 它必须在您打算使用的每个页面上。

The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

会话中包含的变量(例如用户名和喜欢的颜色)由$ _SESSION全局变量设置。 在此示例中,session_start函数位于非打印注释之后,但位于任何HTML之前。

In the example, after viewing page 1.php, the next page, which is page 2.php, contains the session data and so on. The session variables end when the user closes the browser.

在该示例中,在查看页面1.php之后,下一页(页面2.php)包含会话数据,依此类推。 用户关闭浏览器时,会话变量结束。

修改和删除会话 ( Modifying and Deleting a Session )

To modify a variable in a session, just overwrite it. To remove all the global variables and delete the session, use the session_unset() and session_destroy() functions.

要在会话中修改变量,只需将其覆盖即可。 要删除所有全局变量并删除会话,请使用session_unset()和session_destroy()函数。

全局变量与局部变量 ( Global vs. Local Variable )

A global variable is visible throughout the program and it can be used by any function in the program. A local variable is declared inside a function and that is the only place it can be used. 

全局变量在整个程序中都是可见的,并且可以由程序中的任何函数使用。 局部变量在函数内部声明,这是可以使用的唯一位置。

翻译自: https://www.thoughtco.com/sessionstart-php-function-2694087

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值