Hướng dẫn php 5 to php 8 code converter - công cụ chuyển đổi mã php 5 sang php 8

Trong video này, chúng tôi sẽ xem xét Rector, một công cụ OpenSource giúp tăng tốc đáng kể quá trình di chuyển sang PHP 8 hoặc bất kỳ phiên bản PHP nào khác.

Liên kết

  • FRONT LINE PHP: Cuốn sách giải thích tất cả các tính năng PHP 8 và độ sâu PHP hiện đại
  • Hiệu trưởng trên GitHub

-1

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi có một trang web dựa trên Php 5.4 và tôi muốn chuyển đổi nó thành Php 8, đây là lần đầu tiên tôi có một nhiệm vụ như vậy để tôi có ý tưởng "bằng không" Một hướng dẫn làm thế nào tôi có thể làm điều đó.

Cảm ơn bạn trước

Hỏi ngày 11 tháng 12 năm 2021 lúc 15:54Dec 11, 2021 at 15:54

Hướng dẫn php 5 to php 8 code converter - công cụ chuyển đổi mã php 5 sang php 8

5

Nó có thể hữu ích và tiết kiệm nhiều giờ để quét các lỗi và cảnh báo với khả năng tương đương phpcomp

Công cụ này phân tích nguồn mã của bạn và tạo báo cáo chi tiết với các cảnh báo và lỗi cho mỗi tệp PHP.

Nếu bạn sử dụng Docker, bạn chỉ cần chạy lệnh bên dưới từ dự án Root Dir:

docker run --rm -v "$(pwd):/app" vfac/php7compatibility 8.0 --extensions=php .

Đã trả lời ngày 11 tháng 12 năm 2021 lúc 22:13Dec 11, 2021 at 22:13

Hướng dẫn php 5 to php 8 code converter - công cụ chuyển đổi mã php 5 sang php 8

Damosse31Damosse31Damosse31

3725 Huy hiệu bạc15 Huy hiệu Đồng5 silver badges15 bronze badges

1

Mặc dù thực tế là PHP 7.0 là một phiên bản chính mới, những nỗ lực đã được thực hiện để làm cho việc di chuyển không đau nhất có thể. Bản phát hành này tập trung chủ yếu vào việc loại bỏ chức năng không được phản đối trong các phiên bản trước và cải thiện tính nhất quán của ngôn ngữ.

Có một vài tính năng không tương thích và các tính năng mới cần được xem xét và mã nên được kiểm tra trước khi chuyển đổi phiên bản PHP trong môi trường sản xuất.

TL; DR: Nhiều ứng dụng PHP vẫn đang chạy trên PHP 5.x, chưa sẵn sàng để tận dụng tối đa các tính năng tuyệt vời mà PHP 7 cung cấp. Rất nhiều nhà phát triển đã không thực hiện chuyển đổi vì những lo ngại nhất định về các vấn đề tương thích, những thách thức di cư và cảm giác khó xử kỳ lạ rằng việc di chuyển sẽ lấy đi một phần lớn thời gian của họ. Trong phần đầu tiên của hướng dẫn này, chúng tôi đã học được cách thiết lập môi trường phát triển PHP 7. Trong phần thứ hai của hướng dẫn này, chúng tôi đã thảo luận rộng rãi về tất cả các tính năng mới của Php 7 và các cấu trúc và tính năng ngôn ngữ đã bị xóa hoặc không dùng nữa. Lần này, chúng tôi sẽ chỉ cho bạn cách bạn có thể tận dụng tất cả các tính năng PHP 7 mới khi di chuyển và cả các công cụ sẽ giúp làm cho quá trình không gây đau đớn! Many PHP applications are still running on PHP 5.x, not ready to take full advantage of the awesome features that PHP 7 offers. A lot of developers have not made the switch because of certain fears of compatibility issues, migration challenges and the strange awkward feeling that migrating will take away a big chunk of their time. In the first part of this tutorial we learned how to set up a PHP 7 development environment. In the second part of this tutorial, we discussed extensively about all the new features PHP 7 offers and the language constructs and features that have been either removed or deprecated. This time, we'll show you how you can leverage all the new PHP 7 features when migrating and also the tools that will help to make the process painless!


