注意:会话全局未定义索引
我正在向我的网站添加多语言系统,但遇到了此错误。每次我尝试在 $_SESSION 上设置一些值时,它都会说
Notice: Undefined index: lang in ...\pages\lang.php on line 3
我注意到的是,当我尝试在核心文件上设置 $_SESSION 时,它会起作用,但是如果我尝试包含设置会话到核心文件,它会抛出该错误。这是 core.php 的片段:
<?php
session_start();
//some code
//if I type $_SESSION['lang'] = 'smth'; here, it will work
if (!isset($_GET['page']) || $_GET['page'] == "" || !file_exists('pages/'.$_GET['page'].'.php')) {
include 'pages/home.php';
} else {
include 'pages/'.$_GET['page'].'.php';
}
?>
pages/lang.php:
<?php
$lang = $_GET['lang'];
$_SESSION['lang'] == $lang;
var_dump($_SESSION); //prints an empty array ( array { } )
//header("Location: index.php");
?>
有什么想法吗?谢谢。
I was adding a multi-language system to my website, but came accross this error. Everytime I try to set some value on my $_SESSION, it says
Notice: Undefined index: lang in ...\pages\lang.php on line 3
What I noticed, is when I'm trying to set the $_SESSION on my core file, it works, but if I'm trying to include the file which sets the session to the core file, it throws out that error. Here's the snippet of core.php:
<?php
session_start();
//some code
//if I type $_SESSION['lang'] = 'smth'; here, it will work
if (!isset($_GET['page']) || $_GET['page'] == "" || !file_exists('pages/'.$_GET['page'].'.php')) {
include 'pages/home.php';
} else {
include 'pages/'.$_GET['page'].'.php';
}
?>
pages/lang.php:
<?php
$lang = $_GET['lang'];
$_SESSION['lang'] == $lang;
var_dump($_SESSION); //prints an empty array ( array { } )
//header("Location: index.php");
?>
Any ideas? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在使用 $_SESSION 变量的每个脚本的开头包含
session_start();
。还有一个拼写错误:
$_SESSION['lang'] == $lang;
应该只是一个“=”。You need to include
session_start();
in the start of every script that uses the $_SESSION variables.Also a typo:
$_SESSION['lang'] == $lang;
should be just one '='.