如何保护PHP脚本?

Guu*_*ijn 4 php security ftp

我有一个PHP脚本.在这个脚本中,我将连接到FTP服务器,并显示一个文件.什么是保护此脚本的最佳方法,因此只能在1小时内登录5次或类似的东西?

我的剧本:

<?php
session_start();

$_SESSION["ftp_user"] = $_POST["user"];    
$_SESSION["ftp_pass"] = $_POST["pass"];
$ftp_pass = $_POST["pass"];
$ftp_server = "ftp.guusvanwalstijn.nl";

function test()    
{ 
echo "Do whatever i want";
}


// Verbinding maken
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 


// Error die je krijgt als je het wachtwoord verkeerd hebt ingetypt
function error_while_connecting() {
echo "Error while connecting to the server";
echo "<br />Probably the password is wrong or the server is offline";
}

// Inloggen met de gegeven inloggegevens
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    test();

} else {
    error_while_connecting();
    print("<br />Debug: " . $_POST["pass"] . "<br />");
    print("<br />Debug: " . $_POST["user"] . "<br />");
}

ftp_close($conn_id);
?>
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以将日志表(时间戳)添加到数据库或将其存储在文件中.从中获取最后5条记录,并检查最旧的记录是否超过1小时.如果是,则可以允许连接到数据库.如果没有,请拒绝,因为已达到限制.您还可以定期清除此文件/ db表的内容以避免过多增长.