2014-09-02 158 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"); 
    } 
} 
?> 

您可以收到您的login_error throught $ _SESSION [ 'login_error']

+0

謝謝你完美工作 – 2014-09-02 05:00:23

0

如果你的兩個領域是空白的,你不重定向任何地方所以嘗試後也添加頁眉$error

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

此外,如果你重定向的HTML頁面,不需要使用$錯誤,因爲你不能得到PHP在html頁面上變量。

+0

它是不工作,也影響路徑到profile.php – 2014-09-02 04:47:57

+0

沒有得到它會如何影響 – 2014-09-02 04:50:20

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

它不會影響我的路徑profile.php。我試過使用它,但它不允許我登錄或重定向到正確的頁面 – 2014-09-02 04:47:12

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 
相關問題