Xây dựng trang web bằng php để đọc và ghi tệp trên máy chủ

Nếu bạn đang tìm cách kiểm tra cấu hình PHP của trang web, còn được gọi là phpinfo, bạn đã đến đúng nơi. Ví dụ: bạn có thể muốn kiểm tra xem cấu hình PHP của mình có đáp ứng yêu cầu của bất kỳ phần mềm nào bạn muốn chạy trên máy chủ của mình không

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo tệp phpinfo và cả cách kiểm tra thông tin chi tiết của tệp đó bằng các công cụ được cung cấp trong bảng điều khiển lưu trữ của bạn. Vì vậy, hay thực hiện ngay bây giơ

//httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted

Mặc định khi bạn cài xampp thì




My First Website



Hello, World!





3 sẽ trỏ về



My First Website



Hello, World!





5, nghĩa là nó sẽ phục vụ các file trong thư mục



My First Website



Hello, World!





6

Hãy tiếp tục và tạo một thư mục mới và đặt tên cho nó theo ý muốn của bạn và sửa đổi




My First Website



Hello, World!





3 như vậy



My First Website



Hello, World!





8

Bạn luôn có thể thay đổi đường dẫn




My First Website



Hello, World!





3 thành bất cứ đâu bạn muốn. Theo ý kiến ​​​​của tôi, đây chỉ là một cách sạch hơn nhiều để nhóm các dự án PHP của bạn

Thời gian để bắt đầu viết mã

Trước khi bắt tay vào viết mã, bạn cần có hai thứ

  1. Nhận và IDE của sự lựa chọn của bạn. [Visual Studio Code, Atom, Eclipse hoặc có lẽ là Notepad++]
  2. Đảm bảo rằng bạn đã khởi động Apache từ xampp. Nếu bạn đang sử dụng phiên bản 8 mới nhất trở lên, hãy truy cập vào



    My First Website



    Hello, World!





    20 để bắt đầu khởi động máy chủ

Bây giờ, hãy truy cập vào




My First Website



Hello, World!





21 mà bạn đã tạo trong thư mục



My First Website



Hello, World!





6

Tạo một tệp mới và đặt tên là




My First Website



Hello, World!





23. Mở nó lên và dán như sau




My First Website



Hello, World!





Tất nhiên, chúng ta cần bắt đầu với




My First Website



Hello, World!





24 truyền thống cho trang đầu tiên của bạn. Lưu tệp và nhớ khởi động Apache của bạn với xampp. Mở trình duyệt của bạn và truy cập



My First Website



Hello, World!





25. Bạn sẽ thấy truyền thống trong lập trình. Chào thế giới

mục lục. php

Vì vậy, bây giờ chúng ta hãy đi xa hơn

Bố cục cơ bản của các trang web

Bất kỳ trang web chung nào thường bao gồm ít nhất 3 phần riêng biệt được gọi là




My First Website



Hello, World!





26 ,



My First Website



Hello, World!





27 và



My First Website



Hello, World!





28

Bây giờ bạn có thể đoán rằng




My First Website



Hello, World!





26 là phần của trang nơi nó hiển thị logo của trang web, menu điều hướng, tìm kiếm, hồ sơ người dùng, v.v.




My First Website



Hello, World!





27 là phần dưới cùng của trang web nơi chúng tôi thường hiển thị những thứ linh tinh như liên kết mạng xã hội, menu liên hệ với chúng tôi, quyền riêng tư & chính sách cùng những thứ khác

Phần




My First Website



Hello, World!





28 là tốt, bây giờ có lẽ bạn đã biết rằng phần



My First Website



Hello, World!





28 là phần giữa



My First Website



Hello, World!





26 và



My First Website



Hello, World!





27

Có những thứ khác mà bạn có thể thấy trên một trang web như thanh bên rất phổ biến đối với các trang quản trị và tiện ích trang web

Tạo một trang có Header, Main và Footer

Mở trang




My First Website



Hello, World!





23 của bạn và dán nội dung sau




My First Website



Hello, World!





2

Tải lại trang và bạn sẽ thấy đầu trang và chân trang cơ bản

trang chủ

Bây giờ hãy coi bạn




My First Website



Hello, World!





23 là trang chủ của bạn. Bạn có thể thấy có 3 menu trong thanh điều hướng của mình ở phần trên cùng của trang

