Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这个脚本可以改进吗?_Php_Security_Design Patterns - Fatal编程技术网

Php 这个脚本可以改进吗?

Php 这个脚本可以改进吗?,php,security,design-patterns,Php,Security,Design Patterns,好的,我想知道存储在user_login.php中的代码是否可以改进,或者我是否做得不对。我很困惑,因为我在应用程序中的所有脚本都只有30-40行,我想知道我是否遗漏了什么 与我的应用程序中除模板文件之外的所有其他脚本一样,此脚本通过ajax调用进行调用 <?php # Ignore if (!defined('APP_ON')) { die('Silence...'); } # Gets the variables sent $user_name = post('user_name'

好的,我想知道存储在user_login.php中的代码是否可以改进,或者我是否做得不对。我很困惑,因为我在应用程序中的所有脚本都只有30-40行,我想知道我是否遗漏了什么

与我的应用程序中除模板文件之外的所有其他脚本一样,此脚本通过ajax调用进行调用

<?php

# Ignore
if (!defined('APP_ON')) { die('Silence...'); }

# Gets the variables sent
$user_name = post('user_name');
$user_password = extra_crypt(post('user_password'));

# Check if the user exists
if (!user::check($user_name, $user_password)) { template::bad($lang['incorrect_login']); }

# Logging in
$id = user::get_id($user_name, $user_password);
$u = new user($id);
$u->login();
template::good($lang['correct_login']);

?>
这基本上是检查文件是否没有被直接调用。每个url都被重定向到一个index.php文件,该文件管理url(es:www.mysite.com/user/view/id/1)并包含正确的文件。在index.php文件的第一行中有一个
define('APP_ON',true)。索引文件还初始化调用某些函数集和某些类的应用程序

# Gets the variables sent
$user_name = post('user_name');
$user_password = extra_crypt(post('user_password'));
函数
post()
通过一些检查来管理$\u post['user\u name']的恢复。 函数
extra\u crypt()
只需使用sha1和自定义alghoritm对密码进行加密即可。 我在sql查询中使用prepared语句,所以不用担心转义post变量

# Check if the user exists
if (!user::check($user_name, $user_password)) { template::bad($lang['incorrect_login']);
user
类既有静态方法也有常规方法。静态方法不需要从id开始,普通方法需要。例如
user::check()
检查数据库中是否存在用户名和密码。
template
类只有两个静态方法(
template::bad()
template::good()
)可以管理快速对话框,以便发送给用户而不需要任何页眉或页脚。相反,如果实例化类
$t=new template('user\u view')
,则会调用模板
user\u view\u body.php
,您可以使用
$t->assign()
为模板分配静态变量,或者使用启动循环的
$t->loop()
来管理该页面。 最后,
$lang
是一个数组,在用户设置的语言中有一些公共字符串

# Logging in
$id = user::get_id($user_name, $user_password);
$u = new user($id);
$u->login();
template::good($lang['correct_login']);
最后我们有了实际的登录。将实例化
用户
类并恢复id。具有该id的用户已登录,我们将向用户返回一个
template::good()
消息框。
对于任何澄清,请在上面写一条评论。

由于函数背后的功能太多,您还没有提供,很难说是否需要修复任何东西。您是否确保
post()
中的输入是干净的?
user::check()中的数据库调用是否安全?您是否正在使用会话cookie简化用户首选项/信息存储?你的
模板
类写得好吗?创建新用户时,是否记录了所有必要的信息(登录时间、IP地址(如有必要)等)?如果这在这里很重要,您是否确保用户没有双重登录


关于你写的东西,我能告诉你的唯一具体的事情是,你应该用其他的东西来代替;有关建议,请参见下面的评论。

由于功能背后有很多功能,您还没有提供,很难说是否需要修复。您是否确保
post()
中的输入是干净的?
user::check()中的数据库调用是否安全?您是否正在使用会话cookie简化用户首选项/信息存储?你的
模板
类写得好吗?创建新用户时,是否记录了所有必要的信息(登录时间、IP地址(如有必要)等)?如果这在这里很重要,您是否确保用户没有双重登录


关于你写的东西,我能告诉你的唯一具体的事情是,你应该用其他的东西来代替;请参阅下面的评论以获取建议。

首先,像sha1这样的消息摘要函数不是加密函数。因此,请从函数名中删除
crypt
,以避免混淆。此外,存储密码的“自定义算法”的整个想法让我非常害怕。sha256是比sha1更好的选择,但sha1并没有那么糟糕,毕竟它仍然是NIST批准的功能。与md5不同,没有人能够为sha1生成碰撞(尽管这将在未来几年内发生)。如果您使用sha1,请确保salt是前缀,以阻止前缀攻击

输入验证必须始终在使用时进行。post()函数不应负责任何输入验证或转义。这只是
magic\u quotes\u gpc
的一个化身,因为它是一个安全列车环而被删除

参数化查询库(如PDO和ADODB)非常好,我建议使用它。这是使用时消毒的一个很好的例子


您的模板
assign()
方法可用于清理XSS。Smarty附带了一个
htmlspecialchars
output filter模块,用于执行此操作

首先,像sha1这样的消息摘要函数不是加密函数。因此,请从函数名中删除
crypt
,以避免混淆。此外,存储密码的“自定义算法”的整个想法让我非常害怕。sha256是比sha1更好的选择,但sha1并没有那么糟糕,毕竟它仍然是NIST批准的功能。与md5不同,没有人能够为sha1生成碰撞(尽管这将在未来几年内发生)。如果您使用sha1,请确保salt是前缀,以阻止前缀攻击

输入验证必须始终在使用时进行。post()函数不应负责任何输入验证或转义。这只是
magic\u quotes\u gpc
的一个化身,因为它是一个安全列车环而被删除

参数化查询库(如PDO和ADODB)非常好,我建议使用它。这是使用时消毒的一个很好的例子

可以使用模板
assign()
方法
# Logging in
$id = user::get_id($user_name, $user_password);
$u = new user($id);
$u->login();
template::good($lang['correct_login']);