2012-10-24 102 views
0

從本網站獲得幫助後,我現在已經獲得了我的日誌腳本,但很有用,但是當我嘗試檢查受限制頁面上的會話時,出現以下問題。與登錄腳本中的會話相關的問題

日誌腳本本身

<?php 

function validateUser() 
{ 

    session_regenerate_id(); //this is a security measure 
    $_SESSION['valid'] = 1; 
    $_SESSION['userid'] = $userid; 
} 


?> 

<?php 

ob_start(); // Start output buffering 

error_reporting(E_ALL & ~E_NOTICE); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 

session_start(); //must call session_start before using any $_SESSION variables 
$username = $_POST['username']; 
$password = $_POST['password']; 
//connect to the database here 

$hostname_PropSuite = "localhost"; 
$database_PropSuite = "propsuite"; 
$username_PropSuite = "root"; 
$password_PropSuite = "root"; 
$PropSuite = mysql_pconnect($hostname_PropSuite, $username_PropSuite, $password_PropSuite) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_PropSuite, $PropSuite); 

$username = mysql_real_escape_string($username); 

$query = "SELECT password, salt FROM admin_users WHERE username = '$username';"; 

$result = mysql_query($query) or die(mysql_error()); 

if(mysql_num_rows($result) < 1) //no such user exists 
{ 
    header('Location: http://localhost/PropSuite/index.php?login=fail'); 

    die(); 
} 
$userData = mysql_fetch_array($result, MYSQL_ASSOC); 
$hash = hash('sha256', $userData['salt'] . hash('sha256', $password)); 
if($hash != $userData['password']) //incorrect password 
{ 
    header('Location: http://localhost/PropSuite/index.php?login=fail'); 

    die(); 
} 
else 
{ 
    validateUser(); //sets the session data for this user 
} 
//redirect to another page or display "login success" message 
header('Location: http://localhost/PropSuite/main'); 
die() 




//redirect to another page or display "login success" message 


?> 

我想限制的頁面。

<?php 

error_reporting(E_ALL & ~E_NOTICE); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 



function isLoggedIn() 
{ 
    if(isset($_SESSION['valid']) && $_SESSION['valid']) 
     return true; 
    return false; 
} 

//if the user has not logged in 
if(!isLoggedIn()) 
{ 
    header('Location: http://localhost/PropSuite/index.php'); 
    die(); 
} 
//page content follows 
?> 

當我按下登錄會發生什麼事是,它帶我回到登錄頁面看起來是這樣的經歷在日誌中的腳本,然後當它擊中那個扔我返回到登錄主頁在頁面中。我confused.com :-(

預先感謝您的幫助

回答

1

在頁面你試圖限制我看不出有任何的session_start

<?php 
    session_start(); 
    error_reporting(E_ALL & ~E_NOTICE); 
    ini_set('display_errors', TRUE); 
    ini_set('display_startup_errors', TRUE); 
    ..... 
?> 

順便說在你的其他腳本,你應該寫這個

<?php 
    ..... 
    $username = isset($_POST['username'])?$_POST['username']:''; 
    $password = isset($_POST['password'])?$_POST['password']:''; 
    ... 
    ?>