保护 PHP 网页访问
Posted
技术标签:
【中文标题】保护 PHP 网页访问【英文标题】:Securing PHP web page access 【发布时间】:2015-12-12 07:07:44 【问题描述】:我有一个用php和mysql编写的登录表单代码(和一个DB打印代码),由六部分组成:dbmanager.php、login.php、index.php、login_process.php、home.php和user.php。
登录过程正常,当有人输入有效的用户名和密码时,该用户将被重定向到 home.php。而且,如果有人输入了无效的用户名或密码,该用户将被重定向到 login_process.php,表明它是无效的用户名或密码,这很好。
问题是,如果任何人直接访问 home.php,则该人无需输入任何用户名或密码即可正确验证。
请大家指导我,以便仅为数据库中的用户保护 home.php 吗?
提前致谢!!非常感谢您的帮助!
我的数据库由四列组成:用户名、密码、姓氏和名字。
这是我的代码:
dbmanager.php
<?php
class DBManager
function getConnection()
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql-5.5"][0]["credentials"];
$db = $mysql_config["name"];
$host = $mysql_config["host"];
$port = $mysql_config["port"];
$username = $mysql_config["user"];
$password = $mysql_config["password"];
$conn = mysql_connect($host . ':' . $port, $username, $password);
if(! $conn )
die('Could not connect: ' . mysql_error());
mysql_select_db($db);
return $conn;
?>
index.php
<?php
require 'user.php';
?>
<html>
<head>
<title>DB Query PHP Page</title>
</head>
<body>
<p>SAMPLE PHP SITE</p>
<p>Contents of table User:</p>
<table border='1'>
<tr>
<td>Username</td>
<td>Password</td>
<td>Last Name</td>
<td>First Name</td>
</tr>
<?php
//refer to user.php for the implementation of the class User
$user_list = (new User())->selectAll();
foreach($user_list as $user)
echo '<tr>';
echo '<td>'.$user->username.'</td>';
echo '<td>'.$user->password.'</td>';
echo '<td>'.$user->lastname.'</td>';
echo '<td>'.$user->firstname.'</td>';
echo '</tr>';
?>
</table>
<br><br>
Click <a href='login.php'>[here]</a> to test the login page.<br>
</body>
</html>
login.php
<html>
<head>
<title>Login Page</title>
</head>
<body>
<p>SAMPLE PHP SITE</p>
<p>Enter Username and Password to Login:</p>
<form action='login_process.php' method='post'>
<table border='1'>
<tr>
<td>Username:</td>
<td><input type='text' name='username'></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password'></td>
</tr>
<tr>
<td> </td>
<td><input type='submit' value='Login'></td>
</tr>
</table>
</form>
</body>
</html>
login_process.php
<?php
require 'user.php';
?>
<?php
$user = new User();
$user->username = $_REQUEST['username'];
$user->password = $_REQUEST['password'];
$found = $user->checkLogin();
if ($found)//redirect to home page
session_start();
$_SESSION['current_user']=$user;
header("Location: home.php");
exit;
else//invalid username and password
echo "Invalid username/password. Click <a href='login.php'>[here]</a> to login again.<br>";
echo "<br>";
echo "You may also click <a href='index.php'>[here]</a> to see the list of usernames and passwords.<br>";
?>
home.php
<?php
require 'user.php';
?>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<p>SAMPLE PHP SITE</p>
<p>
You have successfully logged in
<?php
session_start();
$user = $_SESSION['current_user'];
echo $user->firstname.' '.$user->lastname.'.';
?>
</p>
<p>This is your home page.</p>
</body>
</html>
user.php
<?php
require 'dbmanager.php';
?>
<?php
class User
var $username;
var $password;
var $lastname;
var $firstname;
function checkLogin()
$dbm = new DBManager();
$conn = $dbm->getConnection();
$username = mysql_real_escape_string($this->username);
$password = mysql_real_escape_string($this->password);
$sql_stmt = "SELECT * FROM User WHERE username = '".$username."' AND password = '".$password."'";
//place in retval result of the SQL query
$retval = mysql_query($sql_stmt, $conn);
//check if SQL query is successful
if(! $retval )
mysql_close($conn);
die('Could not read User table: ' . mysql_error());
$found = false;
//get first retrieved row from retval
if ($dbfield = mysql_fetch_assoc($retval))
$found = true;
//initialize fields of this object with the columns retrieved from the query
$this->username = $dbfield['username'];
$this->password = $dbfield['password'];
$this->lastname = $dbfield['lastname'];
$this->firstname = $dbfield['firstname'];
return $found;
function selectAll()
$dbm = new DBManager();
$conn = $dbm->getConnection();
$sql_stmt = "SELECT * FROM User";
//place in retval result of the SQL query
$retval = mysql_query($sql_stmt, $conn);
//check if SQL query is successful
if(! $retval )
mysql_close($conn);
die('Could not read User table: ' . mysql_error());
//create an empty array that will eventually contain the list of users
$user_list=array();
//iterate each row in retval
while ($dbfield = mysql_fetch_assoc($retval))
//instantiate a user object
$user = new User();
//initialize fields of user object with the columns retrieved from the query
$user->username = $dbfield['username'];
$user->password = $dbfield['password'];
$user->lastname = $dbfield['lastname'];
$user->firstname = $dbfield['firstname'];
//add the user object in the array
$user_list[] = $user;
mysql_close($conn);
//return the array
return $user_list;
?>
【问题讨论】:
使用$_SESSION
数组的状态来测试用户是否正确登录。
【参考方案1】:
基本上您必须在本地目录结构中创建一个 lib 目录,并且 lib 中的所有文件都不会公开可用,但您仍然可以从您的 php 应用程序内部访问它们。
以下是 Cloud Foundry 中 PHP 应用程序的文件结构示例:
alexs-mbp:ads-php-test adasilva$ ls -l
total 72
-rw-r--r--@ 1 adasilva staff 490 Sep 14 23:00 README.txt
-rw-r--r--@ 1 adasilva staff 990 Sep 14 19:09 checklogin.php
-rw-r--r--@ 1 adasilva staff 2 Sep 14 23:00 composer.json
drwxr-xr-x@ 3 adasilva staff 102 Sep 14 19:01 images
drwxr-xr-x 3 adasilva staff 102 Sep 14 19:14 includes
-rw-r--r--@ 1 adasilva staff 709 Sep 14 23:00 index.php
drwxr-xr-x 2 adasilva staff 68 Sep 15 17:41 lib
-rw-r--r--@ 1 adasilva staff 261 Sep 14 19:09 loginsuccess.php
-rw-r--r--@ 1 adasilva staff 88 Sep 14 19:11 logout.php
-rw-r--r--@ 1 adasilva staff 809 Sep 14 19:08 main_login.php
-rw-r--r--@ 1 adasilva staff 193 Sep 14 23:00 manifest.yml
-rw-r--r--@ 1 adasilva staff 1157 Sep 14 23:00 style.css
alexs-mbp:ads-php-test adasilva$
lib目录下的任何东西都不会公开,所以你可以把你的home.php文件放在那里。
在此处查看更多详细信息:
http://docs.cloudfoundry.org/buildpacks/php/gsg-php-usage.html
【讨论】:
感谢您的回答!我只是这样做了,但是在我重定向到 home.php 的 login_process.php 中,我更改了 header("Location: home.php"); for header("位置:lib/home.php");但工作不正常,你能告诉我我做错了什么吗? if ($found)//重定向到首页 session_start(); $_SESSION['current_user']=$user; header("位置:lib/home.php");退出;【参考方案2】:您的每个“安全”页面都需要有用户身份验证/验证内容。
这可以很简单:
whatever.php:
<?php
include("usercheck.php");
?>
page stuff here...
usercheck.php:
<?php
session_start();
if (!$_SESSION['logged_in'])
header('Location: login.php');
exit();
【讨论】:
您好,感谢您的回答!我刚刚使用您提供给我的代码创建了一个 usercheck.php 文件,然后将其包含到 home.php 文件中,但对我不起作用:-S 我不知道我是否错过了您向我解释的内容!请纠正我,感谢您的宝贵时间! 您是否将!$_SESSION['logged_in']
更改为!isset($_SESSION['current_user'])
以适应您的代码?这只是一个例子,不要盲目复制。
我之前更改了 current_user 的 logged_in,但没有添加 !isset,但现在可以了!!!谢谢我的朋友!!!以上是关于保护 PHP 网页访问的主要内容,如果未能解决你的问题,请参考以下文章