2017-07-04 16 views
0

を送った - すでに(出力によって送られたヘッダが../GoogleAuth/index.phpで開始しました:2)../GoogleAuth/index.phpの30行目。 HEREは、ヘッダー情報を変更することはできません - ヘッダが既に私は2要素認証のためのgoogleAuthを使用して、それが「ヘッダー情報を変更することはできませんエラー</p> <p>下になっ

はindex.phpを

<?php 
 
include("config.php"); 
 
if(!empty($_SESSION['uid'])) 
 
{ 
 
    header("Location: device_confirmations.php"); 
 
} 
 

 
include('class/userClass.php'); 
 
$userClass = new userClass(); 
 

 
require_once 'googleLib/GoogleAuthenticator.php'; 
 
$ga = new GoogleAuthenticator(); 
 
$secret = $ga->createSecret(); 
 

 
$errorMsgReg=''; 
 
$errorMsgLogin=''; 
 
if (!empty($_POST['loginSubmit'])) 
 
{ 
 
$usernameEmail=$_POST['usernameEmail']; 
 
$password=$_POST['password']; 
 
if(strlen(trim($usernameEmail))>1 && strlen(trim($password))>1) 
 
    { 
 
    $uid=$userClass->userLogin($usernameEmail,$password,$secret); 
 
    if($uid) 
 
    { 
 
     $url=BASE_URL.'device_confirmations.php'; 
 
     header("Location: $url"); 
 
    } 
 
    else 
 
    { 
 
     $errorMsgLogin="Please check login details."; 
 
    } 
 
    } 
 
} 
 

 
if (!empty($_POST['signupSubmit'])) 
 
{ 
 

 
\t $username=$_POST['usernameReg']; 
 
\t $email=$_POST['emailReg']; 
 
\t $password=$_POST['passwordReg']; 
 
    $name=$_POST['nameReg']; 
 
\t $username_check = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $username); 
 
\t $email_check = preg_match('~^[a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~i', $email); 
 
\t $password_check = preg_match('~^[[email protected]#$%^&*()_]{6,20}$~i', $password); 
 

 
\t if($username_check && $email_check && $password_check && strlen(trim($name))>0) 
 
\t { 
 
    
 
    $uid=$userClass->userRegistration($username,$password,$email,$name,$secret); 
 
    if($uid) 
 
    { 
 
    \t $url=BASE_URL.'device_confirmations.php'; 
 
    \t header("Location: $url"); 
 
    } 
 
    else 
 
    { 
 
     $errorMsgReg="Username or Email already exits."; 
 
    } 
 
    
 
\t } 
 
    else 
 
    { 
 
     $errorMsgReg="Enter valid details."; 
 
    } 
 

 

 
} 
 

 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>2-Step Verification using Google Authenticator</title> 
 
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" /> 
 
</head> 
 
<body> 
 
<div id="container"> 
 
    <h1>2-Step Verification using Google Authenticator</h1> 
 
<div id="login"> 
 
<h3>Login</h3> 
 
<form method="post" action="" name="login"> 
 
<label>Username or Email</label> 
 
<input type="text" name="usernameEmail" autocomplete="off" /> 
 
<label>Password</label> 
 
<input type="password" name="password" autocomplete="off"/> 
 
<div class="errorMsg"><?php echo $errorMsgLogin; ?></div> 
 
<input type="submit" class="button" name="loginSubmit" value="Login"> 
 
</form> 
 
</div> 
 

 

 
<div id="signup"> 
 
<h3>Registration</h3> 
 
<form method="post" action="" name="signup"> 
 
<label>Name</label> 
 
<input type="text" name="nameReg" autocomplete="off" /> 
 
<label>Email</label> 
 
<input type="text" name="emailReg" autocomplete="off" /> 
 
<label>Username</label> 
 
<input type="text" name="usernameReg" autocomplete="off" /> 
 

 
<label>Password</label> 
 
<input type="password" name="passwordReg" autocomplete="off"/> 
 
<div class="errorMsg"><?php echo $errorMsgReg; ?></div> 
 
<input type="submit" class="button" name="signupSubmit" value="Signup"> 
 
</form> 
 
</div> 
 

 
</div> 
 

 
</body> 
 
</html>

device_confirmation.phpある

<?php 
 
include('config.php'); 
 

 
if(empty($_SESSION['uid'])) 
 
{ 
 
\t header("Location: index.php"); 
 
} 
 

 
include('class/userClass.php'); 
 
$userClass = new userClass(); 
 
$userDetails=$userClass->userDetails($_SESSION['uid']); 
 
