2017-04-18 1 views
2

admin 및 user에 대한 라디오 버튼이있는 HTML 로그인 페이지가 있는데, 사용자가 "admin"을 선택하면 관리자 로그인 페이지로 리디렉션되어야하며 "user" 다음은 사용자 로그인 페이지로 이동합니다PHP 로의 HTML 리디렉션

html로 로그인 페이지는 다음과 같이 주어진다 :.

<!-- THE LOG IN PAGE --> 
<div id="id01" class="modal"> 
    <form name="login" id="login" class="modal-content animate" action="login.php" method="post"> 
     <div class="imgcontainer"> 
      <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">&times;</span> 
      <img src="images/avatar.png" alt="Avatar" class="avatar"> 
     </div> 
     <div class="container"> 
      <label><b>Email Id:</b></label> 
      <input type="text" placeholder="Enter Email Id" name="email" id="email" required> 
      <label><b>Password:</b></label> 
      <input type="password" placeholder="Enter Password" name="psw" id="psw" required> 
      <label><b>User Type:</b></label> 
      <table width="100%"> 
      <tr> 
      <td><input type="radio" name="usertype" id="usertype" value="admin" required>ADMIN</td> 
      <td><input type="radio" name="usertype" id="usertype" value="user" required>USER</td> 
      </tr> 
      </table>    
      <button type="submit">Login</button> 
      <input type="checkbox" checked="checked"> Remember me 
      <span class="psw"><a href="#">Forgot password?</a></span> 
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button> 
     </div> 
    </form> 
    <script> 
     // Get the modal 
     var modal = document.getElementById('id01'); 
     // When the user clicks anywhere outside of the modal, close it 
     window.onclick = function(event) 
     { 
      if (event.target == modal) 
      { 
       modal.style.display = "none"; 
      } 
     } 
    </script> 
</div> 

사용자에 대한 admin_login.php 페이지가 아래와 같습니다 :

<?php 

으로 session_start()를 ;

$ email = $ _ POST [ "email"];

$ psw = $ _ POST [ "psw"];

$ name = "";

에는 'db_config.php'가 포함됩니다.

$ sql = "이름, 비밀번호, 이메일 FROM admin_details from email = '$ email'";

$ result = $ conn-> query ($ sql);

다른

{

echo '<script type="text/javascript">'; 

echo 'alert("You must REGISTER first and then LOGIN !!!");'; 

echo 'window.location.href = "admin_index.php";'; 

echo '</script>'; 

}

,617 ($에서 그 결과> NUM_ROWS> 0)

{

// output data of each row 

while($row = $result->fetch_assoc()) 

{ 

    $name=$row['name']; 

    if($psw == $row['password']) 

    { 

     $_SESSION['name']=$name;  

     $_SESSION['email']=$email; 

     header('Location: admin_index.php'); 

    } 

    else 

    { 

     echo '<script type="text/javascript">'; 

     echo 'alert("WRONG PASSWORD ENTRY. TRY LOGGING IN AGAIN !!!");'; 

     echo 'window.location.href = "admin_index.php";'; 

     echo '</script>';   

    } 

} 
}

경우

$ conn-> close();

>

+0

여기서 리디렉션에 사용되는 PHP 스크립트는 무엇입니까? –

+0

Javascript 또는 PHP를 사용하는 두 가지 방법이 있습니다 (요소 값을 가져 와서 비교하고 리디렉션 함) –

+0

사람, 눈에 괴롭다 ... 온라인 형식 지정 기나 뭔가를 통해 던져보십시오. 이건 꽤 괜찮은 것 같아요. http://www.freeformatter.com/html-formatter.html –

답변

0

먼저 추가하고 핸들러 라디오 버튼에 :

<script type="text/javascript"> 
    function handleUserTypeChanges(radioButton){ 
     var form = document.getElementById("login"); 
     var userType = radioButton.value; 

     if (userType == 'admin') { 
      form.action = 'your/path/to/admin/page/admin.php'; 
     }else{ 
      form.action = 'your/path/to/user/page/user.php'; 
     } 
    } 
</script> 
+0

이유는 모르지만 코드가 제 경우에는 작동하지 않습니다 – Neha

+0

시도해 보았습니다. https://jsfiddle.net/L5L6hjag/ –

+0

내 질문을 편집하고 admin_login을 삽입했습니다. .php admin_login.php 페이지에 문제가 있습니까? – Neha

0
: 그럼 당신은 같은 일부 JS를 사용할 수

<td><input type="radio" name="usertype" id="usertype" value="admin" required>ADMIN</td> 
<td><input type="radio" name="usertype" id="usertype" value="user" required>USER</td> 

<td><input type="radio" name="usertype" id="usertype" onclick="handleUserTypeChanges(this);" value="admin" required>ADMIN</td> 
<td><input type="radio" name="usertype" id="usertype" onclick="handleUserTypeChanges(this);" value="user" required>USER</td> 

그런 식으로 사용할 수 있습니다. 사용자의 유형과 redirect accodingly

if(isset($_POST['login'])){ 
    // your code to check email id and password 
    if($_POST['usertype'] == 'admin'){ 
     header('Location: admin.php');   
    }elseif($_POST['usertype'] == 'user'){ 
     header('Location: user.php');  
    }else{ 
     $error = "Invalid Login";   
    }  
} 
0

사용 login.php 파일에이 코드를 검사가 도움이되기를 바랍니다.

if(isset($_POST['email'])) 
{ 

if($_POST['usertype']=='admin') header('location: admin.php'); 
else if($_POST['usertype']=='user') header('location: user.php'); 
exit(); 

} 
0

나는 while 루프에서 몇 가지 변경을했습니다. 희망 그래서 그것은 당신의 문제를 해결할 것입니다.

admin 또는 user 인 사용자의 역할을 선택할 수 있습니다. 사용자 역할에 따라 사용자를 관리 페이지 또는 로그인 페이지로 이동할 수 있습니다. 여기 데이터베이스에 다른 속성을 추가하고 사용자가 관리 라디오 버튼을 선택한 다음 역할 속성에 1을 설정하고 사용자가 사용자 라디오 버튼을 선택하면 역할 속성에 2를 설정하면 역할을 삽입합니다.

while($row = $result->fetch_assoc()) 

{ 

    $name=$row['name']; 
$psw = $row['password']; 
$role =$row['role']; 
    if($psw == $row['password']) 

    { 

     $_SESSION['name']=$name;  

     $_SESSION['email']=$email; 
if($role == 1){ 
     header('Location: admin_index.php'); 
} 
if($role == 2){ 
     header('Location: admin_index.php'); 
} 

    } 

    else 

    { 

     echo '<script type="text/javascript">'; 

     echo 'alert("WRONG PASSWORD ENTRY. TRY LOGGING IN AGAIN !!!");'; 

     echo 'window.location.href = "admin_index.php";'; 

     echo '</script>';   

    } 

}