php最佳实践_2015年PHP技巧,资源和最佳实践-程序员宅基地

技术标签: python  java  linux  编程语言  数据库  

php最佳实践

PHP has had many reputations over the years, but being insecure as a language never really was one of them.

多年来,PHP享有许多声誉,但作为一种语言而缺乏安全感从来没有真正成为其中之一。

The core team, all its faults notwithstanding, is rather quick in pouncing on all security matters, and updating PHP to the latest version will often allay all worries. But the end users, such as we are, tend to mess things up. We don’t update, we use outdated packages or packages with holes in them we’re not aware of, we use ancient extensions… we expose ourselves to risk in some truly creative ways.

尽管存在所有缺点,但核心团队仍会Swift解决所有安全问题,并且将PHP更新到最新版本通常可以消除所有麻烦。 但是像我们这样的最终用户往往会搞砸。 我们不更新,我们使用过时的程序包或其中我们不知道的有Kong的程序包,我们使用古老的扩展程序……我们以某些真正有创意的方式使自己承受风险。

提示,资源和最佳做法 (Tips, Resources and Best Practices)

Let’s start 2015 off right, shall we? This post will list important resources you should have in your brain/toolbelt before building anything with PHP in 2015. I’ll do my best to keep this post up to date, so it stays relevant indefinitely, but, like I said, I’ll need your help.

让我们从2015年开始吧,是吗? 这篇文章将列出您在2015年使用PHP构建任何东西之前应该在大脑/工具带中拥有的重要资源。我会尽力保持此帖子为最新状态,因此它会无限期保持相关性,但是, 就像我说的那样 ,我需要您的帮助。

It’s a good idea to keep a PHP cheat sheet handy so you can refer to some of the basic elements you’ll need to use frequently, without wasting unnecessary time.

最好随身携带一个PHP备忘单,这样您就可以参考一些经常需要使用的基本元素,而又不会浪费不必要的时间。

使您PHP保持最新 (Keep your PHP up to date)

When I wrote about the cancer that is legacy code, I focused on application code. I still firmly believe that you should never develop for the lowest common denominator, else you risk becoming the very thing that’s holding your language’s progress back. That’s not the focus of this section, though. Recently, a new version related discussion ensued.

当我写关于遗留代码癌症时 ,我专注于应用程序代码。 我仍然坚信,您永远不应该为最低的公分母而发展,否则您可能会成为阻碍语言发展的事情。 但是,这不是本节的重点。 最近,有关新版本的讨论随之而来。

CodeIgniter and WordPress are sticking with PHP 5.2 support (a version that’s been dead for four years now and shouldn’t exist on anyone’s server), and Anthony Ferrara responded in his blog post. Before you read this section any further, I implore you to read that post first. It’s important.

CodeIgniter和WordPress一直坚持对PHP 5.2的支持(该版本已经有4年的历史了,并且不应该存在于任何人的服务器上),并且Anthony Ferrara 在他的博客文章中回答。 在您进一步阅读本节之前,请您先阅读该内容。 这一点很重要。

What that post accomplished was getting some people too lazy to upgrade riled up.

那篇文章的成就是使有些人懒得升级。

alt

They argued for legacy support without considering the damage they’re doing to the PHP ecosystem. Anthony wrote another post in which he further explains his stances, and finished up with yet another post taking apart installation percentages of old PHP versions across the web, comparing them to the stability list to find out how many servers running PHP out there are insecure by default and hackable – today. The results are frightening to say the least.

他们主张提供遗留支持,而没有考虑对PHP生态系统造成的损害。 Anthony在另一篇文章中进一步解释了他的立场,并在另一篇文章中总结了网络上旧PHP版本的安装百分比,将它们与稳定性列表进行比较,以找出有多少运行PHP的服务器不安全。默认且可入侵- 今天 。 至少可以说结果令人恐惧。

If you’re developing something and are running anything but the latest major version of PHP, I urge you to give these posts a thorough read. If your client insists on host X or version Y for whatever reason, refer them to these posts, educate them, and help them see the error of their ways – teach them about the vulnerabilities they’re introducing to their project, and tell them about the horrors that can happen if they don’t act before it’s too late. Upgrading your PHP’s version is not something you should file under “we can do that later”. Do it now, and do it often.

如果您正在开发某些东西,并且正在运行最新的主要PHP版本以外的任何东西,我敦促您对这些文章进行全面的阅读。 如果您的客户出于任何原因坚持使用主机X或版本Y,请引导他们参阅这些帖子,进行培训,并帮助他们了解自己的方式中的错误-教给他们有关他们在项目中引入的漏洞,并告诉他们有关如果他们在为时已晚之前不采取行动,就会发生恐怖。 不应将PHP的版本升级到“我们以后可以做”下。 立即执行,并经常执行。