Bạn cần lưu ý rằng phần lớn, mã PHP 5.x có thể chạy trên PHP 7. Trong Php 7, có một số thay đổi không tương thích ngược, vì vậy các ứng dụng được xây dựng với PHP 5.x sử dụng các hàm và cấu trúc ngôn ngữ đã được Đã loại bỏ hoặc thực hiện nội bộ đã thay đổi mạnh mẽ sẽ gây ra lỗi trong khi cố gắng chạy trên Php 7.

Một trong những phần khó khăn nhất trong công việc của chúng tôi khi các nhà phát triển phần mềm phải làm việc trên các cơ sở mã cũ. Trong một tình huống mà bạn được giao nhiệm vụ di chuyển một ứng dụng PHP 5.x lớn có lẽ đã tồn tại trong khoảng 10 năm, bạn sẽ đi về nó như thế nào?

Cách di chuyển dễ nhất và rõ ràng nhất là ban đầu sao chép ứng dụng trên máy cục bộ của bạn, cài đặt Php 7 và chạy ứng dụng. Bạn có thể đi qua các lỗi và cảnh báo giảm giá được hiển thị trong thiết bị đầu cuối và sửa chữa thủ công chúng từng bước bằng cách kết hợp các tính năng PHP 7. Điều này có thể rất khó khăn và tốn thời gian. Tại sao chúng ta không thể tự động hóa quá trình này?

Hiện tại không có công cụ nào thực hiện chuyển đổi tự động 100% của cơ sở mã PHP 5.x của bạn thành PHP 7, nhưng các công cụ này bên dưới sẽ giúp bạn di chuyển không đau.

Php 7 Mar

Php7Mar là một công cụ dòng lệnh tạo ra các báo cáo về cơ sở mã PHP 5.x dựa trên khả năng tương thích PHP 7. Các báo cáo chứa số dòng, các vấn đề được ghi nhận và đề xuất sửa chữa cùng với các liên kết tài liệu.

Lưu ý: Công cụ không sửa mã. Nó chỉ cung cấp cho bạn các báo cáo về tất cả các tệp PHP trong cơ sở mã của bạn. Hạnh phúc sửa chữa! The tool does not fix code. It only gives you reports about all the PHP files in your codebase. Happy fixing!

Trình kiểm tra tương thích PHP 7

PHP7CC là một công cụ dòng lệnh được thiết kế để di chuyển từ PHP 5.3 - 5.6 sang PHP 7 thực sự dễ dàng. Báo cáo PHP7CC:

  • Lỗi: Fatal, Cú pháp, Thông báo. Chúng được làm nổi bật trong màu đỏ. Fatal, Syntax, Notice. These are highlighted in red.
  • Cảnh báo: Chúng được tô sáng màu vàng. These are highlighted in yellow.

Phan

Phan là một máy phân tích tĩnh cho PHP cố gắng chứng minh tính không chính xác hơn là tính chính xác. Phan tìm kiếm các vấn đề phổ biến và xác minh khả năng tương thích loại trên các hoạt động khác nhau khi có sẵn thông tin loại hoặc có thể được suy luận. Phan kiểm tra rất nhiều thứ bao gồm cả khả năng tương thích ngược của PHP7/PHP5.

phpto7aid

PHPTO7AID là một công cụ được sử dụng để xác định mã PHP 5 sẽ không hoạt động trong PHP 7. Nó cố gắng hỗ trợ bạn nhiều nhất có thể trong việc giải quyết các vấn đề này, bằng cách cung cấp giải pháp chính xác hoặc đưa ra gợi ý về cách giải quyết vấn đề.

Phpstorm Php 7 Kiểm tra khả năng tương thích

Phpstorm là một ide PHP rất thông minh, được phát triển bởi JetBrains.

Nguồn: JetBrains.com

PhpStorm 10 đi kèm với công cụ kiểm tra khả năng tương thích PHP 7 có thể hiển thị chính xác mã nào sẽ gây ra lỗi nếu bạn đang chạy PHP7.

Nguồn: JetBrains.com

PhpStorm 10 đi kèm với công cụ kiểm tra khả năng tương thích PHP 7 có thể hiển thị chính xác mã nào sẽ gây ra lỗi nếu bạn đang chạy PHP7.Run Inspection By Name option from the Code menu, and then selecting the PHP 7 Compatibility section will give you results like this one below:

Nguồn: JetBrains.com

