2013-11-04 41 views
0

我有下面的代碼,但是我在這裏封鎖..PHP登錄頭部位置

<?php 
require 'members/core/init.php'; 
$general->logged_in_protect(); 

if (empty($_POST) === false) { 

$username = trim($_POST['username']); 
$password = trim($_POST['password']); 

if (empty($username) === true || empty($password) === true) { 
    $errors[] = 'Sorry, but we need your username and password.'; 
} else if ($users->user_exists($username) === false) { 
    $errors[] = 'Sorry that username doesn\'t exists.'; 
} else if ($users->email_confirmed($username) === false) { 
    $errors[] = 'Sorry, but you need to activate your account. 
       Please check your email.'; 
} else { 
    if (strlen($password) > 18) { 
     $errors[] = 'The password should be less than 18 characters, without spacing.'; 
    } 
    $login = $users->login($username, $password); 
    if ($login === false) { 
     $errors[] = 'Sorry, that username/password is invalid'; 
    }else { 
     session_regenerate_id(true);// destroying the old session id and creating a new one 
     $_SESSION['id'] = $login; 
     header('Location: http://www.site.ro/1/index.html'); 
     exit(); 
    } 
} 
} 
?> 

及以下:

<?php if ($page == 'login'): ?> 
<?php endif; ?> 

有人可以幫助我的代碼部分相結合?

我tryed這一點,但告訴我,頭已經發送..

<?php if ($page == 'login'): ?> 
<?php 
require 'members/core/init.php'; 
$general->logged_in_protect(); 

if (empty($_POST) === false) { 

$username = trim($_POST['username']); 
$password = trim($_POST['password']); 

if (empty($username) === true || empty($password) === true) { 
    $errors[] = 'Sorry, but we need your username and password.'; 
} else if ($users->user_exists($username) === false) { 
    $errors[] = 'Sorry that username doesn\'t exists.'; 
} else if ($users->email_confirmed($username) === false) { 
    $errors[] = 'Sorry, but you need to activate your account. 
       Please check your email.'; 
} else { 
    if (strlen($password) > 18) { 
     $errors[] = 'The password should be less than 18 characters, without spacing.'; 
    } 
    $login = $users->login($username, $password); 
    if ($login === false) { 
     $errors[] = 'Sorry, that username/password is invalid'; 
    }else { 
     session_regenerate_id(true);// destroying the old session id and creating a new one 
     $_SESSION['id'] = $login; 
     header('Location: http://www.site.ro/1/index.html'); 
     exit(); 
    } 
} 
} 
?> 
<?php endif; ?> 

在PHP林初學,所以請理解我。 預先感謝您。

已解決!

,它的最終代碼:

<?php if ($page == 'login'): 
require 'members/core/init.php'; 
$general->logged_in_protect(); 

if (empty($_POST) === false) { 

$username = trim($_POST['username']); 
$password = trim($_POST['password']); 

if (empty($username) === true || empty($password) === true) { 
    $errors[] = 'Sorry, but we need your username and password.'; 
} else if ($users->user_exists($username) === false) { 
    $errors[] = 'Sorry that username doesn\'t exists.'; 
} else if ($users->email_confirmed($username) === false) { 
    $errors[] = 'Sorry, but you need to activate your account. 
       Please check your email.'; 
} else { 
    if (strlen($password) > 18) { 
     $errors[] = 'The password should be less than 18 characters, without spacing.'; 
    } 
    $login = $users->login($username, $password); 
    if ($login === false) { 
     $errors[] = 'Sorry, that username/password is invalid'; 
    }else { 
     session_regenerate_id(true);// destroying the old session id and creating a new one 
     $_SESSION['id'] = $login; 
     header('Location: http://www.site.ro/1/index.html'); 
     exit(); 
    } 
} 
} 
endif; ?> 
+0

如果您在header()之前寫出任何內容(甚至是第一個<?php!之前的空格),您將看到此錯誤。 – idmean

+0

這裏的兩個''塊用正在發送的新行分開。 – Cthulhu

+0

我認識弟弟,這就是爲什麼我要求你的幫助! –

回答

2

一旦任何內容發送給用戶,則不能修改標題。這包括任何錯誤的空白或換行符。

<?php if ($page == 'login'): ?> 
<?php 
require 'members/core/init.php'; 
$general->logged_in_protect(); 

您停止PHP執行足夠長的時間來發送線1和2

更改爲以下之間的一個換行符,然後再試一次。

<?php if ($page == 'login'): 

require 'members/core/init.php'; 
$general->logged_in_protect(); 
+0

這是正確的答案。你有我的尊重。乾杯兄弟! –

+0

實際上,最好正確地關閉問題並單擊Castis答案旁邊的白色複選標記,直到它變成綠色,因爲它確實是正確的答案。 @AvadaniiClaudiu –

0

您的代碼中某處可能有一些空白。

  1. 你可以找到它。

  2. 還是這個最簡單的解決方法是打開output buffering

什麼輸出緩衝允許你做的是你要回聲/打印任何東西,並仍然保持在任何地方發送頭信息的能力,你的頁。