2017-01-01 52 views
0

我試圖通過使用htaccess直接訪問我ftp服務器上的rar文件,並將它重定向到可以登錄併成功登錄後訪問文件的頁面。我已經設置像這樣:如何檢查登錄是否成功並且不重定向?

的.htaccess:

RewriteEngine on 
Redirect /Downloads/file1.rar /loginAuth1.php 
Redirect /Downloads/file2.rar /loginAuth2.php 

loginAuth1:

if(isset($_POST['username']) && isset($_POST['password'])){ 

$username = mysqli_real_escape_string($con, $_POST['username']); 
$password = mysqli_real_escape_string($con, md5($_POST['password'])); 

$result = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$username'") or die(mysqli_error($con)); 
if(mysqli_num_rows($result) < 1){ 
    header("Location: loginAuth1.php?error=incorrect-password"); 
} 
while($row = mysqli_fetch_array($result)){ 
    if($password != $row['password']){ 
     header("Location: loginAuth1.php?error=incorrect-password"); 
    }elseif($row['status'] == "0"){ 
     header("Location: loginAuth1.php?error=banned"); 
    }else{ 
     $_SESSION['id'] = $row['id']; 
     $_SESSION['username'] = $username; 
     $_SESSION['email'] = $row['email']; 
     $_SESSION['rank'] = $row['rank']; 
     header("Location: Downloads\file1.rar"); 
    } 
} 
} 

loginAuth2:

if(isset($_POST['username']) && isset($_POST['password'])){ 

$username = mysqli_real_escape_string($con, $_POST['username']); 
$password = mysqli_real_escape_string($con, md5($_POST['password'])); 

$result = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$username'") or die(mysqli_error($con)); 
if(mysqli_num_rows($result) < 1){ 
    header("Location: loginAuth2.php?error=incorrect-password"); 
} 
while($row = mysqli_fetch_array($result)){ 
    if($password != $row['password']){ 
     header("Location: loginAuth2.php?error=incorrect-password"); 
    }elseif($row['status'] == "0"){ 
     header("Location: loginAuth2.php?error=banned"); 
    }else{ 
     $_SESSION['id'] = $row['id']; 
     $_SESSION['username'] = $username; 
     $_SESSION['email'] = $row['email']; 
     $_SESSION['rank'] = $row['rank']; 
     header("Location: Downloads\file2.rar"); 
    } 
} 
} 

什麼將是檢查的最佳途徑用戶成功登錄,並停止重定向,然後用戶可以下載文件?

謝謝。

回答

1

更好的方法是檢查身份驗證,然後返回rar文件內容的八位字節流,並設置成功時的頭部類型,大小和文件名。這樣你就不需要重定向到一個不受保護的文件。請參閱https://stackoverflow.com/a/8485963/7361251

+0

我看到了您提供的答案,該答案會爲用戶提供特定的URL以供下載文件,正確嗎?如何防止用戶在未下載文件的情況下記錄日誌。 – Matt142

+0

沒關係:)爲你的幫助歡呼。 – Matt142