PhpStorm 10 đi kèm với công cụ kiểm tra khả năng tương thích PHP 7 có thể hiển thị chính xác mã nào sẽ gây ra lỗi nếu bạn đang chạy PHP7.

Hình ảnh dưới đây cho thấy một ví dụ điển hình về một ứng dụng có các lớp có tên được dành riêng trong PHP 7. Chọn tùy chọn kiểm tra chạy theo tên từ menu mã, sau đó chọn phần tương thích PHP 7 sẽ cung cấp cho bạn kết quả như thế này bên dưới:

  • Người dùng sẽ có thể đăng ký trên ứng dụng.
  • Người dùng sẽ có thể đăng nhập vào ứng dụng.
  • Người dùng sẽ được gán tên mã Star Wars ngẫu nhiên.
  • Người dùng sẽ có thể đăng xuất khỏi ứng dụng.

Xây dựng ứng dụng này sẽ yêu cầu chúng tôi thiết lập cơ sở dữ liệu để lưu trữ người dùng, viết mã đăng ký và mã đăng nhập của chúng tôi và quản lý phiên người dùng. Bây giờ, chúng tôi sẽ không sử dụng bất kỳ khung nào vì chúng tôi không muốn bất kỳ hình thức chi phí nào. Thông thường, việc xây dựng ứng dụng này sẽ mất rất nhiều thời gian và thiết lập nhưng có một dịch vụ chúng ta có thể sử dụng để loại bỏ rắc rối. Ồ, vâng, Auth0 để giải cứu!

Auth0 cung cấp các công cụ giao diện người dùng đơn giản và dễ sử dụng nhất để giúp quản trị viên quản lý danh tính người dùng bao gồm đặt lại mật khẩu, tạo và cung cấp, chặn và xóa người dùng.

Tạo và định cấu hình máy khách Auth0

Điều đầu tiên chúng tôi cần làm là đăng ký tài khoản Auth0 miễn phí và định cấu hình máy khách mới.

Auth0 cung cấp một tầng miễn phí hào phóng để bắt đầu với xác thực hiện đại.free tier to get started with modern authentication.

Bây giờ hãy truy cập vào tab Máy khách và tạo một ứng dụng mới chọn 'Ứng dụng web thông thường' dưới dạng loại máy khách. Hãy đặt tên cho nó là một cái gì đó giống như 'Appal WebApp "cơ bản'.

Bây giờ chúng tôi đã tạo khách hàng của mình, chúng tôi cần lưu ý ba thuộc tính:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
8,
{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
9 và
body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
0. Tất cả chúng có thể được tìm thấy trên tab
body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
1 của máy khách mà chúng tôi vừa tạo.

Cấu hình cuối cùng mà chúng tôi cần làm, trước khi cập nhật mã của mình, là thêm

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
2 dưới dạng
body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
3 trên máy khách AUTH0 của chúng tôi.

Xây dựng ứng dụng

Tạo tệp

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
4 trong một thư mục mới và thêm nó vào nó như vậy:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}

composer.json

Tất cả những gì chúng ta cần là gói

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
5 để đọc các biến môi trường và gói
body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
6 giúp dễ dàng sử dụng dịch vụ AUTH0.

Tạo một thư mục

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
7 bên trong thư mục Thêm tệp
body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
8 trong đó.

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}

app.css

Đi trước và tạo tệp

