2014-09-02 4 views
0

나는 사용자가 자신의 계정에 성공적으로 로그인 한 후 profile.php 페이지로 리디렉션되는 로그인 페이지가 있습니다. 그러나 값을 입력하지 않고 로그인 버튼을 클릭하거나 잘못된 값을 입력하는 경우 index.html 페이지로 리디렉션하고 싶습니다. 나는 header("location: index.html");을 사용하여 시도했지만 효과가 없습니다. 누구나 할 수있는 방법을 말해주십시오.PHP에서 한 페이지에서 다른 페이지로 리디렉션

로그인 양식 페이지는 다음과 같습니다 당신의 분야 모두가 어디 그렇게 후도 헤더를 추가하려고 어떤 리디렉션되지 않은 비어있는 경우

<form role="form" action="login.php" method="post"> 
    <div class="form-group"> 
     <label for="exampleInputEmail1">Email</label> 
     <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="email"> 
    </div> 

    <div class="form-group"> 
     <label for="exampleInputPassword1">Password</label> 
     <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password"> 
    </div> 
    <input name="submit" type="submit" value=" Login ">    
</form> 

Login.php 페이지

<?php 
    session_start(); // Starting Session 
    $error=''; // Variable To Store Error Message 
    if (isset($_POST['submit'])) { 
     if (empty($_POST['email']) || empty($_POST['password'])) { 
      $error = "email or Password is invalid"; 
     } 
     else 
     { 
      // Define $email and $password 
      $email=$_POST['email']; 
      $password=$_POST['password']; 
      // To protect MySQL injection for Security purpose 
      $email = stripslashes($email); 
      $password = stripslashes($password); 
      $email = mysql_real_escape_string($email); 
      $password = mysql_real_escape_string($password); 

      //Establishing Connection with Server by passing server_name, user_id and password as a parameter 
      $connection = mysql_connect("abc.com", "abc", "abc"); 
      // Selecting Database 
      $db = mysql_select_db("abc", $connection); 
      // SQL query to fetch information of registerd users and finds user match. 
      $query = mysql_query("select * from users where password='$password' AND email='$email'", $connection); 
      $rows = mysql_num_rows($query); 

      if ($rows == 1) { 
       $_SESSION['login_user']=$email; // Initializing Session 
       header("location: profile.php"); // Redirecting To Other Page 
      } else { 
       $error = "email or Password is invalid"; 
       header("location: index.html"); 
      } 

      mysql_close($connection); // Closing Connection 
     } 
    } 
?> 

답변

1
<?php 
session_start(); // Starting Session 

if (isset($_POST['submit'])) { 
    try { 
     if (empty($_POST['email']) || empty($_POST['password'])) { 
      throw new Exception("email or Password is invalid"); 
     } else { 
      // Define $email and $password 
      $email  = $_POST['email']; 
      $password = $_POST['password']; 
      // To protect MySQL injection for Security purpose 
      $email  = stripslashes($email); 
      $password = stripslashes($password); 
      $email  = mysql_real_escape_string($email); 
      $password = mysql_real_escape_string($password); 
      //Establishing Connection with Server by passing server_name, user_id and password as a parameter 
      $connection = mysql_connect("abc.com", "abc", "abc"); 
      // Selecting Database 
      $db   = mysql_select_db("abc", $connection); 
      // SQL query to fetch information of registerd users and finds user match. 
      $query  = mysql_query("select * from users where password='$password' AND email='$email'", $connection); 
      $rows  = mysql_num_rows($query); 

      if ($rows != 1) 
       throw new Exception("email or Password is invalid"); 

      $_SESSION['login_user'] = $email; // Initializing Session 
      header("location: profile.php"); // Redirecting To Other Page 
      mysql_close($connection); // Closing Connection 
     } 
    } 
    catch (Exception $e) { 
     $_SESSION['login_error'] = $e->getMessage(); 
     header("Location: index.html"); 
    } 
} 
?> 

당신은 $ _SESSION [ 'login_error']

+0

감사 완벽하게 일했습니다 –

0

입니다 $error

if (empty($_POST['email']) || empty($_POST['password'])) { 
    header("location: index.html"); 
} 

또한 HTML 페이지를 리디렉션하면 $ error를 사용하지 않아도 PHP를 얻을 수 없습니다. HTML 페이지의 변수.

+0

을 제대로 작동 코드를 사용하고 그것을 조금 수정 시도 throught를 당신의 login_error를받을 수 있습니다 그것은 작동하지 않고 profile.php의 경로에도 영향을 미침 –

+0

영향을받지 않는 방법 –

0
// Change you conditional statement like ... 
if (empty($_POST['email']) || empty($_POST['password'])) { 
    header("location: index.html"); 
    exit(0); 
} 
+0

profile.php에 대한 내 경로에 영향을주지 않습니까? 나는 그것을 사용했지만 그것도 올바른 페이지로 리디렉션하거나 로그인 할 수 없었어요 –

0

내가 나를 위해

session_start(); // Starting Session 
$error=''; // Variable To Store Error Message 
if (isset($_POST['submit'])) { 
if (empty($_POST['email']) || empty($_POST['password'])) { 
    header("location: index.html"); 
    exit(0); 
} // ..... and so on 
관련 문제