$secret=$userDetails->google_auth_code; 
 
$email=$userDetails->email; 
 

 
require_once 'googleLib/GoogleAuthenticator.php'; 
 

 
$ga = new GoogleAuthenticator(); 
 

 
$qrCodeUrl = $ga->getQRCodeGoogleUrl($email, $secret,'9lessons Demos'); 
 

 

 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>2-Step Verification using Google Authenticator</title> 
 
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" /> 
 
</head> 
 
<body> 
 
\t <div id="container"> 
 
\t \t <h1>2-Step Verification using Google Authenticator</h1> 
 
\t \t <div id='device'> 
 

 
<p>Enter the verification code generated by Google Authenticator app on your phone.</p> 
 
<div id="img"> 
 
<img src='<?php echo $qrCodeUrl; ?>' /> 
 
</div> 
 

 
<form method="post" action="home.php"> 
 
<label>Enter Google Authenticator Code</label> 
 
<input type="text" name="code" /> 
 
<input type="submit" class="button"/> 
 
</form> 
 
</div> 
 
<div style="text-align:center"> 
 
\t <h3>Get Google Authenticator on your phone</h3> 
 
<a href="https://itunes.apple.com/us/app/google-authenticator/id388497605?mt=8" target="_blank"><img class='app' src="images/iphone.png" /></a> 
 

 
<a href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=en" target="_blank"><img class="app" src="images/android.png" /></a> 
 
</div> 
 
</div> 
 
</body> 
 
</html>

config.phpを

<?php 
 
session_start(); 
 
/* DATABASE CONFIGURATION */ 
 
define('DB_SERVER', 'localhost'); 
 
define('DB_USERNAME', 'root'); 
 
define('DB_PASSWORD', ''); 
 
define('DB_DATABASE', 'demos'); 
 
define("BASE_URL", "http://localhost/GoogleAuth/"); // Eg. http://yourwebsite.com 
 

 

 
function getDB() 
 
{ 
 
\t $dbhost=DB_SERVER; 
 
\t $dbuser=DB_USERNAME; 
 
\t $dbpass=DB_PASSWORD; 
 
\t $dbname=DB_DATABASE; 
 
\t try { 
 
\t $dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); \t 
 
\t $dbConnection->exec("set names utf8"); 
 
\t $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
 
\t return $dbConnection; 
 
    } 
 
    catch (PDOException $e) { 
 
    echo 'Connection failed: ' . $e->getMessage(); 
 
\t } 
 

 
} 
 
?>

とhome.php

<?php 
 
include('config.php'); 
 
include('class/userClass.php'); 
 
$userClass = new userClass(); 
 
$userDetails=$userClass->userDetails($_SESSION['uid']); 
 

 
if($_POST['code']) 
 
{ 
 
$code=$_POST['code']; 
 
$secret=$userDetails->google_auth_code; 
 
require_once 'googleLib/GoogleAuthenticator.php'; 
 
$ga = new GoogleAuthenticator(); 
 
$checkResult = $ga->verifyCode($secret, $code, 2); // 2 = 2*30sec clock tolerance 
 

 
if ($checkResult) 
 
{ 
 
$_SESSION['googleCode']=$code; 
 

 

 
} 
 
else 
 
{ 
 
echo 'FAILED'; 
 
} 
 

 
} 
 

 

 
include('session.php'); 
 
$userDetails=$userClass->userDetails($session_uid); 
 

 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>2-Step Verification using Google Authenticator</title> 
 
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" /> 
 
</head> 
 
<body> 
 
\t <div id="container"> 
 
<h1>Welcome <?php echo $userDetails->name; ?></h1> 
 

 
<pre> 
 
<?php print_r($userDetails); ?> 
 
</pre> 
 
<h4><a href="<?php echo BASE_URL; ?>logout.php">Logout</a></h4> 
 
</div> 
 
</body> 
 
</html>

私はあなたがのような関数を使用する場合、この問題が発生

答えて

1

間違っていたところを教えてください10とsetcookieを入力すると、出力がブラウザに送信されます。リクエストの本文の送信を開始した後はHTTPヘッダーを変更できないため、これらの関数の呼び出しが行われる前にコンテンツが送信されていないことを確認する必要があります。

これは、出力コード生成(HTML)からPHPコードを分離する良い理由の1つです。主要なPHPフレームワークのほとんどは、TwigSmartyのような別々のテンプレートエンジンを使用してこれを行います。

ヘッダが設定された後に出力が生成されることを確認するために過度の努力が必要なプロジェクトが不十分な場合は、PHPのOutput Control関数を使用して簡単に回避できます。

関連する問題