body {
  font-family: "proxima-nova", sans-serif;
  text-align: center;
  font-size: 300%;
  font-weight: 100;
}
input[type=checkbox],
input[type=radio] {
  position: absolute;
  opacity: 0;
}
input[type=checkbox] + label,
input[type=radio] + label {
  display: inline-block;
}
input[type=checkbox] + label:before,
input[type=radio] + label:before {
  content: "";
  display: inline-block;
  vertical-align: -0.2em;
  width: 1em;
  height: 1em;
  border: 0.15em solid #0074d9;
  border-radius: 0.2em;
  margin-right: 0.3em;
  background-color: white;
}
input[type=radio] + label:before {
  border-radius: 50%;
}
input[type=radio]:checked + label:before,
input[type=checkbox]:checked + label:before {
  background-color: #0074d9;
  box-shadow: inset 0 0 0 0.15em white;
}
input[type=radio]:focus + label:before,
input[type=checkbox]:focus + label:before {
  outline: 0;
}
.btn {
  font-size: 140%;
  text-transform: uppercase;
  letter-spacing: 1px;
  border: 0;
  background-color: #16214D;
  color: white;
}
.btn:hover {
  background-color: #44C7F4;
}
.btn:focus {
  outline: none !important;
}
.btn.btn-lg {
  padding: 20px 30px;
}
.btn:disabled {
  background-color: #333;
  color: #666;
}
h2,
h2,
h3 {
  font-weight: 100;
}
#logo img {
  width: 300px;
  margin-bottom: 60px;
}
.home-description {
  font-weight: 100;
  margin: 100px 0;
}
h2 {
  margin-top: 30px;
  margin-bottom: 40px;
  font-size: 200%;
}
label {
  font-size: 100%;
  font-weight: 300;
}
.btn-next {
  margin-top: 30px;
}
.answer {
  width: 70%;
  margin: auto;
  text-align: left;
  padding-left: 10%;
  margin-bottom: 20px;
}
.login-page .login-box {
  padding: 5px 0;
}
9 bên trong thư mục như vậy:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Tạo một tệp

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
0. Tệp này sẽ chứa thông tin xác thực AUTH0 của chúng tôi.

AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'

Lưu ý: Thay thế các giá trị này bằng

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
1,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
2 và
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
3 từ bảng điều khiển AUTH0 của bạn.
Replace these values with the
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
1,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
2 and
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
3 from your Auth0 dashboard.

Thêm giá trị của

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
4 vào các URL gọi lại được phép trong cài đặt của bạn trên bảng điều khiển.Allowed Callback URLs in your Settings on the dashboard.

Bảng điều khiển Auth0: URL gọi lại cho phép

Ngoài ra, đừng quên thêm cùng một giá trị vào nguồn gốc được phép (CORS) trong cài đặt của bạn trên bảng điều khiển.Allowed Origins(CORS) in your Settings on the dashboard.

Chúng tôi cần một tệp để gọi thư viện

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
5 và tải các giá trị mà chúng tôi đã gửi trong tệp
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
0. Tạo một tệp mới, dotenv-loader.php như vậy:

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }

dotenv-loader.php

Cuối cùng, hãy tạo tệp

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
7 trong đó tất cả logic ứng dụng của chúng tôi sẽ nằm. Giống như tôi đã đề cập trước đó, nó chỉ là một ứng dụng cơ bản vì vậy đừng lo lắng về việc tách các mối quan tâm.

Đây là cách mà tệp sẽ trông như thế nào:

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>

Thư giãn, hãy phân tích mã cùng nhau.

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

Đây là nơi chúng tôi yêu cầu bộ tải tự động tải và trình tải của nhà soạn nhạc dotenv. Trình tải tự động giúp chúng tôi có thể nhập bất kỳ lớp nào từ các gói PHP được cài đặt trong ứng dụng.

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
8 là lớp xác thực AUTH0. Nó có các phương pháp để truy xuất hồ sơ của người dùng khi đăng nhập.

Sau đó, chúng tôi chuyển sang khởi tạo lớp

AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
5.

Phương thức

AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
6 theo mặc định lưu trữ thông tin người dùng trong phiên PHP và chúng tôi cũng đã hướng dẫn nó lưu
AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
7 và
AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
8.

AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
9 là URL ủy quyền.

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
0 Mảng chứa một số nhân vật từ Star Wars. Sau đó trong mã, người dùng sẽ được gán tên mã ngẫu nhiên từ mảng này.

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
1 Lấy thông tin người dùng.

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

Điều này kiểm tra xem người dùng đã gửi yêu cầu đăng xuất, xóa phiên và chuyển hướng người dùng trở lại trang chủ.

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
0

Chúng tôi đang sử dụng trang đăng nhập tập trung của Auth0 và chúng tôi cũng sử dụng jQuery để gọi các phương thức và xử lý sự kiện nhấp vào nút.

Trang đăng nhập tập trung của Auth0 là cách an toàn nhất để dễ dàng xác thực người dùng cho các ứng dụng của bạn.

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
1

Kéo trong bootstrap và phông chữ để làm đẹp.

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
2