避免过时的主机 (Avoid outdated hosts)

Inspired by the aforementioned discussions, Phil Sturgeon compiled a table of current PHP versions supported by various hosts. You can find it at PhpVersions.info or if you’d like to contribute and add some missing values, on Github.

受上述讨论的启发, Phil Sturgeon编制了一张表格,列出了各种主机支持的当前PHP版本。 您可以在PhpVersions.info上找到它,或者如果您想贡献并添加一些缺失的值,请在Github上找到

I recommend you steer clear of all shared hosting in general – there are extremely cheap VPS providers out there now, like DigitalOcean (feel free to check them out via my ref link for a leg up).

我建议您避开所有共享主机,现在,那里有非常便宜的VPS提供商,例如DigitalOcean(可通过我的参考链接免费检出它们)。

alt

Don’t be these guys. It starts out innocent and simple, but when you end up having to lend out your partner while doing someone else’s laundry, it stops being fun.

不要这些家伙。 它从一开始就简单而简单,但是当您不得不在洗别人的衣服时借给伴侣时,它就不再有趣了。

When opting for a VPS, other than saving you from sharing an environment with everyone else or being susceptible to the instability of a system as introduced by someone else, setting up your own server from scratch is a fun and rewarding experience you should be familiar with anyway. Besides, you can see on the list that barely anyone has the latest PHP version as the default one – why settle for anything but the latest software when starting a new project?

选择VPS时,除了要避免与其他人共享环境或容易受到其他人介绍的系统不稳定的影响外,从头开始设置自己的服务器是您应该熟悉的有趣而有益的体验无论如何。 此外,您可以在列表上看到几乎没有人将最新PHP版本作为默认版本-为什么在启动新项目时除了最新的软件之外,别无其他选择?

采用HTTPS (Adopt HTTPS)

Encryption is crazy important today. Not just as a means of defending yourself from government snooping, but also as a way to make sure your clients and website visitors are protected as well and aren’t leaking any personal data. With advocates such as Ilya Grigorik and his pitches for TLS to Google announcing it would favor websites with HTTPS in search results, there’s no question about the ever increasing importance of HTTPS, even for simple websites.

今天,加密非常重要。 不仅是为了保护自己免受政府的监视,而且是为了确保您的客户和网站访问者也受到保护并且不会泄漏任何个人数据。 Ilya Grigorik等倡导者及其向Google 提出TLS的倡导者宣布,它将在搜索结果中偏爱使用HTTPS的网站,毫无疑问,即使对于简单网站,HTTPS的重要性也越来越高。

alt

While there are workarounds to getting HTTPS everywhere, one shouldn’t rely on those – it’s our responsibility as web developers to improve the web at large. HTTPS is not directly related to PHP, but whenever you’re starting a new PHP project it’s generally easier to set up your server to use HTTPS before you start coding, rather than in the middle of a project. To help you get through this often cryptic, daunting and discouraging task, (at least until Let’s Encrypt is out) Chris Palmer put together this Google Doc.

尽管有各种变通办法可以使HTTPS遍地开花 ,但不应依赖那些变通办法–作为Web开发人员,我们有责任改善整个Web。 HTTPS是没有直接关系PHP,但每当你开始一个新PHP项目它通常更容易设置你的服务器在开始编码,而不是在项目中期之前 ,使用HTTPS。 为了帮助您完成这项通常难以理解,令人生畏和令人沮丧的任务,(至少要等到我们加密之前), 克里斯·帕尔默(Chris Palmer)整理了这份Google文档

保护您PHP (Secure your PHP)

Don’t be these guys.

这些人

alt

Follow best practices in password protection, generation, encryption and authentication. Read books and use packages like those suggested on the SecuringPHP site.

遵循密码保护,生成,加密和身份验证方面的最佳做法。 阅读书籍并使用SecuringPHP网站上建议的软件包。

保持正确的道路 (Stay on the Right Way)

PHP The Right Way is responsible for improving the life of many a PHP project out there. In book form or digital, PTRW is an indispensable resource for making sure you’re fit to handle the challenges of modern app development. If you feel like it’s missing something or just want to contribute with typo corrections or alternative resources and guides, feel free to do so via Github.

PHP正确的方法负责改善许多PHP项目的寿命。 PTRW以书本形式或数字形式提供,是确保您适合应付现代应用程序开发挑战的必不可少的资源。 如果您感觉缺少任何东西,或者只是想对错字更正或其他资源和指南做出贡献,请随时通过Github进行

