【发布时间】:2014-02-13 09:43:15
【问题描述】:
当我在我的代码中使用 session_start() 时,我正确设置了会话,当我运行代码并登录时,它使我保持登录状态并且一切正常,但是当我在位于顶部的 PHP 代码下方添加 HTML 时页面并刷新它(登录时)它让我退出?
我的代码没有 HTML
核心文件
ob_start();
session_start();
function loggedin() {
if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])) {
return true;
} else {
return false;
}
}
require 'connect.inc.php';
require 'core.php';
include 'main_login.inc.php';
if (loggedin()) {
$dir = $user_name;
if($handle = @opendir($dir)) {
if($handle2 = @opendir($dir.'/docs')){
//If Logged In And Users File Exists: Load Page
}
} else {
// If Users File Does Not Exist: Create User File
@mkdir($user_name, 0755, true);
// Create Docs
@mkdir($user_name.'/docs', 0755, true);
}
} else {
// If User Is Not Logged: Redirect To Jamie Co Home
@header('Location: http://www.jamieco.ca');
}
?>
我的 HTML 代码(非工作代码)
<?php
require_once 'connect.inc.php';
require_once 'core.php';
include 'main_login.inc.php';
if (loggedin()) {
$dir = $user_name;
if($handle = @opendir($dir)) {
if($handle2 = @opendir($dir.'/docs')){
//If Logged In And Users File Exists: Load Page
}
} else {
// If Users File Does Not Exist: Create User File
@mkdir($user_name, 0755, true);
// Create Docs
@mkdir($user_name.'/docs', 0755, true);
}
} else {
// If User Is Not Logged: Redirect To Jamie Co Home
@header('Location: #');
}
?>
<!DOCTYPE html>
<html>
<body>
<header>
<nav>
<ul>
<li><a href = "index.html">Home</a></li>
<li><a href = "clients.html">Clients</a></li>
<li><a href = "login.html">Sign In</a></li>
<li><a href = "contact.html">Contact</a></li>
</ul>
</nav>
</header>
<div id = "clear" />
<div id = "big_wrapper">
<div id = "wrapper">
<br><br>
<!-- Logged In Stuff Goes Here -->
<div id = "user_options">
<ul>
<li><?php echo 'Welcome, '.$first_name.' '.$last_name.'<br><br>'; ?></li>
<li><a href = "logout.php">Logout</a></li>
</ul>
</div>
<?php echo 'Welcome Back '.$first_name; ?>
<br><br>
<table border = "1" cellspacing = "5" id = "files">
<tr><td colspan = "7">File Handling</td></tr>
<tr>
<td>File Name:</td>
<td>File Size:</td>
<td>File Type:</td>
<td>Security Level:</td>
<td colspan = "3">Actions:</td>
</tr>
<?php
$dir = $user_name.'/docs/';
if($handle = @opendir($dir)) {
while($file = @readdir($handle)) {
if($file!='.'&&$file!='..') {
echo '<tr><td><a href = "'.$user_name.'/'.$file.'">'.$file.'</a></td>';
$name = $dir.$file;
$size = filesize($dir.$file);
$type = substr($name, strrpos($name, '.')+1);
echo '<td>'.$size.' Bytes'.'</td>';
echo '<td>'.$type.'</td>';
echo '<td>'.$file_grade.'</td>';
echo '<td><a href = "'.$dir.$file.'">'.'Download'.'</a></td>';
echo '<td><a href = "#">'.'Rename'.'</a></td>';
echo '<td><a href = "#">'.'Delete'.'</a></td>';
}
}
}
?>
</tr>
</table>
<br><br>
<!-- Stuff Here -->
<br><br>
</div>
</div>
</body>
</html>
【问题讨论】:
-
我是瞎了吗?我在任何地方都看不到
session_start():S 你希望这是在<?php之后的代码中的第一件事 -
旁注:你有什么理由同时使用
require和include?建议使用require_once,如果您的文件计划包含一次,因为它会抛出错误而不是警告(并且将停止运行代码之后) -
旁注:如果不是最后一行,记得在
header("Location: $url");之后调用exit; -
Timmy 不,你不是盲人会话开始在页面顶部的
require core.php文件中 -
这么多错误抑制..