Rất tiếc, hiện chỉ có menu




My First Website



Hello, World!





17 hoạt động, 2 menu còn lại thì không

Điều này là do trang cho menu tương ứng chưa tồn tại

Hãy tiếp tục tạo hai trang này

Tạo thêm trang

Trong PHP, một trang web về cơ bản là một tệp




My First Website



Hello, World!





18. Nếu bạn là người mới bắt đầu và nếu điều này làm bạn bối rối, hãy để tôi giải thích

Khi bạn mở trình duyệt của mình và nhập ví dụ:




My First Website



Hello, World!





19 và sau đó bạn nhấn enter, điều xảy ra ở đây là bạn thực sự đang gửi yêu cầu đến máy chủ, máy chủ sẽ đọc, diễn giải yêu cầu và trả lại phản hồi. Trong trường hợp này, máy chủ sẽ phản hồi lại bằng cách gửi tệp



My First Website



Hello, World!





40, tệp này sau đó hiển thị trên trình duyệt của bạn dưới dạng trang web. Khá dễ dàng phải không?

Hãy tiếp tục và tạo hai tệp mới,




My First Website



Hello, World!





40 và



My First Website



Hello, World!





42

Sao chép mã bên dưới bắt đầu bằng




My First Website



Hello, World!





40 và



My First Website



Hello, World!





42




My First Website



Hello, World!





1




My First Website



Hello, World!





42




My First Website



Hello, World!





4

Bây giờ, hãy thử menu điều hướng, nó sẽ hiển thị trang tương ứng khi nhấp vào

Trang động với PHP

Cho đến nay, tất cả những gì chúng tôi đã làm chỉ là viết một số HTML thực sự cơ bản

Vâng, đây là phần tốt

Lưu ý rằng trong cả ba tệp mà chúng tôi đã tạo cho đến nay, tất cả




My First Website



Hello, World!





46 đều có cùng HTML ngoại trừ



My First Website



Hello, World!





47 hiển thị tên trang tương ứng

Hãy tưởng tượng nếu chúng ta có nhiều trang và sau đó chúng ta cần thay đổi chẳng hạn như




My First Website



Hello, World!





48 hoặc có thể thêm nhiều hơn nữa



My First Website



Hello, World!





49 hoặc



My First Website



Hello, World!





60 trong phần



My First Website



Hello, World!





61 của trang. Sau đó, bạn sẽ phải đi đến từng trang để thực hiện các thay đổi

Có vẻ khá nan giải phải không?

Đây là nơi PHP phát huy tác dụng. Chúng tôi có thể nhóm hoặc tách phần tương tự trong mỗi trang và cô lập nó trong một tệp duy nhất. Sau đó, chúng tôi có thể sử dụng PHP để đưa nó vào bất kỳ trang nào chúng tôi muốn

Tạo hai tệp mới có tên là




My First Website



Hello, World!





62 và



My First Website



Hello, World!





63

Mở




My First Website



Hello, World!





62 và dán như sau




My First Website



Hello, World!





6

Sau đó, mở




My First Website



Hello, World!





63 và dán thông tin sau

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
3

Hiện tại,




My First Website



Hello, World!





23 của bạn chỉ nên chứa nội dung này

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
5

Nếu bạn vào trang chủ của mình, bạn sẽ chỉ thấy dòng chữ




My First Website



Hello, World!





67. Thanh điều hướng và chân trang hiện đã biến mất

Quay lại




My First Website



Hello, World!





23 và dán thông tin sau

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
8

Paste ở trên vào 2 file còn lại và nhớ thay _




My First Website



Hello, World!





47

Đoạn mã trên chứa hàm PHP là

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
30 thực sự lấy tệp mà bạn chỉ định. Mọi nội dung của tệp mà bạn
DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
31 sau đó sẽ có sẵn trong tệp mà bạn gọi hàm

Khá gọn gàng phải không?

Được rồi, tiếp tục. Hãy xem chúng ta có thể làm gì khác với PHP trong phát triển web

Cơ sở dữ liệu trong phát triển web

Hầu hết mọi trang web trên internet đều có một loại cơ sở dữ liệu mà nó phụ thuộc vào để lưu dữ liệu

Ví dụ: khi bạn đăng nhập vào một trang web, nó sẽ lưu dữ liệu tên người dùng và mật khẩu đăng nhập của bạn

