2016-10-27 5 views
-5

データベースに接続するPHPコードがあります。 PHPは、ログインしたユーザーのデータベースから変数TEAMを取得することを目的としており、チームがハードコードされた値と一致した場合、ユーザーはページを続行します。そうでない場合、ユーザーは別のページにリダイレクトされます。しかし、私はそれをテストしたとき、ページはリダイレクトされませんでした。なぜこれが起こっているのですか?どうすれば修正できますか? コード:データベースから変数を取り出すときにPHPがリダイレクトされないのはなぜですか?

// First we execute our common code to connection to the database 
// and start the session 
require("../common.php"); 

// Construct the query with :placeholders (instead of using variables 
// to construct a query, which isn't secure) 
$PDOSelectTeam = $db->prepare('SELECT team FROM `users` WHERE `username` LIKE :userNameToLookUp'); 
// Bind a variable to the placeholder(s). You can have as many of 
// these bindParam calls as you need, if you have more placeholders 
// in your SQL query 
$PDOSelectTeam->bindParam(':userNameToLookUp', $username); 

// ALTERNATIVELY: 
//  $PDOSelectTeam->bindValue(':userNameToLookUp', 'jakebathman'); 
// If you need to bind a VALUE to a placeholder (instead of a 
// variable), you must use the bindValue() method. Multiple 
// bindParam() and bindValue() methods may be used as needed. 

// Execute the query on the database (this doesn't return anything) 
$PDOSelectTeam->execute(); 

} 

if(empty($_SESSION['user'])) { 
    // If they are not, we redirect them to the login page. 
    header("Location: ../login.php"); 

    // Remember that this die statement is absolutely critical. Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to login.php"); 
} 
if((strcmp($db, "8514")) !== 0 || (strcmp($db, "ALL") !== 0)) { 
    // If they are not, we redirect them to the login page. 
    header("Location: ../index.php"); 

    // Remember that this die statement is absolutely critical. Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to index.php"); 
} 
// Everything below this point in the file is secured by the login system 

// We can display the user's username to them by reading it from the session array. Remember that because 
// a username is user submitted content we must use htmlentities on it before display 

MySQL variables

+0

は、あなたが '試してみました....これを試してみてください空 '? – UltrasoundJelly

+2

おそらくあなたの構文エラーと関係があります。あなたは余分な括弧 '}'を持っています)。いずれの場合でも、これらの変数に何が含まれているかを知らなければ、デバッグを本当に助けることはできません。また、技術的には 'Location'ヘッダに相対パスを使うことはできませんが、ほとんどのブラウザで動作します。 – Brad

+0

@UltrasoundJelly OPは 'empty'を正しく使っています – Phil

答えて

0

`代わりに使用しての`()ISSET($ _ SESSION [ 'ユーザー'])場合

<?php 


// First we execute our common code to connection to the database 
// and start the session 
require("../common.php"); 

// Construct the query with :placeholders (instead of using variables 
// to construct a query, which isn't secure) 
$PDOSelectTeam = $db->prepare('SELECT team FROM `users` WHERE `username` LIKE :userNameToLookUp'); 
// Bind a variable to the placeholder(s). You can have as many of 
// these bindParam calls as you need, if you have more placeholders 
// in your SQL query 
$PDOSelectTeam->bindParam(':userNameToLookUp', $username); 

// ALTERNATIVELY: 
//  $PDOSelectTeam->bindValue(':userNameToLookUp', 'jakebathman'); 
// If you need to bind a VALUE to a placeholder (instead of a 
// variable), you must use the bindValue() method. Multiple 
// bindParam() and bindValue() methods may be used as needed. 

// Execute the query on the database (this doesn't return anything) 
$PDOSelectTeam->execute(); 



} 


if($PDOSelectTeam->rowCount()) { 
    // If they are not, we redirect them to the login page. 
    header("Location: ../index.php"); 

    // Remember that this die statement is absolutely critical. Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to index.php"); 
} 
else 
{ 

    header("Location: ../login.php"); 

    // Remember that this die statement is absolutely critical. Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to login.php"); 

} 
// Everything below this point in the file is secured by the login system 

// We can display the user's username to them by reading it from the session array. Remember that because 
// a username is user submitted content we must use htmlentities on 

############################################ 
########### WHERE IS SESSION SET VAR ?? #### 
############################################ 
if(empty($_SESSION['user'])) { 
    // If they are not, we redirect them to the login page. 
    header("Location: ../login.php"); 

    // Remember that this die statement is absolutely critical. Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to login.php"); 
} 
########################################## 
########################################## 
+0

これは何を問わずログインページに戻るはずですか?それが結果です。 – Caleb

関連する問題