避免不良包装 (Avoid Bad Packages)

Almost two years ago, Fabien Potencier of Symfony fame announced the creation of a list of vulnerable packages for PHP. A year and a half later, this became standard part of Symfony and was turned into open source public domain property. You could now post your composer.lock file to their API or the web interface, or even the CLI tool, and it would check your project for vulnerabilities. However, this still required one step from the end users, and we’re lazy, lazy people.

大约两年前,Symfony的Fabien Potencier 宣布为PHP创建了一系列易受攻击的软件包 。 一年半后,这成为Symfony的标准部分,并变成了开源公共领域财产。 您现在可以将composer.lock文件发布到他们的API或Web界面,甚至CLI工具中,它将检查您的项目中是否存在漏洞。 然而,这仍然需要从最终用户的一个步骤,我们是懒惰,懒惰的人。

alt

Enter Roave team, the laziest of us. They made a security-advisories package which uses this database of known vulnerabilities. As Marco Pivetta explains in his blog post, you require it in your project like any other package, but instead of downloading anything, the package serves as a meta-package, not downloading anything and instead checking for whether the bad versions are required in your project. It will warn you and prevent even the attempt to download those packages, saving you not only a checking step, but a step that includes deleting them as well.

进入我们最懒的Roave团队。 他们制作了一个安全建议程序包,该程序包使用此已知漏洞数据库。 正如Marco Pivetta在他的博客文章中解释的那样,您需要像其他任何软件包一样在项目中要求它,但该软件包不作为下载任何内容的工具,而是用作元软件包,不下载任何内容,而是检查您的版本是否需要不良版本项目。 它将警告您,甚至阻止您尝试下载那些软件包,不仅节省了检查步骤,还节省了包括删除它们的步骤。

I urge everyone doing PHP development to include this in their projects. By jointly attacking the common vectors of insecurity, we’ll be one step closer to eradicating security holes on a large scale.

我敦促每个进行PHP开发的人员都将其包含在他们的项目中。 通过共同攻击不安全的常见媒介,我们将进一步消除大规模的安全漏洞。

躲避常见错误 (Dodge common mistakes)

We’ve compiled lists of common mistakes before. Read the following posts to learn what to avoid:

我们之前已经编译了常见错误列表。 阅读以下文章,了解要避免的事情:

By keeping these in mind, you’ll save yourself a world of trouble and major headaches down the road.

只要牢记这些,您就可以为自己节省很多麻烦和麻烦。

虚拟化 (Virtualization)

Use Vagrant! Even PTRW says so.

使用流浪汉! 甚至PTRW也是如此。

Vagrant helps you run cloned environments in small, headless virtual machines that forward requests to ports inside the machine, letting you use your host’s browser and your host’s IDE without interference. Want to nest a virtual machine inside a virtual machine? You can do that too, and it’s all completely safe! We’ve got a bunch of Vagrant tutorials and explanations under the Vagrant tag, so if you’re confused about the technology, read up.

Vagrant帮助您在无头的小型虚拟机中运行克隆的环境,这些虚拟机将请求转发到计算机内部的端口,从而使您可以使用主机的浏览器和主机的IDE,而不会受到干扰。 是否要将虚拟机嵌套在虚拟机中? 您也可以这样做,而且完全安全! 我们在Vagrant标签下有很多Vagrant教程和说明,所以如果您对技术感到困惑,请继续阅读。

alt

Here at SitePoint, we have an officially endorsed fork of the Homestead Vagrant box (prepared by Laravel’s Taylor Otwell, but compatible with any framework and PHP application) called Homestead Improved. It’s runnable in under five minutes and you’ll have a completely encapsulated PHP environment to play in – with no fear of messing up your host OS or other projects. Made a mistake? Just destroy, rebuild, and you’re back where you started (with zero code lost) in a minute!

在SitePoint上,我们有Homestead Vagrant盒的正式认可分支(由Laravel的Taylor Otwell准备,但与任何框架和PHP应用程序兼容),称为Homestead Improvement 。 它可以在不到五分钟的时间内运行,并且您将拥有一个完全封装PHP环境,无需担心会弄乱您的主机OS或其他项目。 犯了一个错误? 只需销毁,重建,一分钟您就可以回到开始的位置(丢失零代码)!

Note that we’re using this box in all our tutorials, so getting familiar with it now will both save you some time in the long run, and help you follow along with everything we do with great ease, not to mention the effect it’ll have on your local development environment.

