我有一个问题,获得打开数据库连接的连接变量。
这是我在html
中的代码 <form action="password.php" method="post">
<div class="form-group">
<input type="password" class="form-control" name="current" placeholder="Contraseña Actual..." />
</div>
<div class="form-group">
<input type="password" class="form-control" name="new" placeholder="Nueva Contraseña..." />
</div>
<div class="form-group">
<input type="password" class="form-control" name="confirm" placeholder="Repetir Nueva Contraseña..." />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="q" value="proofQueries">
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> Cambiar</button>
</form>
而类php
的代码$settings = new Datasettings();
require_once('../config.php'); // file of connection of PDO
$conexion = new Conexion();
if(isset($_POST['q'])){ // get the name from html form for go to a function of this class
$settings->$_POST['q']($conexion);
}
class Datasettings {
function __construct(){
session_start();
if(!isset($_SESSION['id'])){
header('location:mystyle.css');
}
}
function proofQueries($conexion){
}
... other functions....
可以改变模型我如何调用一个函数?我怎么能做到呢?
我假设这段代码:
if(isset($_POST['q'])){ // get the name from html form for go to a function of this class
$settings->$_POST['q']($conexion);
}
并提交名为q
的隐藏表单字段,其值为proofQueries
,您正在尝试调用$settings->proofQueries($conexion)
。这是一个非常糟糕的主意。
你正在有效地执行直接来自客户端的代码,这是一个巨大的漏洞风险。
这似乎是一个奇怪的方法,开始指定客户端函数,然后在PHP中执行它(即服务器端)。为什么要指定q
值,而不是在PHP中显式地执行$settings->proofQueries($conexion)
?
如果你以某种方式必须指定要在客户端调用的函数,这样做:
if(isset($_POST['q'])){ // get member function from submitted form
$f = $_POST['q'];
if ($f=='proofQueries') {
$settings->proofQueries($conexion);
}
else {
die("Nope");
}
}
或者如果你有多个可能的函数,用白名单显式地过滤它们,使绝对100%确定只有你决定的函数名可以被调用:
if(isset($_POST['q'])){ // get member function from submitted form
$f = $_POST['q'];
$allowedFunctions = array('proofQueries','doSomething','otherFunction');
if (in_array($f,$allowedFunctions)) {
$settings->$f($conexion);
}
else {
die("Nope");
}
}
但是,这似乎是一个奇怪的方法。不应该通过客户端指定服务器端特定的实现细节。