使用MAMP的.php文件安全性
我已经生成了一个php文件,其中包含存储在数据库中的信息。要访问此页面,用户必须使用登录页 但是,当您使用MAMP时,如何通过写入IP地址和php文件名(例如123.456.78.00:80/fileone.php)来阻止某人访问该文件。我希望这个fileone.php是隐藏的,他们只能通过登录页面访问它。 提前谢谢使用MAMP的.php文件安全性,php,mamp,Php,Mamp,我已经生成了一个php文件,其中包含存储在数据库中的信息。要访问此页面,用户必须使用登录页 但是,当您使用MAMP时,如何通过写入IP地址和php文件名(例如123.456.78.00:80/fileone.php)来阻止某人访问该文件。我希望这个fileone.php是隐藏的,他们只能通过登录页面访问它。 提前谢谢 <?php session_start(); $username = mysql_real_escape_string($_POST['username']); $pass
<?php
session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
mysql_connect("localhost", "root","root") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
$exists = mysql_num_rows($query); //Checks if username exists
$table_users = "";
$table_password = "";
if($exists > 0) //IF there are no returning rows or no existing username
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
$table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
$table_id = $row['id'];
$page_id = $row['page'];
}
if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
{
if($password == $table_password)
{
$_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
//echo $table_id;
//echo $page_id;
redirect ($page_id); //take the user to the page specified in the users table
}
else
{
echo "Login Failed";
}
}
else
{
Print '<script>alert("1. Incorrect Password!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
function redirect($page_id)
{
/* Redirect browser */
header('Location: ' . $page_id);
/* Make sure that code below does not get executed when we redirect. */
exit;
}
?>
登录检查
if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] === true) {
"Your script"
}
如果您有一个用户配置文件,比如普通用户=0,管理员=1,您可以这样做
if(isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] === true && $_SESSION['profile'] == 1) {
"Your script"
}
设置会话
要将会话设置为true,您需要
if(isset($_POST['submit'])) {
$_SESSION['loggedIn'] = true;
// for set a profile
$_SESSION['profile'] = 1;
}
也许我不太明白你的意思,但我可以肯定地解释一下:
您说过附加checklogin.php,但不能用它来拒绝非成员的访问。如果他们知道该文件存在,他们可以在URL中键入该文件并仍然读取fileone.php。第一个编码块需要在fileone.php中
会话时间
在php.ini中搜索“session.gc\u maxlifetime”。将会有一个数字,这是以秒为单位的时间。我已经附上了我的checklogin.php。登录页面本身除了“登录”按钮之外没有包含太多内容,然后将用户重定向到此页面。我已经设置了用户名和密码,但这是为了检查数据库中是否存在该用户,然后确保他们在登录后被重定向到个人页面。因此,我意识到,当用户登录和注销时,我需要一些能够触发的东西。您的代码如何识别用户何时离开,或在20分钟内处于非活动状态。谢谢是的,这很有帮助,你在底部解释的部分是完全正确的。这就是我的问题所在,事实上他们可以键入URL并仍然读取文件,所以这个解决方案非常好。我只有3个用户,他们的用户Id是简单的1、2和3。你所说的建立个人资料是什么意思?我的所有用户都有相同的编辑权限等。如果我没有个人资料,我会怎么做?用户ID存储在数据库中,checklogin.php只是确保用户名和密码匹配。谢谢您的帮助。@Rus84如果您有普通用户和管理员,配置文件很有用。不允许普通用户编辑内容,但允许管理员编辑内容。在你的情况下,你可以不带个人资料就离开,因为每个有账户的人都会为此感谢你。作为一个新手,我可以澄清一下在哪里添加这些注释吗:顶部注释(登录检查)需要添加到每个.php文件的顶部(用户将访问的文件,包含他们的信息),设置会话代码需要添加到检查登录?对吗?第一个代码是如何通过键入URL阻止某人访问文件的?感谢您的澄清。@Rus84如果您从左到右,从上到下阅读PHP,您将走得很远!如果他们在url上打开,会话将检查他们。请记住“If(condition is true){do this}else If(或这些条件是否为true){do this}else{do this}”如果没有条件为true,else语句将始终运行。