请注意,我们在所有教程中都使用了此框,因此,从长远来看,现在熟悉它既可以节省您一些时间,又可以帮助您轻松地完成我们所做的所有事情,更不用说它的效果了。在您当地的开发环境上。

黑火 (Blackfire.io)

Blackfire.io is a service from SensioLabs, the guys in charge of the Symfony framework and all its related technologies. It’s a transparent and low-overhead profiler able to analyze your code and alert you to issues with everything from application logic flow to interactions with the DB engines and even the cache layer. Blackfire is already installed in the Homestead Improved box mentioned above, so if you use our box or the original Homestead, you’re all set!

Blackfire.io是SensioLabs的服务,SensioLabs是Symfony框架及其所有相关技术的负责人。 这是一个透明且成本低廉的探查器,能够分析代码并警告您有关从应用程序逻辑流到与数据库引擎甚至是缓存层的交互等所有问题。 Blackfire已安装在上述“改进的Homestead”包装盒中,因此,如果您使用我们的包装盒或原始的Homestead,则一切准备就绪!

alt

Catch problems before they throw a wrench into production! More detailed tutorials regarding Blackfire are coming soon!

在扳手投入生产之前发现问题! 有关Blackfire的更多详细教程即将推出!

结论 (Conclusion)

We looked at some important links and resources for starting off your 2015 PHP projects properly, with performance and safety in mind. If you’re already using all these approaches, good for you – you can help us spread the word. Tell your friends and developer circles about it, direct them here, point the newbies who ask you how to get started our way and refer to the specific links in the post whenever someone tells you that legacy code should be supported and old PHP versions are fine. Send them here, and we’ll rough’em up!

我们着眼于性能和安全性,着眼于一些重要的链接和资源,这些链接和资源可正确启动您的2015 PHP项目。 如果您已经在使用所有这些方法,那么对您有好处-您可以帮助我们宣传。 告诉您的朋友和开发人员圈子,将其定向到此处,指出要求您入门的新手,并在有人告诉您应该支持旧代码且旧版本 PHP很好的情况下,引用帖子中的特定链接。 。 将它们发送到这里,我们将开始工作!

Disagree with any of these? Would you add some critical resources that the resources we’ve linked to don’t already mention? Let us know – I’ll make sure the list gets updated!

不同意其中任何一个? 您是否会添加一些我们链接到的资源尚未提及的关键资源? 让我们知道–我将确保列表得到更新!

翻译自: https://www.sitepoint.com/php-tips-resources-best-practices-2015/

php最佳实践

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/culh2177/article/details/108374890

智能推荐

5个超厉害的资源搜索网站,每一款都可以让你的资源满满!_最全资源搜索引擎-程序员宅基地

文章浏览阅读1.6w次,点赞8次,收藏41次。生活中我们无时不刻不都要在网站搜索资源,但就是缺少一个趁手的资源搜索网站,如果有一个比较好的资源搜索网站可以帮助我们节省一大半时间!今天小编在这里为大家分享5款超厉害的资源搜索网站,每一款都可以让你的资源丰富精彩!网盘传奇一款最有效的网盘资源搜索网站你还在为找网站里面的资源而烦恼找不到什么合适的工具而烦恼吗?这款网站传奇网站汇聚了4853w个资源,并且它每一天都会持续更新资源;..._最全资源搜索引擎

Book类的设计(Java)_6-1 book类的设计java-程序员宅基地