Trong mã trên, nếu

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
2 không được đặt, thì điều đó có nghĩa là người dùng chưa đăng nhập, vì vậy chúng tôi hiển thị nút Signin. Nếu người dùng đã đăng nhập, thì chúng tôi lấy thông tin của người dùng và hiển thị nó cùng với nút
<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
3.

Lưu ý bảo mật quan trọng: Nếu bạn có API cho ứng dụng của mình, không nên sử dụng

AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
8 để bảo mật API; Thay vào đó, sử dụng
<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
5 với cấu hình thích hợp. Bạn có thể đọc về cách thực hiện xác thực API với AUTH0 với tài trợ ngầm.
If you have an API for your application, the
AUTH0_DOMAIN='blahabababababa.auth0.com'
AUTH0_CLIENT_ID='xxxxxxxxx'
AUTH0_CLIENT_SECRET='xxxxxxxxx'
AUTH0_CALLBACK_URL='http://localhost:3000'
8 should not be used to secure the API; instead use an
<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
5 with the appropriate configuration. You can read about how to implement API authentication with Auth0 with implicit grant.

Chạy ứng dụng

Chuyển đến thiết bị đầu cuối của bạn và chạy

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
6 để cài đặt các phụ thuộc. Tiếp theo, chạy máy chủ PHP 5.x của bạn. Nếu máy chủ PHP của bạn có thể truy cập được từ thiết bị đầu cuối, thì bạn có thể chạy nó qua
<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
7.

Mở trình duyệt của bạn và kiểm tra ứng dụng. Trang chỉ mục sẽ trông như thế này:

Hướng dẫn php 5 to php 8 code converter - công cụ chuyển đổi mã php 5 sang php 8
Trang mục lục

Bây giờ, đăng ký & dấu hiệu.

Đăng nhập

Khi bạn đã đăng nhập, bạn nên được chỉ định một mã mã Star Wars như vậy:

Đăng nhập

Ứng dụng của chúng tôi hiện đang chạy thành công trên máy chủ PHP 5.x. Bạn có thể lấy mã nguồn từ GitHub để đảm bảo mọi thứ hoạt động như mong đợi.

Di chuyển ứng dụng PHP5 của chúng tôi sang PHP7

Chúng tôi hiện đang chạy một ứng dụng PHP 5.x. Hãy di chuyển nó sang PHP 7. Điều tốt là hầu hết các lần bạn có thể không phải thay đổi bất cứ điều gì trong cơ sở mã. Hãy xem nếu điều đó đúng với ứng dụng này.

Nâng cấp máy chủ của bạn lên ít nhất Php 7.0.0 và chạy lại ứng dụng này.

Máy chủ Php 7 đang chạy

Ứng dụng chạy trên Php 7 mà không có bất kỳ lỗi nào

Tuyệt vời, bây giờ ứng dụng đầu tiên của chúng tôi đang chạy trên Php 7 thành công!

Làm việc với ứng dụng thứ hai

Ứng dụng PHP thứ hai chúng tôi sẽ trải qua là API. Đó là một API Chuck Norris đơn giản. Nó đã được xây dựng với Php 5 trong tâm trí.

CLONE nó từ GitHub và chạy

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
6 để cài đặt tất cả các phụ thuộc. Sau đó chạy ứng dụng trên máy chủ PHP 5.x.

Mở Postman và kiểm tra API như vậy:

Chạy

<?php

  // Read .env
  try {
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
  } catch(InvalidArgumentException $ex) {
    // Ignore if no dotenv
  }
9 như vậy:

API hiển thị danh mục

Chạy

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
0 như vậy:

API hiển thị những trò đùa ngẫu nhiên

Ứng dụng đang hoạt động tốt, không có lỗi!

Sử dụng các tính năng Php 7 trong ứng dụng thứ hai

Hãy tái cấu trúc ứng dụng này và tích hợp một số tính năng PHP 7.

Đây là cấu trúc thư mục của ứng dụng API của chúng tôi tại thời điểm này:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
3

Đây là cách mà tệp

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
1 của chúng tôi trông giống như ngay bây giờ:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
4

Hãy bắt đầu bằng cách thêm khai báo loại trả về Php 7 vào các phương thức trong lớp này như vậy:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
5

Các khai báo loại trả về Php 7 được thêm vào trong main.php

Một tính năng PHP 7 khác mà chúng ta có thể thêm là Kiểu chữ tham số chức năng. Chúng tôi có một phương pháp,

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
2 chấp nhận
<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
3 là một số nguyên.

