2012-09-11 5 views
0
<?php 

    $userid = $_POST["userid"]; 
    $pword = $_POST["pword"]; 

    # session 

    session_start(); 

    # check that session is valid and set 

    if(!isset($_SESSION['login'])) 
    { 
     header('Location: login.php'); 
    } 

    # check that the required values have been entered 
    $testin1 = ($userid); 
    $testin2 = ($pword); 

    if($testin1 == "") 
    { 
     print "<hr><h1> No Username Entered, Please return to the Login page</h1></hr>"; 
    } 
    elseif ($testin2 == "") 
    { 
     print "<hr><h1> No Password Entered, Please return to the Login page</h1></hr>"; 
    } 

    # Connect to database 

    $connect = mysql_connect ("localhost","root") or die("Error Connecting to SQLServer"); 
    $db = mysql_select_db ("test"); 

    # query 

     $query = mysql_query ("select username from login where username = '$userid' and pword = '$pword';"); 

    if($query === FALSE) 
    { 
     die(mysql_error()); 
    } 

    $result = mysql_fetch_array($query); 
    $record = $result['username'] ; 

    if ($record != null) 
    # check if session is operational, if so redirect the user to the correct page  
    { 
     $_SESSION['login'] = true; 
     header('Location: index.php') ; 
    } 
    else if ($record == null) 
    { 
     header('Location: login.php'); 
    } 
?> 

어디에서 작동하지 않는지 알고 계십니까? 'error free'인 것처럼 보이지만 index.php 페이지와 반대로 login.php 페이지로 다시 리디렉션됩니다. PHP에서 상대적인 초보자로서 도움이 될 것입니다. 당신은 키에 의해 결과를 액세스하기 위해 mysql_fetch_array().를 사용하는PHP가 데이터베이스에 데이터를 등록하고 있지 않습니다.

감사

+0

일부 인쇄 문을 넣었을 때 오류가 발생한 부분을 정확히 찾아 내면 로그인 할 때 무슨 일이 일어나는지 정확히 알 수 있습니다. – ajon

+0

세션 변수가 확실합니까? 설정되어 있습니까? echo $ _SESSION [ 'login '] 그리고 설정되어 있는지 확인하십시오. – Wearybands

+2

mysql_ * 함수를 사용하지 마십시오. 더 이상 사용되지 않습니다. 대신에 mysqli 나 PDO를 사용하라. 그러나, 당신은'if (! isset ($ _ SESSION [ 'login'])) {header ('Location : login.php'); }'index.php의 시작 부분에 표시됩니다. 즉,이 세션 속성이 설정되어 있지 않기 때문에 항상 리디렉션됩니다. – shadyyx

답변

0

header()이 출력되기 전에 사용자에게 데이터를 출력 할 수있는 것처럼 보입니다. <HR> 및 출력 (오류가있는 경우에만 있음을 알고 있습니다.)을 사용하고 있다면 코드 위에 <HTML> 등을 넣을 수 있습니다.

이 경우 다른 헤더는 작동하지 않습니다.

는 (코멘트 너무 오래 같이 답변으로 게시 - 그리고 문제가 될 수

를이 코드를보십시오 :

<?php 

    session_start(); 
    $userid = $_POST["userid"]; 
    $pword = $_POST["pword"]; 

    // check that the required values have been entered 
    $testin1 = ($userid); 
    $testin2 = ($pword); 

    if($testin1 == "") 
    { 
     print "<hr><h1> No Username Entered, Please return to the Login page</h1></hr>"; 
    } 

    if ($testin2 == "") 
    { 
     print "<hr><h1> No Password Entered, Please return to the Login page</h1></hr>"; 
    } 

    // Connect to database 

    $connect = mysql_connect ("localhost","root") or die("Error Connecting to SQLServer"); 
    $db = mysql_select_db ("test"); 

    // query 

     $query = mysql_query ("select username from login where username = '$userid' and pword = '$pword';"); 

    if($query === FALSE) 
    { 
     die(mysql_error()); 
    } 

    $result = mysql_fetch_array($query); 
    $record = $result['username'] ; 

    if ($record != null) 
    // check if session is operational, if so redirect the user to the correct page  
    { 
     $_SESSION['login'] = true; 
     header('Location: index.php') ; 
    } 
    else if ($record == null) 
    { 
     header('Location: login.php'); 
    } 
?> 

내가 통해 모습을하고 논리를 수정뿐만 아니라 주위에 몇 가지를 이동했다.

+0

@ user1662306 내가 추가 한 코드를 사용해 볼 수 있습니까? – Fluffeh

+0

코드가 여전히 나를 login.php 페이지로 리디렉션했습니다. – user1662306

2

당신은 mysql_fetch_assoc().

$result = mysql_fetch_assoc($query); 
    $record = $result['username'] ; 

사용 (이는 mysql_real_escape_string와 사용자 입력을 탈출하는 것이 가장 있다는 사실을 숙지 할 필요가).

+0

fetch_array의 기본값은 실제로 숫자와 assoc 배열 둘 다입니다 :'array mysql_fetch_array (resource $ result [, int $ result_type = MYSQL_BOTH])' – Fluffeh

0

! isset()이 login.php 페이지로 리디렉션되고 있는지 확인하기 때문에 세션에 문제가있는 것 같습니다. $ _SESSION [ 'login']이 (가) 설정되었는지 여부를 확인하십시오. 코드를 디버깅하여 세션 변수를 확인하십시오.

관련 문제