2017-02-12 109 views
-2

我想在登錄後重定向到同一頁面,但我需要像if username and password come from index.php這樣的頁面將重定向到dashboard.php,else it will redirect on the same page (exmple.php)如何在登錄後用用戶名重定向到同一頁面PHP

的login.php:

<?php 
include ('include/connection.php'); 
if (isset($_POST['loginform'])) { 
    session_start(); 
    $email = trim(mysql_escape_string($_POST['email'])); 
    $passwords = trim(mysql_escape_string($_POST['pwd'])); 
    $password = md5($passwords); 
    $verify_query = "SELECT * FROM end_user WHERE (email='$email' AND password='$password')"; 
    verify_result = mysqli_query($con, $verify_query); 
    if(!$verify_result){ 
     echo '<h2>Couldnot Process your request Please try to login after some time. </h2>'; 
    } 
    if (@mysqli_num_rows($verify_result) == 1) { 
     $_SESSION = mysqli_fetch_array($verify_result, MYSQLI_ASSOC); 
     header("Location: dashboard.php"); 
    } 
    else { 
     echo '<h2 style="color:#CC3300;">Incorrect Credentials, You need to register <a href="signup.php" style="font-size:20px;" class="btn btn-primary">Here</a></h2>'; 
    } 
mysqli_close($con); 
} 
?> 

的index.php:

session_start(); // starts the session 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 

而且我用同樣的代碼在example.php,這樣我就可以在$_SESSION得到URL。

+0

您可以使用頭,如果沒有HTML已發送到客戶端 – NoLiver92

+0

你的mysql混合的API也是 –

+0

並且不要**在實時環境中使用此代碼;這完全不安全。 –

回答

0

從你的index.php登錄表單傳遞一個隱藏字段一樣

<input type="hidden" name="extrafield" value="fromindex"> 

然後

<?php 
include ('include/connection.php'); 
if (isset($_POST['loginform'])) { 
session_start(); 
$email = trim(mysql_escape_string($_POST['email'])); 
$passwords = trim(mysql_escape_string($_POST['pwd'])); 
$password = md5($passwords); 
     $verify_query = "SELECT * FROM end_user WHERE (email='$email' AND password='$password')"; 
      $verify_result = mysqli_query($con, $verify_query); 
     if(!$verify_result){ 
     ?>     
            <?php 
           echo '<h2>Couldnot Process your request Please try to login after some time. </h2>'; 
            } 

             if (@mysqli_num_rows($verify_result) == 1) 
            { 
             $_SESSION = mysqli_fetch_array($verify_result, MYSQLI_ASSOC); 
     if(isset($_POST['extrafield']) == 'fromindex'){ 
            header("Location: dashboard.php"); 
     } else { 
       header("Location: exmple.php"); 
     } 
            }else 
            { 
             ?> 

            <?php echo '<h2 style="color:#CC3300;">Incorrect Credentials, You need to register <a href="signup.php" style="font-size:20px;" class="btn btn-primary">Here</a>         </h2>'; 
              } 


           mysqli_close($con); 

            } 
               ?> 
+0

thnx fr ur hlp。在隱藏的領域,我高壓傳遞給login.php? – nitul

+0

並且我還將這個值直接傳遞給它,它的nt wrkng,它唯一的重定向到dashboard.php,即使我在login.php頁面中手動將值「fromindex」改爲「fromindex11」。 bt其唯一重定向到dashboard.php – nitul

+0

使用$ _SESSION = mysqli_fetch_array($ verify_result,MYSQLI_ASSOC)後使用的條件;如果用戶從索引頁面用戶登錄將被重定向到dashboard.php,並且如果用戶從其他頁面登錄,他們將被重定向到exemple.php頁面。 –

相關問題