PHP session_start() 函数:为什么每次使用与 PHP 会话相关的任何东西时都需要它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5779744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:28:06  来源:igfitidea点击:

PHP session_start() function: Why I need it everytime I use anything related to PHP sessions

phpsessionlogout

提问by Sumit Gupta

For logging out a user from my website, I am redirecting the page to logout.phpwhere I am using session_destroy() function. Even there also, logout functionality is not working without session_start()function. By adding session_start() function before session_destroy()function, I am able to logout the user successfully.

为了从我的网站注销用户,我将页面重定向到logout.php我使用 session_destroy() 函数的位置。即使在那里,注销功能也无法正常工作session_start()。通过在函数之前添加 session_start() 函数session_destroy(),我可以成功注销用户。

Why do I need to use session_start()function everytime and in every page where I am doing something related to sessions?

为什么我session_start()每次都需要在我正在做与会话相关的事情的每个页面中使用函数?

回答by Pekka

Why do I need to use session_start() function everytime and in every page where I am doing something related to sessions?

为什么我每次都需要在每个页面中使用 session_start() 函数,我正在做与会话相关的事情?

So PHP knows which session to destroy. session_start()looks whether a session cookie or ID is present. Only with that information can you destroy it.

所以 PHP 知道要销毁哪个会话。session_start()查看是否存在会话 cookie 或 ID。只有有了这些信息,你才能摧毁它。

回答by John Cartwright

session_destroy() destroys the active session. If you do not initialized the session, there will be nothing to be destroyed.

session_destroy() 销毁活动会话。如果不初始化会话,则不会有任何东西被销毁。

回答by Jesse Bunch

In the default configuration, PHP Sessions operate off of the hard disk. PHP asks you to explicitly tell it when you need this support to avoid unnecessary disk IO.

在默认配置中,PHP 会话在硬盘上运行。PHP 要求您在需要此支持时明确告诉它,以避免不必要的磁盘 IO。

session_start()also tells PHP to find out if the user's session exists.

session_start()还告诉 PHP 找出用户的会话是否存在。

回答by Mike Lewis

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

session_start() 根据通过 GET 或 POST 请求或通过 cookie 传递的会话标识符创建会话或恢复当前会话。

as per http://php.net/manual/en/function.session-start.php

根据http://php.net/manual/en/function.session-start.php

Essentially by calling session_start(), PHP reads the header and cross references that session ID to what is on your system(file system/database/etc), which can then populate the $_SESSIONthat is relavent to that specific user. Which in turn allows you to call session_destroy()because it knows what session to actually destroy.

本质上,通过调用session_start(),PHP 读取标头并将该会话 ID 交叉引用到您的系统(文件系统/数据库/等)上的内容,然后可以填充与$_SESSION该特定用户相关的内容。这反过来又允许您调用,session_destroy()因为它知道要实际销毁哪个会话。

回答by webzy

consider session_start() as your way of telling the php engine.... that you want to work with sessions.

考虑 session_start() 作为你告诉 php 引擎的方式......你想使用会话。

and, as i understand it, always make that to be the first line ever in php page.

而且,据我所知,始终将其设为 php 页面中的第一行。

回答by KalC

I was confused with the usage of session_start(); and every time I was using a session variable, I was calling session_start. Precisely, I had session_start(); more than once on each page (without even calling session_destroy()). For example,

我对 session_start() 的用法感到困惑;每次我使用会话变量时,我都会调用 session_start。准确地说,我有 session_start(); 每个页面上不止一次(甚至没有调用 session_destroy())。例如,

// 1st call
session_start();

if (!isset($_SESSION['UserID']))    
{       
    // Do something    
}

else
{
   // Do something else
}

// .... some other code

// 2nd call
session_start();

if (!isset($_SESSION['UserID']))    
{       
    // Do something totally different   
}

else
{
   // Do something else totally different
}

This was creating a performance issue for me. So I ended up calling session_start();just once at the very top of the page and everything seems to be working fine.

这给我带来了性能问题。所以我最终session_start();只在页面顶部调用了一次,一切似乎都运行良好。

回答by Matthew

You have to call session_start once (and only once) in every file you want sessions to work in.

您必须在您希望会话工作的每个文件中调用 session_start 一次(并且仅一次)。

A common approach allowing you to only call it once is to have a dispatcher file as your index.php; call session_start in here and have this page include others based on the url's $_GET.

允许您只调用一次的常用方法是将调度程序文件作为 index.php;在此处调用 session_start 并让此页面包含基于 url 的 $_GET 的其他页面。

<?php
    session_start();
    if(isset($_GET['page']) && file_exists('pages/'.$_GET['page'].'.php') {
        include $_GET['page'];
    }
?>
//www.mysite.com/index.php?page=fish will display /pages/fish.php with session access