2012-04-12 12 views
0

私はphpタグの前に空白のような通常のものをチェックし、session_start()を持っていますが、私の人生のためにこれを実行することはできません。私はクライアントのシステムにログインしているので、これは非常に重要です。セッション変数が機能しないのはなぜですか?

本質的に、一度私は2番目のページに行きます$ _SESSION ['username'];空です。 私はそれを印刷して空にしましたが、atmはヘッダーのリダイレクトをアクティブにして、コード内に表示します。任意の助けを事前に

おかげで受け取った:)

関連するコード:?

<?php 
session_start(); 

include '../resources/methods/Library.php'; 

if(isset($_SESSION['username'])) 
{ 
    //User already logged in! 
    header('Location: Index.php'); 
} 

//Username and password submitted by user 
$usernameSubmitted = $_POST['username']; 
$passwordSubmitted = $_POST['password']; 

if($usernameSubmitted != "" && $passwordSubmitted != "") 
{ 
    //User has entered both a username and a password. We shall validate them 

    //Connect to database and select all the admin accounts 
    connectToDB(); 
    $query = "SELECT * FROM admins" or die(mysql_error()); 
    $data = mysql_query($query) or die(mysql_error()); 
    $numberOfAdmins = mysql_num_rows($data) or die(mysql_error()); 


    //Check if the username corresponds to any found in the database       
    $usernameValid = false; 

    for($i = 0; $i < $numberOfAdmins; $i++) 
    { 
     if($usernameSubmitted == mysql_result($data, $i, "Username")) 
     { 
      $userToLogInAs = $i; 
      $usernameValid = true; 
     } 
    } 

    //If username is valid, check password 
    if($usernameValid != false) 
    { 
     //Passwords are held as blowfish encryptions for security. Encypt this so we can compare 
     $encryptedPasswordSubmitted = crypt($passwordSubmitted, '$2a$07$buzzybees5hivestottenhoe$'); 

     if($encryptedPasswordSubmitted == mysql_result($data, $userToLogInAs, "Password")) 
     { 
      //Create a session variable so the user remains logged in 
      $_SESSION['username'] = $usernameSubmitted; 

      //User entered the correct username and password, redirect them to the website. 
      header('Location: Index.php'); 
     } 
    } 

    //If we've got this far then the user didn't authenticate successfully. 
    $message = "<h2>Sorry, Invalid Credentials</h2><p>Check that you're tying your username and password correctly.</p>"; 
} 

>

次のページ:

<?php 
session_start(); 

if(!isset($_SESSION['username'])) 
{ 
    //User not signed in, send them to the log in page 
    header('Location: Log-In.php'); 
} 
?> 

任意のアイデア?

おかげで、 ダニー

+1

これは機能していないとはどういう意味ですか? – Michelle

+0

なぜ実際に動作していないのか教えてください。エラーメッセージ?予期せぬことは何が起きているのですか? –

+0

そのようなアマチュアの間違いは、なぜそれが壊れているのかを含めません。 – DanTonyBrown

答えて

1

これは単なる推測ですが、あなただけのログインスクリプトのコードの一部を示していると仮定:

あなたは成功したログインのリダイレクト後die()を使用していませんそれ以降に来て、あなたがここに示していないコードは実行されます。そこに$_SESSION変数を操作していると、問題が発生する可能性があります。

だけにあなたのコードを変更するには、安全のために:

if($encryptedPasswordSubmitted == mysql_result($data, $userToLogInAs, "Password")) 
    { 
     //Create a session variable so the user remains logged in 
     $_SESSION['username'] = $usernameSubmitted; 

     //User entered the correct username and password, redirect them to the website. 
     header('Location: Index.php'); 
     die(); // this is important! 
    } 

、それが問題を解決するかどうかを確認。リダイレクトするたびにそれを行う必要があることに注意してください。

+0

まだ動作しません: – DanTonyBrown

関連する問題