2011-02-23 3 views
0

다음 코드가 있습니다. 이제 로그인 버튼을 누르면 아무 일도 일어나지 않고 사용자 이름과 비밀번호가 지워집니다.PHP 로그인 문제

<?php 
    session_start(); 
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="csduc"; // Database name 
    $tbl_name="students"; // Table name 

    // Connect to server and select databse. 
    $connect=mysql_connect("$host", "$username", "$password") or die("cannot connect"); 
    mysql_select_db("$db_name") or die("cannot select DB"); 

    // username and password sent from form 
    $myusername=$_POST['username']; 
    $mypassword=$_POST['password']; 

    // To protect MySQL injection (more detail about MySQL injection). 

    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 
    $result=mysql_query($sql,$connect); 
    $row=mysql_fetch_array($result); 

    // Mysql_num_row is counting table row. 
    //$count=mysql_num_rows($result); 
    // If result matched $myusername and $mypassword, table row must be 1 row. 

    if($row) 
    { 
     // Register $myusername, $mypassword and redirect to file "login_success.php". 
     session_register("myusername"); 
     session_register("mypassword"); 
     header("location: main.php"); 
    } 
    else 
    { 
     echo "Wrong Username or Password"; 
    } 
?> 

어떻게 해결할 수 있습니까?

+2

양식 코드는 어디에 있습니까? –

+0

$ sql을 인쇄하여 거기에 저장된 것을 확인하십시오. 아무것도 반환하지 않습니까? 모양을 가지고 귀하의 HTML을 (그냥 양식 내 로그인 비트) 올려 – JohnP

+1

session_register는 http://php.net/manual/en/function.session-register.php –

답변

1

헤더의 올바른 이름은 "위치"(대문자 'L')입니다. 이것은 중요 할 수도 있고 중요하지 않을 수도 있습니다. 또한 기술적으로 Location 헤더에는 절대 URL이 필요합니다 (예 : "http://example.com/main.php") - 일부 브라우저는 상대 URL을 허용하지만 spec에는 절대 URL이 필요합니다. 다시 말하지만, 이것이 문제의 원인이 될 수도 있고 아닐 수도 있습니다.

그래서 더 "기술적으로 정확"당신의 리디렉션이 이런 식으로 변경 될 수 있습니다 될 수 있습니다 :

$host = $_SERVER['HTTP_HOST']; 
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/'); 
header("Location: http://$host$uri/main.php"); 

체크 아웃 자세한 내용은 php documentation page for the header() function. 때문에 암호로 시험 사용

echo $myusername=$_POST['username']; 
echo $mypassword=$_POST['password']; 
exit; 

및 사용 SQL에 대한

0

는 양식 태그에 문제가

$sql="SELECT * FROM $tbl_name WHERE username='$myusername'"; 
$result=mysql_query($sql,$connect); 
$row=mysql_fetch_array($result); 
1

인코딩 할 수있다.

그것은 다음과 같이 할 필요가

:

<form method="POST" action=""> 

가정 조치가 위의 코드와 동일한 페이지입니다. 그렇지 않으면 코드가있는 페이지로 작업을 지정하십시오. 코드가 페이지 맨 위에 있는지 확인하십시오. 그렇지 않으면 세션 시작과 헤더가 작동하지 않습니다.