Hãy tái cấu trúc phương pháp đó,

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
4. Chúng tôi sẽ loại bỏ điều kiện
<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
5 và chỉ cần TypeHint tham số
<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
3 như vậy:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
6

Bây giờ nếu bạn cố gắng vượt qua một giá trị bên cạnh một số nguyên như vậy:

{
    "name": "basic php webapp",
    "description": "Basic sample for securing a WebApp with Auth0",
    "require": {
         "vlucas/phpdotenv": "2.3.0",
         "auth0/auth0-php": "~4.0"
    },
    "license": "MIT"
}
7

index.php

Php 7 sẽ ném một loại lỗi như vậy:

Php 7 typeerror

Chúng tôi đã có thể thêm một số tính năng PHP 7. Ứng dụng này cũng chạy trên máy chủ Php 7 và mọi thứ đều hoạt động tốt!

Mã nguồn của phiên bản Php 7 của API có thể được tìm thấy trên

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
7 trên GitHub.

Màn biểu diễn

Php 7 chạy trên Zend Engine 3.0 mới, do đó làm cho các ứng dụng của bạn thấy hiệu suất nhanh hơn 2 lần và mức tiêu thụ bộ nhớ tốt hơn 50% so với Php 5.6. Nó cũng cho phép bạn phục vụ người dùng đồng thời hơn mà không cần thêm bất kỳ phần cứng nào.

Rasmus Ledorf, người tạo PHP và người phát minh ra mệnh đề giới hạn SQL đã thực hiện một số điểm chuẩn với một vài dự án PHP phổ biến với các phiên bản khác nhau của PHP từ PHP 5.4 trở lên cho đến PHP 7.0 và cũng được đánh dấu so với HHVM 3.6.1.

Hãy xem xét kỹ điểm chuẩn. Thông số kỹ thuật của hộp kiểm tra Rasmus được sử dụng là:

  • Gigabyte Z87X-UD3H I7-4771 4 lõi @ 3.50GHz w/ 16g RAM @ 1600MHz
  • Hyperthreading được kích hoạt cho tổng số 8 lõi ảo
  • Toshiba thnsnhh256gbst SSD
  • Linux Debian 3.16.0-4-AMD64 #1 SMP Debian 3.16.7-CKT9-2 (2015-04-13) x86_64 GNU/Linux
  • MySQL 5.6.24
  • NGINX-1.6.2 + PHP-FPM cho tất cả các bài kiểm tra trừ khi có quy định khác
  • Mạng 100Mbps cục bộ yên tĩnh
  • Công cụ điểm chuẩn Siege chạy từ một máy riêng biệt

ZenCart 1.5.4

Moodle 2.9-Dev

Bộ đệm

TraQ 3.5.2

Geeklog 2.1.0

Tủ quần áo CMS 1.2.0

OpenCart 2.0.2.0

MediaWiki 1.24.1

PHPBB 3.1.3

WordPress 4.1.1

Drupal 8

Từ kết quả ở trên, bạn có thể thấy rằng chúng ta có thể thực hiện gấp đôi số lượng yêu cầu trong thời gian ít hơn trong Php 7 so với Php 5.

Những thông số kỹ thuật này có thể được tìm thấy trong cuộc nói chuyện

<?php

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

require __DIR__ . '/dotenv-loader.php';

use Auth0\SDK\API\Authentication;

$domain        = getenv('AUTH0_DOMAIN');
$client_id     = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri  = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
  'persist_id_token' => true,
  'persist_refresh_token' => true,
]);

$authorizationURL = sprintf(
    "https://%s/authorize?client_id=%s&response_type=code&redirect_uri=%s",
    $domain,
    $client_id,
    $redirect_uri);

$starWarsNames = ['Darth Vader', 'Ahsoka Tano', 'Kylo Ren', 'Obi-Wan Kenobi', 'R2-D2', 'Snoke'];

$userInfo = $auth0Oauth->getUser();

if (isset($_REQUEST['logout'])) {
    $auth0Oauth->logout();
    session_destroy();
    header("Location: /");
}

