我收到的错误高于students.php
会话已经启动 - 在...中忽略session_start()
我有一个CRUD,如果学生已被更新,删除或创建,我必须通知用户。
CRUD工作正常,但是,如果我移除session_start()
中的server.php
,则通知无法显示。
students.php:
<?php if (isset($_SESSION['msg'])): ?>
<div class="msg">
<?php
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
server.php
if(!isset($_SESSION))
{
session_start();
}
...
$_SESSION['msg'] = "New student saved";
header('location: students.php'); //redirect back to page
$_SESSION['msg'] = "Information updated";
header('location: students.php'); //redirect back to page
redirect.php
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
我应该将var $_SESSION['msg']
更改为另一个变量吗?我是PHP的初学者,对不起,如果这可能是一个愚蠢的问题。
答案 0 :(得分:-1)
看起来你没有框架,所以我认为有一些一般的技巧可以帮助你的脚本成功。
1)在您的网站根目录中放置一个顶级配置文件,该文件包含在所有MAIN页面的顶部:
<强> /config.php 强>
<?php
# Create some helpful constants
define('ROOT_DIR',__DIR__);
define('DS',DIRECTORY_SEPARATOR);
define('FUNCTIONS',ROOT_DIR.DS.'functions');
define('BASE_URL','http://www.example.com');
# This includes our function loader (see below)
include_once(FUNCTIONS.DS.'loadFunc.php');
# Load the getSession() function (see below)
loadFunc('getSession');
# Start session here
session_start();
2)然后创建一些有用的函数(我将学习面向对象的编程,这样你就可以有效地使用框架,但函数总比没有好):
<强> /functions/loadFunc.php 强>
function loadFunc($name)
{
if(!is_array($name))
$name = array($name);
foreach($name as $func) {
# If the function is already loaded, skip
if(function_exists($func))
continue;
# See if the function file exists
if(is_file($file = FUNCTIONS.DS.$func.'.php'))
include_once($file);
}
}
}
<强> /functions/getSession.php 强>
function getSession($key=false,$clear=false)
{
# If requesting a specific value return that
if(!empty($key)) {
# Get value
$value = (isset($_SESSION[$key]))? $_SESSON[$key] : false;
# If the key is set and clear is set, clear the value
if(isset($_SESSON[$key]) && $clear) {
unset($_SESSON[$key]);
}
# Return session value
return $value;
}
# No key set, return full session
return $_SESSION;
}
<强> /functions/setSession.php 强>
function setSession($key,$value)
{
$_SESSION[$key] = $value;
}
<强> /functions/redirect.php 强>
function redirect($to,$exit=true)
{
header("Location: {$to}");
# You should exit on redirect
if($exit)
exit;
}
3)现在,当你去创建一个页面时,你在页面顶部包含了这个配置ONCE:
<强> /students.php 强>
<?php
# Now that you include this, you know session is always set
include(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Get the key msg from the session and use parameter #2 to clear the session key
$msg = getSession('msg',true);
# It's not empty, write message
if($msg): ?>
<div class="msg">
<?php echo $msg ?>
</div>
<?php endif ?>
<强> /server.php 强>
<?php
# Include config
include(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Load our two required functions
loadFunc(array('setSession','redirect'));
# You have to determine either message here, not both.
# Using pseudo-code here to demonstrate
if($add) {
# Use function to set session value
setSession('msg',"New student saved");
}
elseif($update) {
setSession('msg',"Information updated");
}
# Since both use the same redirect, you only need it once
redirect('students.php');
因此,如果删除除配置之外的所有文件中的所有session_start()
,然后始终在顶部的顶级页面中包含配置,则不会遇到会话错误。