简体   繁体   English

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

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

For logging out a user from my website, I am redirecting the page to logout.php where I am using session_destroy() function.为了从我的网站注销用户,我将页面重定向到logout.php ,在那里我使用 session_destroy() 函数。 Even there also, logout functionality is not working without session_start() function.即使在那里,如果没有session_start()函数,注销功能也无法工作。 By adding session_start() function before session_destroy() function, I am able to logout the user successfully.之前加入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()函数,我正在做与会话相关的事情?

session_destroy() destroys the active session. session_destroy() 销毁活动会话。 If you do not initialized the session, there will be nothing to be destroyed.如果不初始化会话,则不会有任何东西被销毁。

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.所以 PHP 知道要销毁哪个会话。 session_start() looks whether a session cookie or ID is present. session_start()查看是否存在会话 cookie 或 ID。 Only with that information can you destroy it.只有有了这些信息,你才能摧毁它。

In the default configuration, PHP Sessions operate off of the hard disk.在默认配置中,PHP 会话在硬盘上运行。 PHP asks you to explicitly tell it when you need this support to avoid unnecessary disk IO. PHP 要求您在需要此支持时明确告诉它以避免不必要的磁盘 IO。

session_start() also tells PHP to find out if the user's session exists. session_start()还告诉 PHP 找出用户的会话是否存在。

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 $_SESSION that is relavent to that specific user.本质上,通过调用session_start() ,PHP 读取标头并将该会话 ID 交叉引用到您的系统(文件系统/数据库/等)上的内容,然后可以填充与该特定用户相关的$_SESSION Which in turn allows you to call session_destroy() because it knows what session to actually destroy.这反过来又允许您调用session_destroy()因为它知道要实际销毁哪个会话。

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 页面中的第一行。

I was confused with the usage of session_start();我对 session_start() 的用法感到困惑; and every time I was using a session variable, I was calling session_start.每次我使用会话变量时,我都会调用 session_start。 Precisely, I had session_start();准确地说,我有 session_start(); more than once on each page (without even calling session_destroy()).每个页面上不止一次(甚至没有调用 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();所以我最终调用了session_start(); just once at the very top of the page and everything seems to be working fine.仅在页面最顶部一次,一切似乎都运行良好。

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;允许您只调用一次的常用方法是将调度程序文件作为 index.php; call session_start in here and have this page include others based on the url's $_GET.在此处调用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要在PHP中使用session_start()来使用$ _SESSION吗? - Do I need to use session_start() in PHP to use $_SESSION? PHP会话和session_start() - PHP sessions and session_start() 为什么会出现此PHP session_start()错误? - Why am I getting this PHP session_start() error? 如果我在函数中使用session_start,它将启动会话,但不输出会话 - If i use session_start in function it starts session but doesn't output sessions 我在不使用session_start()的情况下在不同的php文件中使用了变量。 我对session_start()缺少什么,什么时候应该使用此函数? - I use variables in different php files without using session_start(). What am I missing about session_start() and when should I use this function? 为什么当我使用 session_start() 时我的 PHP 文件停止执行? - Why does my PHP file stop executing when I use session_start()? 为什么我不能在php脚本中使用session_start()? 它说标题已经发送 - Why can't I use session_start() in my php script? It says headers are already sent 我可以在不使用 session_start 的情况下获得帮助重写我的 php 代码吗? 如果我需要附加任何其他内容,请告诉我 - Can I get help re-writing my php code without using session_start? If I need to attach anything else please let me know 为什么session_start()函数在php构造函数中不起作用? - Why does session_start() function does not work in php constructor? PHP中的session_start() - session_start() in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM