PHP - 读取ftp文本文件并提取所需数据

时间:2012-05-20 03:22:59

标签: php regex text ftp extract

这是我的第一个php脚本。我实际上在vb.net中编码..我正在为.net应用程序制作这个许可系统。该许可系统有一个管理员可以轻松控制和查看的管理员。我也建立一个类库来登录并注册相同的。我仅使用vb.net代码成功完成了这项工作,但由于凭据需要存储在应用程序中,因此始终存在威胁。使用php这个问题可以在某种程度上克服:舌头:。所以我决定使用这种PHP脚本制作登录+注册系统。我正在使用一个只有管理员阅读,写文本文件而不是一个mysql数据库(轻松maneagable所有托管服务)。所以,我已经提出了以下这段代码,我在确认登录部分时需要一些帮助。 文本文件是这样的:

用户名密码hwid lastdate membershiptype

全部由'space'分隔,每行一个帐户。 我希望我已经提供了足够的信息,如果需要额外的信息,我会给它。

<?php
$user = addslashes($_GET['username']);
$pass = addslashes($_GET['password']);

  $username = "theusername";  
  $password = "thepassword";  
  $url = "mywebsite.com/file.txt";
  $hostname= "ftp://$username:$password@$url";  
  $contents = file_get_contents($hostname); 
// That gives me the txt file which can only be read and written by the admin
if (strpos($contents,$user) !== false) {
   // Need code here to check if the adjacent word and the $pass are same to establish a successfull login
} else {
 echo "Username does not exist, please register"
}
?>

1 个答案:

答案 0 :(得分:1)

在这里尝试一下,希望它有所帮助:

file.txt需要采用这种格式,值由:分隔,空格不是分隔值的好方法。

username:password:hwid:lastdate:membershiptype

PHP位:

<?php
$user = $_GET['username'];
$pass = $_GET['password'];

if(check_auth(get_auth(),$user,$pass)==true){
    echo 'Yes';
}else{
    echo 'No';
}

/**
 * This function will grab the text file and create a user array
 * 
 * @return array(0=>username,1=>password)
 */
function get_auth(){
    $username = "theusername";
    $password = "thepassword";
    $url = "mywebsite.com/file.txt";
    $location = "ftp://$username:$password@$url";

    $users = file($location);
    function split_auth(&$value){
        $value = explode(':',$value);
    }
    array_walk($users,'split_auth');
    return $users;
}

/**
 * This Function will check the username and password
 *  against the users array
 *
 * @param array $users
 * @param string $username
 * @param string $password
 * @return bool (true|false)
 */
function check_auth($users,$username,$password){
    foreach($users as $user){
        if($user[0]==$username && $user[1]==$password){
            return true;
        }
    }
    return false;
}
?>
相关问题