Một ví dụ khác như khi bạn đang duyệt qua một trang web thương mại điện tử và tìm thấy một sản phẩm bạn thích, khi nhấp vào nút, nó sẽ lưu sản phẩm đó vào giỏ hàng của bạn, điều này cũng liên quan đến việc lưu sản phẩm đó vào cơ sở dữ liệu. Vì vậy, lần sau khi bạn mở trang web, bạn vẫn có thể xem sản phẩm trong giỏ hàng của mình

Với ý nghĩ đó, hãy xem cách chúng ta có thể sử dụng PHP để kết nối với cơ sở dữ liệu

Trước khi làm điều đó, trước tiên hãy tạo cơ sở dữ liệu của chúng tôi trong MySQL

Khi bạn cài đặt XAMPP, nó cũng đi kèm với MySQL, điều này thực sự tuyệt vời

Chuyển đến

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
32 và nhấp vào nút
DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
33 ở phía bên trái của trang

phpmyadmin

Nhập tên cơ sở dữ liệu mà bạn muốn tạo và nhấp vào

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
34

tạo db

Hãy nhớ tên cơ sở dữ liệu của bạn vì chúng tôi sẽ cần đến nó sau này

Khi tạo thành công, nhấp vào cơ sở dữ liệu mà bạn vừa tạo

tạo bảng

Tạo một bảng

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
35. Nhấp vào nút
DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
36

bảng người dùng

Chỉ cần làm theo cấu hình trên để tạo bảng

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
35. Nhấp vào
DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
38

Bạn sẽ thấy một cái gì đó như thế này sau khi tạo bảng thành công

Bây giờ việc cần làm là tạo một vài bảng dữ liệu hoặc bản ghi

Nhấp vào

DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
39 ở phần trên cùng bên cạnh
DocumentRoot "C:/xampp/htdocs"

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# //httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
50

chèn dữ liệu

Bạn có thể tạo bao nhiêu bản ghi tùy thích nếu muốn. Bây giờ chúng ta đã hoàn thành với cơ sở dữ liệu

Kết nối cơ sở dữ liệu với PHP

Quay trở lại




My First Website



Hello, World!





23 của bạn và dán thông tin sau




My First Website



Hello, World!





2

Nhớ thay thế tên cơ sở dữ liệu trong mã

Làm mới trang và bạn sẽ có một cái gì đó như thế này

trang chủThe End…

Trên đây chỉ là những thứ cơ bản trong phát triển web với PHP và nếu bạn chưa quen với điều này, tôi thực sự khuyên bạn nên dành thời gian để hiểu tất cả những điều trên, đặc biệt là phần động và phần cơ sở dữ liệu

Chỉ là một mẹo nhỏ, nếu bạn là người mới, sau phần này, tôi khuyên bạn nên xem xét CRUD với PHP. Nếu bạn đang tìm kiếm một thử thách, thì có lẽ bạn có thể thử tạo một hệ thống đăng nhập bằng PHP. Hãy xem tại đây, Đăng nhập bằng PHP với MySQL

Làm cách nào để tạo một trang web bằng PHP?

Đọc tiếp để tìm hiểu cách tạo một trang web PHP theo từng bước. .
Tạo một trang web PHP. tiêu đề. Để tạo một trang web bằng PHP, bạn sẽ cần xây dựng ba trang web. .
Đặt nội dung vào phần thân trang web PHP của bạn. .
Mã trang web PHP đơn giản cho chân trang. .
Kết hợp trang web PHP đơn giản của bạn với nhau

Làm cách nào để chạy tệp PHP trên máy chủ?

php” được đặt trong thư mục “htdocs”. Nếu bạn muốn chạy nó, hãy mở bất kỳ trình duyệt web nào và nhập “localhost/demo. php” và nhấn enter . Chương trình của bạn sẽ chạy.

Làm cách nào để ghi vào tệp văn bản bằng PHP?

PHP Ghi vào Tệp - fwrite[] . Tham số đầu tiên của fwrite[] chứa tên của tệp cần ghi và tham số thứ hai là chuỗi được ghi.

Bạn có thể sử dụng chức năng nào trong PHP để mở tệp để đọc hoặc ghi hoặc cả hai?

Tệp mở PHP - fopen[] . Hàm này cung cấp cho bạn nhiều tùy chọn hơn hàm readfile[].

Chủ Đề