简体   繁体   English

重定向到基本URL不起作用

[英]Redirect to base URL doesn't work

I need to make members only access to my index.php page. 我需要使成员仅访问我的index.php页面。 So my php script at the beginning of my index.php file is checking if user name is in session, and if it's not it should automatically redirect user to my login.php page. 因此,位于index.php文件开头的php脚本正在检查用户名是否在会话中,如果不是,则应自动将用户重定向到我的login.php页面。 So my login page should be BASE_URL page. 所以我的登录页面应该是BASE_URL页面。 I also did define ('BASE_URL', 'mywebsite.com/login.php'); 我也define ('BASE_URL', 'mywebsite.com/login.php'); in my verify.php script. 在我的verify.php脚本中。 But when I navigate to my index page I only get (blank screen) error "An error occurred in script'path/index.php' on line 11: Cannot modify header information - headers already sent by (output started at path/index.php:1)". 但是,当我导航到索引页面时,我只会得到(黑屏)错误“第11行的脚本'path / index.php'中发生错误:无法修改标头信息-已发送的标头(输出从路径/索引处开始。 php:1)”。 I don't have any spaces before my HTML, that may be the cause. HTML之前没有空格,这可能是原因。 I also tried to set BASE_URL to my login with "base_href" in head of my login.php , but didn't worked. 我还尝试将BASE_URL设置为我的login.php head的“ base_href”登录名,但没有成功。 This is my script at the begining of my index file: 这是我的索引文件开头的脚本:

<?php 
require_once ('verify.php'); 
$page_title = 'title';
ob_start();
session_start();
if (!isset($page_title)) {
$page_title = 'title';}
if (!isset($_SESSION['name'])) {    
$url = BASE_URL . ''; 
ob_end_clean();
header("Location: $url ");
exit();
}
?>
<!DOCTYPE html><html><head></head><body></body></html>
<?php
ob_end_flush();
?>

My verify.php content: 我的verify.php内容:

<?php 
define('LIVE', TRUE);
define('EMAIL', 'my_mail');
define ('BASE_URL', 'mywebsite.com/login.php');
define ('MYSQL', 'db.php');
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
$message = "<p>An error occurred in script '$e_file' on line $e_line:      $e_message\n<br />"; 
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n</p>";
if (!LIVE) { echo '<div class="error">' . $message . '</div><br />';

} else {        
    mail(EMAIL, 'Site Error!', $message, 'From: you@youremail.com');
    if ($e_number != E_NOTICE) {
    echo '<div class="error">A system error occurred. We apologize for the inconvenience.</div><br />';
    }   }     } 
 set_error_handler ('my_error_handler');
 ?>

I found the solution. 我找到了解决方案。 The answer is to encode in UTF8 without BOM. 答案是在没有BOM的UTF8中进行编码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM