简体   繁体   中英

Redirect to base URL doesn't work

I need to make members only access to my index.php page. 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. So my login page should be BASE_URL page. I also did define ('BASE_URL', 'mywebsite.com/login.php'); in my verify.php script. 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)". I don't have any spaces before my HTML, that may be the cause. I also tried to set BASE_URL to my login with "base_href" in head of my login.php , but didn't worked. 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:

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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