PHP session_start();在页面顶部启动时不起作用 [英] PHP session_start(); isn't working when is initiated at the top of page

查看:29
本文介绍了PHP session_start();在页面顶部启动时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常新奇的问题.

I'm encountering a very new and strange issue.

通常 session_start(); 在每个页面的顶部启动,但在我的情况下,它仅在将其放置在包含的数据库连接文件下方时才起作用,例如

Usually session_start(); is initiated at the top of every page but in my case it's only serving the purpose when it is placed below a included database connection file, something like,

<?php
include 'connection.php'; 
session_start(); 
?>

session_start(); 放反时不起作用,

<?php
session_start();
include 'connection.php';     
?>

基本上这些值是在另一个文件的会话变量中启动并存储的,这些应该打印在我遇到这个奇怪问题的页面上.

Basically the values were initiated and stored in session variables in another file and these should be printed on the page where I'm encountering this strange problem.

这个函数如此奇怪的行为背后的可能原因是什么?

What could be the possible reason behind such strange behavior of this function?

推荐答案

如果包含的文件是会话处理程序,则会话应在会话处理程序下方启动.在下面的代码中 connection.php 正在处理会话并创建到数据库的连接.因此,以下代码应正确启动会话.

If the included file is a session handler, session should be started below the session handler. In the following code connection.php is handling session and creating connection to the database. Therefore, the following code shall initiate the session correctly.

<?php
include 'connection.php'; ///// is a session handler
session_start();          ///// shall serve the purpose.
?>

另一方面,当代码以相反的顺序放置时不会启动会话,

on the other hand session won't be initiated when the codes are put in reverse order like,

<?php
session_start();          ///// shan't serve the purpose.
include 'connection.php'; ///// is a session handler
?>

这篇关于PHP session_start();在页面顶部启动时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