文章浏览阅读4.5k次,点赞5次,收藏18次。阅读测试程序,设计一个Book类。函数接口定义:class Book{}该类有 四个私有属性 分别是 书籍名称、 价格、 作者、 出版年份,以及相应的set 与get方法;该类有一个含有四个参数的构造方法,这四个参数依次是 书籍名称、 价格、 作者、 出版年份 。裁判测试程序样例:import java.util.*;public class Main { public static void main(String[] args) { List <Book>_6-1 book类的设计java

基于微信小程序的校园导航小程序设计与实现_校园导航微信小程序系统的设计与实现-程序员宅基地

文章浏览阅读613次,点赞28次,收藏27次。相比于以前的传统手工管理方式,智能化的管理方式可以大幅降低学校的运营人员成本,实现了校园导航的标准化、制度化、程序化的管理,有效地防止了校园导航的随意管理,提高了信息的处理速度和精确度,能够及时、准确地查询和修正建筑速看等信息。课题主要采用微信小程序、SpringBoot架构技术,前端以小程序页面呈现给学生,结合后台java语言使页面更加完善,后台使用MySQL数据库进行数据存储。微信小程序主要包括学生信息、校园简介、建筑速看、系统信息等功能,从而实现智能化的管理方式,提高工作效率。

有状态和无状态登录

传统上用户登陆状态会以 Session 的形式保存在服务器上,而 Session ID 则保存在前端的 Cookie 中;而使用 JWT 以后,用户的认证信息将会以 Token 的形式保存在前端,服务器不需要保存任何的用户状态,这也就是为什么 JWT 被称为无状态登陆的原因,无状态登陆最大的优势就是完美支持分布式部署,可以使用一个 Token 发送给不同的服务器,而所有的服务器都会返回同样的结果。有状态和无状态最大的区别就是服务端会不会保存客户端的信息。

九大角度全方位对比Android、iOS开发_ios 开发角度-程序员宅基地

文章浏览阅读784次。发表于10小时前| 2674次阅读| 来源TechCrunch| 19 条评论| 作者Jon EvansiOSAndroid应用开发产品编程语言JavaObjective-C摘要:即便Android市场份额已经超过80%,对于开发者来说,使用哪一个平台做开发仍然很难选择。本文从开发环境、配置、UX设计、语言、API、网络、分享、碎片化、发布等九个方面把Android和iOS_ios 开发角度

搜索引擎的发展历史

搜索引擎的发展历史可以追溯到20世纪90年代初,随着互联网的快速发展和信息量的急剧增加,人们开始感受到了获取和管理信息的挑战。这些阶段展示了搜索引擎在技术和商业模式上的不断演进,以满足用户对信息获取的不断增长的需求。

随便推点

控制对象的特性_控制对象特性-程序员宅基地

文章浏览阅读990次。对象特性是指控制对象的输出参数和输入参数之间的相互作用规律。放大系数K描述控制对象特性的静态特性参数。它的意义是:输出量的变化量和输入量的变化量之比。时间常数T当输入量发生变化后,所引起输出量变化的快慢。(动态参数) ..._控制对象特性

FRP搭建内网穿透(亲测有效)_locyanfrp-程序员宅基地

文章浏览阅读5.7w次,点赞50次,收藏276次。FRP搭建内网穿透1.概述:frp可以通过有公网IP的的服务器将内网的主机暴露给互联网,从而实现通过外网能直接访问到内网主机;frp有服务端和客户端,服务端需要装在有公网ip的服务器上,客户端装在内网主机上。2.简单的图解:3.准备工作:1.一个域名(www.test.xyz)2.一台有公网IP的服务器(阿里云、腾讯云等都行)3.一台内网主机4.下载frp,选择适合的版本下载解压如下:我这里服务器端和客户端都放在了/usr/local/frp/目录下4.执行命令# 服务器端给执_locyanfrp

UVA 12534 - Binary Matrix 2 (网络流‘最小费用最大流’ZKW)_uva12534-程序员宅基地

文章浏览阅读687次。题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93745#problem/A题意:给出r*c的01矩阵,可以翻转格子使得0表成1,1变成0,求出最小的步数使得每一行中1的个数相等,每一列中1的个数相等。思路:网络流。容量可以保证每一行和每一列的1的个数相等,费用可以算出最小步数。行向列建边,如果该格子是_uva12534

免费SSL证书_csdn alphassl免费申请-程序员宅基地

文章浏览阅读504次。1、Let's Encrypt 90天,支持泛域名2、Buypass:https://www.buypass.com/ssl/resources/go-ssl-technical-specification6个月,单域名3、AlwaysOnSLL:https://alwaysonssl.com/ 1年,单域名 可参考蜗牛(wn789)4、TrustAsia5、Alpha..._csdn alphassl免费申请

测试算法的性能(以选择排序为例)_算法性能测试-程序员宅基地

文章浏览阅读1.6k次。测试算法的性能 很多时候我们需要对算法的性能进行测试,最简单的方式是看算法在特定的数据集上的执行时间,简单的测试算法性能的函数实现见testSort()。【思想】:用clock_t计算某排序算法所需的时间,(endTime - startTime)/ CLOCKS_PER_SEC来表示执行了多少秒。【关于宏CLOCKS_PER_SEC】:以下摘自百度百科,“CLOCKS_PE_算法性能测试

Lane Detection_lanedetectionlite-程序员宅基地

文章浏览阅读1.2k次。fromhttps://towardsdatascience.com/finding-lane-lines-simple-pipeline-for-lane-detection-d02b62e7572bIdentifying lanes of the road is very common task that human driver performs. This is important ..._lanedetectionlite

推荐文章

热门文章

相关标签