首先,我想開始說我對PHP沒有任何瞭解,所以我希望能夠得到所有幫助。 所以我有一個網站託管在godaddy上傳我的客戶的文件。在朋友的幫助下,我製作了一個帶有用戶名和密碼的簡單登錄系統。問題是,儘管沒有輸入用戶名和密碼就無法訪問網站,但可以通過在瀏覽器中直接輸入完整鏈接來訪問.jpg等文件。我希望它是通過用戶網頁訪問文件的唯一方式。另外我希望每個用戶只能訪問他們自己的文件,而不能訪問其他文件。所以這裏是我的代碼,如果有任何額外的變化需要避免黑客入侵,我將非常感謝輸入。正被用於輸入用戶名和密碼的形式拒絕http訪問目錄,但允許內部服務器訪問
index.php文件代碼:
<form name="form1" method="post" action="checklogin.php">
<div class="lefts">
<p>Login:</p>
<p>Password:</p>
</div>
<div>
<input name="myusername" type="text" id="myusername" />
<input name="mypassword" type="password" id="mypassword" />
</div>
<div><input type="image" name="Submit" id="submit" value="Login" src="images/submitOff.png" /></div>
</form>
checklogin.php:(如果輸入正確的用戶名和密碼,它進入用戶名,如果網頁不是去了錯誤的用戶名或密碼的網頁
<?php
ob_start();
session_start();
$host="hostname"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbnamey"; // Database name
$tbl_name="tablename"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT username FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
//returns false if no results returned
$row = mysql_fetch_row($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($row){
// Register $myusername, $mypassword and redirect to file
$_SESSION["myusername"] = $myusername;
$_SESSION["mypassword"] = $mypassword;
$myPage = $myusername.".php";
$_SESSION["myPage"] = $myPage;
header("location:".$myPage);
}
else {
header("location:index2.php");
}
ob_end_flush();
?>
username1.php:(webapge用戶包含文件)
<?
session_start();
if(
//!session_is_registered(myusername)
!isset($_SESSION["myusername"]) ||
$_SESSION["myPage"] != basename($_SERVER['REQUEST_URI'])
){
header("location:index.php");
}
?>
<html>
//content that consist of links to the files
<a href="ready/username1/file.png">Png 1</a>
</html>
感謝您的答覆。我對PHP一無所知,所以這就是爲什麼它如此糟糕。你認爲你可以重寫它,使它成爲如何? – 2011-02-02 02:04:37