2012-10-25 2 views
1

로그인 스크립트에서 [Welcome] [Username] 마사지를 표시하기 위해 세션 변수를 전달하려고합니다. 나는 PHP에 아주 익숙해 져서 모든 의견을 크게 환영합니다.세션 변수가 로그인 스크립트에서 전달되지 않음

코드는 다음과 같습니다.

<?php 
ob_start(); // Start output buffering 

session_start(); //must call session_start before using any $_SESSION variables 
$_SESSION['username'] = $username; 

function validateUser() 
{ 

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

     $username = isset($_POST['username'])?$_POST['username']:''; 
    $password = isset($_POST['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 


?> 
+0

"환영합니다"메시지 암호. – Neal

+0

그것의 리디렉션 페이지에 – AppleTattooGuy

+1

도 코드를 붙여 넣습니다 .. – Nelson

답변

3

기능

function validateUser($username) 
{ 
    session_regenerate_id(); //this is a security measure 
    $_SESSION['valid'] = 1; 
    $_SESSION['username'] = $username; 
} 

에서 매개 변수로 사용자 이름을 추가하고 함수 내에서 전역 범위에서 선언 된 변수를 액세스하려면

validateUser($username); //sets the session data for this user 
+0

꿈과 같이 작동합니다 - 정말 고마워요 :-) – AppleTattooGuy

3

나는 $ username이 정의되기 전에 $_SESSION['username'] = $username;을 사용하고 있다는 것이 문제라고 생각합니다. $username = isset($_POST['username'])?$_POST['username']:'';$_SESSION['username'] = $username; 이상이어야합니다.

1

, 당신이해야 함을 호출 할 때 그것을 통과 예 :

function validateUser() 
{ 
    global $username;//NEEDED to access $username declared in global scope 
    session_regenerate_id(); //this is a security measure 
    $_SESSION['valid'] = 1; 
    $_SESSION['username'] = $username; 
} 
+0

사실 좋지만 더 나은 옵션은 매개 변수로 전달하는 것입니다 [air4x는 대답에서 제안합니다] (http://stackoverflow.com/a/13073580/215966) – prodigitalson

+1

@prodigitalson 네, 그게 더 좋고 권장되는 방법입니다. 방금 리팩터링을 고려하지 않고 OP의 현재 코드를 수정하는 직접 경로를 가졌습니다. air4x는보다 완벽한 대답입니다. – Nelson

+0

충분히 공정 :-) – prodigitalson

관련 문제