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

发布于 2024-11-03 15:56:48 字数 270 浏览 3 评论 0原文

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

为什么我需要在每次执行与会话相关的操作的每个页面中使用 session_start() 函数?

For logging out a user from my website, I am redirecting the page to logout.php where 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

倥絔 2024-11-10 15:56:48

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

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

红衣飘飘貌似仙 2024-11-10 15:56:48

为什么我需要在每次执行与会话相关的操作的每个页面中使用 session_start() 函数?

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

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

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.

人疚 2024-11-10 15:56:48

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

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

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.

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

入怼 2024-11-10 15:56:48

session_start() 创建一个会话或
基于 a 恢复当前的
通过 GET 传递的会话标识符或
POST 请求,或通过 cookie 传递。

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

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

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.

as per 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 $_SESSION that is relavent to that specific user. Which in turn allows you to call session_destroy() because it knows what session to actually destroy.

秉烛思 2024-11-10 15:56:48

将 session_start() 视为告诉 php 引擎您想要使用会话的方式。

而且,据我了解,始终将其作为 php 页面中的第一行。

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

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

↘紸啶 2024-11-10 15:56:48

我对 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
}

这给我带来了性能问题。所以我最终在页面的最顶部调用了一次 session_start(); ,一切似乎都工作正常。

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,

// 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.

影子是时光的心 2024-11-10 15:56:48

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

允许您只调用一次的常见方法是使用一个调度程序文件作为您的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

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

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.

<?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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文