?>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript"></script>

        <script type="text/javascript" src="//use.typekit.net/iws6ohy.js"></script>
        <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">

        <!-- font awesome from BootstrapCDN -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <link href="public/app.css" rel="stylesheet">



    </head>
    <body class="home">
        <div class="container">
            <div class="login-page clearfix">
              <?php if(!$userInfo): ?>
              <div class="login-box auth0-box before">
                <img src="https://images.ctfassets.net/23aumh6u8s0i/3HNmqlr6oTNkMBE2dhodOU/0866266d4f67c2dcca42fd60f5d019aa/star_warsapp" />
                <p>Heard you don't want to migrate to PHP 7? Dare us!</p>
                <a href="<?php echo $authorizationURL ?>" class="btn btn-primary btn-login">SignIn</a>
              </div>
              <?php else: ?>
              <div class="logged-in-box auth0-box logged-in">
                <h2 id="logo">Star Wars Welcomes You to the Family!</h2>
                <img class="avatar" width="200" src="<?php echo $userInfo['picture'] ?/>"/>

                <h2>Welcome <span class="nickname"><?php echo $userInfo['nickname'] ?></span></h2>
                <h2> Assigned Codename : <b><?php echo $starWarsNames[rand(0, 6)]; ?></b> </h2>
                <a class="btn btn-primary btn-lg" href="?logout">Logout</a>
              </div>
              <?php endif ?>
            </div>
        </div>
    </body>
</html>
8 mà anh ấy đã đưa ra tại Fluent Conf, 2015.

Kiểm tra các điểm chuẩn sau:

  • php7-benchmarks
  • Php7 Phiên bản cuối cùng vs tiêu chuẩn HHVM
  • HHVM vs Performance Hiển thị Down - WordPress, Nginx

Sự kết luận

Chúng tôi đã bao gồm thành công cách nâng cấp môi trường máy chủ và phát triển của bạn từ Php 5 lên Php 7, trải qua các tính năng cung cấp Php 7 và cũng đã di chuyển hai ứng dụng từ Php 5 sang Php 7.

Woot! Woot! Đó là một hành trình làm nổi bật tất cả mọi thứ Php 7 cung cấp. PHP đã phát triển rất nhiều trong những năm qua từ ngôn ngữ đồ chơi đến ngôn ngữ nhanh chóng và doanh nghiệp.

Tài liệu hướng dẫn sử dụng PHP và RFC vẫn là tài liệu tham khảo đầy đủ nhất cho bất kỳ tính năng Php 7 mới nào. Bạn luôn có thể tận dụng chúng để biết thêm thông tin.

Mã PHP5 có chạy trên PHP7 không?

Đối với hầu hết các phần, mã PHP 5.x có thể chạy không thay đổi trên PHP 7. Tuy nhiên, có một vài thay đổi, được phân loại là không tương thích ngược.PHP 5. x code can run unchanged on PHP 7. There are a few changes, however, that are classified as backwards incompatible.

Php 8 có tương thích ngược không?

Php 8 có tương thích ngược không?Trả lời: Các tính năng mới của PHP 8 cũng giới thiệu chúng tôi với các công đoàn trong PHP 8. Bạn có thể tìm hiểu thêm về điều này trong điểm chuẩn PHP.Điều này có nghĩa là có một vấn đề với khả năng tương thích ngược nếu bạn thực hiện các loại Liên minh Php 8.0, vì điều này sẽ bị phá vỡ trên các trang web chạy PHP 7.4 trở xuống.there is an issue with backwards compatibility if you implement PHP 8.0 union types, as this will break on sites running PHP 7.4 or below.

Sự khác biệt giữa PHP7 và PHP 8 là gì?

Php 8 mang đến một loạt các tính năng mới cải tiến, chức năng và khấu hao cho ngôn ngữ so với PHP 7. Trong số tất cả các tính năng mới này, trình biên dịch JIT là người chia sẻ ánh đèn sân khấu.. Among all of these new features, the JIT compiler is the one sharing the limelight.

Làm cách nào để nâng cấp Php 5.6 lên 7?

Tải xuống lần đầu tiên phiên bản PHP mới nhất từ http://windows.php.net/doad#php-7.0.Tải xuống luồng VC14 x86 an toàn hoặc chủ đề VC14 x64 an toàn quyết định trên phiên bản Windows của bạn (x86 cho 32 bit và x64 cho 64 bit).Tiếp theo trích xuất tệp zip trong một thư mục có tên PHP.