Thiết lập dự án PHP

Chỉ cần tạo một số tệp .php, đặt chúng vào thư mục web của bạn và máy chủ sẽ tự động phân tích chúng cho bạn

Bạn không cần phải biên dịch bất cứ thứ gì hoặc cài đặt bất kỳ công cụ bổ sung nào

Vì PHP miễn phí nên hầu hết các máy chủ web đều hỗ trợ PHP


Thiết lập PHP trên PC của riêng bạn

Tuy nhiên, nếu máy chủ của bạn không hỗ trợ PHP, bạn phải

  • cài đặt một máy chủ web
  • cài đặt PHP
  • cài đặt cơ sở dữ liệu, chẳng hạn như MySQL

Trang web PHP chính thức (PHP. net) có hướng dẫn cài đặt cho PHP. http. //php. net/thủ công/vi/cài đặt. php

Hướng dẫn PHP từng bước cho người mới bắt đầu - Tạo chương trình PHP của bạn TỪ ĐẦU. Các chức năng xác thực, tư cách thành viên và CRUD cơ bản

Thiết lập dự án PHP

Kristian Guevara

Đánh giá tôi

Thiết lập dự án PHP

Thiết lập dự án PHP

Thiết lập dự án PHP

Thiết lập dự án PHP

Thiết lập dự án PHP

Thiết lập dự án PHP

Vui lòng Đăng ký hoặc đăng nhập để bình chọn

4. 80/5 (92 phiếu)

26 tháng 1 năm 2021 CPOL 22 phút đọc

Hướng dẫn PHP cho người mới bắt đầu

Đây là một hướng dẫn chi tiết về phát triển web back-end bằng PHP. Tôi sẽ dạy các chức năng CRUD cơ bản, xác thực và bảo mật không phải là một hệ thống thành viên. Những việc như, thiết lập máy chủ của bạn, tạo Trang HTML công khai, tạo cơ sở dữ liệu và các bảng của nó cũng như thêm người dùng vào cơ sở dữ liệu

Giới thiệu

Quan tâm đến việc tạo trang web của riêng bạn? . Nếu bạn chưa quen với việc phát triển web back-end, hướng dẫn này là dành cho bạn. Tôi sẽ giải thích mọi thứ một cách chi tiết để bạn không phải nghiên cứu một số phương pháp cụ thể đang được sử dụng. Chúng tôi sẽ không sử dụng bất kỳ khuôn khổ nào để giữ mọi thứ đơn giản. Ngoài ra, tôi sẽ không tập trung vào thiết kế trang web vì sau các chức năng, thật dễ dàng để triển khai thiết kế. Những gì sẽ làm là một danh sách mục đơn giản khi người dùng đăng nhập

*Phích cắm không biết xấu hổ*

Bạn cũng có thể kiểm tra ASP của tôi. NET MVC 5 từ phiên bản đầu ngay tại đây

Tôi cũng có phiên bản NodeJS/Express với MongoDB ngay tại đây

điều kiện tiên quyết

Trong hướng dẫn này, chúng tôi sẽ sử dụng một số ngôn ngữ sẽ nâng cao sự phát triển của chúng tôi. Nếu bạn thực sự không biết bất kỳ điều gì tôi sẽ đề cập, không sao cả. Chỉ cần cố gắng để đi với dòng chảy. Thật dễ dàng để hiểu chúng vì tôi không sử dụng các phương pháp nâng cao cho phương pháp này. Tất nhiên vì đây là tạo dịch vụ phụ trợ cho một trang web, nên ít nhất bạn phải biết những điều cơ bản sau đây

  1. HTML/CSS - Tất nhiên điều này sẽ không bao giờ nằm ​​ngoài danh sách khi phát triển web. Tại sao tôi thậm chí còn bận tâm nơi này?
  2. JavaScript - Đối với tập lệnh chuyển hướng. Hiện tại không cần jQuery
  3. mySQL - ngôn ngữ cơ bản nhất để truy vấn. Chúng tôi sẽ sử dụng điều này cho các chức năng CRUD (Tạo, Đọc, Cập nhật, Xóa) của chúng tôi

Đối với phần mềm, bạn sẽ cần những điều sau đây

  1. Trình soạn thảo văn bản - Sublime Text/Notepad ++ sẽ làm. Điều này sẽ phục vụ như môi trường mã hóa của chúng tôi
  2. XAMPP - Đây sẽ là máy chủ web của chúng tôi. Đây là nơi chúng tôi sẽ lưu các tệp trang web của mình và lưu dữ liệu của chúng tôi vào cơ sở dữ liệu
  3. Trình duyệt web - Bạn có thể sử dụng bất kỳ trình duyệt nào miễn là nó không ngốn nhiều RAM của bạn

Đảm bảo đã cài đặt ba thứ đó. Không tiếp tục nếu bạn chưa cài đặt xong chúng. Tôi đã cung cấp một siêu liên kết để làm cho mọi thứ nhanh hơn

Đối với chương trình nghị sự của chúng tôi trong chủ đề này

  1. Sử dụng câu lệnh CRUD trong SQL - Tạo, đọc, cập nhật và xóa bản ghi trong cơ sở dữ liệu
  2. Xác thực và bảo mật đơn giản - Đăng nhập và đăng xuất, xử lý người dùng trái phép và hạn chế quyền truy cập
  3. Thao tác thời gian và ngày đơn giản - Hiển thị thời gian và ngày đăng và theo dõi bài đăng của bạn
  4. Hiển thị dữ liệu công khai và riêng tư - Chỉ hiển thị thông tin cho người dùng đã đăng nhập và cho công chúng
  5. Tóm tắt và Tóm tắt - Kết quả cuối cùng và một số lưu ý. Câu hỏi thường gặp cũng được đặt ở đây

Nội dung chủ đề

Ghi chú. Trong trường hợp bạn đã dừng ở một đoạn nào đó hoặc muốn tìm kiếm nhanh, bạn chỉ cần tiếp tục bằng cách nhấn Ctrl + f và tìm số mà bạn đã dừng lần cuối

  1. Thiết lập máy chủ của bạn
  2. Tạo các trang HTML công khai
  3. Tạo cơ sở dữ liệu và các bảng của nó
  4. Thêm người dùng vào cơ sở dữ liệu
  5. Đăng nhập người dùng. xác thực
  6. Thiết lập trang chủ cho người dùng Đăng nhập và Đăng xuất
  7. Kiểm tra bảo mật trang
  8. Thêm dữ liệu vào danh sách - User Access Only
  9. Hiển thị dữ liệu ở trang chủ
  10. Chỉnh sửa dữ liệu
  11. xóa dữ liệu
  12. Hiển thị dữ liệu công khai
  13. Tóm tắt và tóm tắt

Hãy chắc chắn rằng bạn làm theo trình tự thời gian. Đừng tiếp tục khi chưa hoàn thành phần trước, trừ khi bạn biết mã

Một số lời nhắc trước khi chúng ta bắt đầu

Ghi chú. Nếu hình ảnh có vẻ không rõ/mờ, bạn có thể nhấp vào hình ảnh đó để xem ở kích thước lớn hơn. Ngoài ra, thứ lỗi cho tôi vì đã sử dụng hình ảnh để hiển thị mã thay vì hiển thị cho bạn văn bản thô vì nó không thể định dạng chính xác trên đăng ký WordPress của tôi nhưng đừng lo, tôi sẽ tải sản phẩm hoàn thiện lên ở cuối hướng dẫn này lên

1) Thiết lập máy chủ của bạn

Bây giờ bạn đã sẵn sàng và bắt đầu, khi những kiến ​​thức cơ bản về lập trình bắt đầu, hãy bắt đầu bằng cách tạo một tiếng hét đơn giản về "xin chào thế giới" trong máy chủ của chúng ta

Trước tiên, hãy vào thư mục mà bạn đã cài đặt XAMPP (Thường gặp trong C. \xampp). Từ đó, chuyển đến thư mục htdocs (Thường có trong C. \xampp\htdocs) và tạo một thư mục có tên "MyFirstWebsite"

Thiết lập dự án PHP

Từ phần đó, bây giờ bạn đã tạo một URL cục bộ cho trang web của mình. Thư mục đó sẽ được sử dụng để cung cấp trong tất cả các tệp trang web (. html,. php,. css,. js, v.v. ). Mở trình soạn thảo văn bản của bạn và bắt đầu

Tôi sử dụng văn bản tuyệt vời làm trình soạn thảo văn bản của mình. Nếu bạn dùng Notepad++ hay bất kỳ cái nào khác thì không sao. Nó không thực sự là một yếu tố lớn mà nó chỉ là một sở thích mà bạn muốn sử dụng

Những gì chúng tôi sẽ làm là một trang HTML cơ bản và hiển thị "hello world" từ máy chủ bằng cú pháp PHP cơ bản. Sau đó chúng tôi gõ cú pháp sau

PHP

<html>
    <head>
    <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> </body> </html>

Lưu tệp vào Thư mục "MyFirstWebSite" và đặt tên là "chỉ mục. php". (Thư mục như đã thấy ở phần trên cùng của hình ảnh. )

Bây giờ bạn đã có tệp. Bây giờ hãy mở bảng điều khiển XAMPP của bạn. Trong trường hợp nó không xuất hiện trên màn hình của bạn, thì nó nằm trong thư mục XAMPP của bạn như trong hình

Thiết lập dự án PHP

Thiết lập dự án PHP

Bây giờ nó đã ở đó, hãy chạy Apache và mySQL của bạn bằng cách nhấp vào nút "Bắt đầu" trên cột hành động. Bạn sẽ thấy (các) PID ngẫu nhiên và số cổng mặc định. Apache là tên của máy chủ web của chúng tôi, trong đó nó sẽ xử lý tất cả các tệp cũng như đóng vai trò liên lạc với trình duyệt web và MySQL là cơ sở dữ liệu của chúng tôi sẽ lưu trữ tất cả thông tin của chúng tôi

Mở trình duyệt web của bạn và trong thanh địa chỉ, gõ localhost. Bạn sẽ thấy menu XAMPP của mình

Nếu đây là lần đầu tiên bạn chạy nó, nó sẽ hỏi bạn thích ngôn ngữ nào hơn, chỉ cần chọn và nó sẽ dẫn bạn đến menu. Nếu bạn để ý thấy thư mục là localhost/xampp, thì đó là nơi trang mặc định dẫn bạn ngay cả khi bạn nhập localhost

Thiết lập dự án PHP

Nếu điều đó xuất hiện, điều đó có nghĩa là máy chủ XAMPP của bạn hiện đang chạy. Bây giờ hãy thử chạy trang web bạn vừa đặt. Thử gõ localhost/MyFirstWebsite. Nó phải giống như hình bên dưới

Thiết lập dự án PHP

Nếu bạn để ý rằng URL là MyFirstWebsite, nó được lấy từ thư mục htdocs và nó tự động đọc các tệp có tên là "chỉ mục"(Có thể là chỉ mục. html, chỉ mục. aspx, v.v. ), đóng vai trò là trang mặc định. Nhập localhost/MyfirstWebsite/index. php cũng giống như vậy. Bạn cũng có thể tạo tên tùy chỉnh của mình cho URL bằng cách đổi tên thư mục nhưng hãy chỉ sử dụng MyFirstWebsite

Note: If you don't have a file named index and you entered the URL, you will receive an error 404 for not having the file on the server. If you do have different files that are not named index., you have to specify the specific file name. Example: localhost/MyfirstWebsite/page.php.

2) Tạo các trang HTML công khai

Tiếp theo, hãy sửa đổi lại trang web của chúng tôi và thêm liên kết đăng ký nơi người dùng của chúng tôi có thể đăng ký cũng như trang Đăng nhập ngay sau khi đăng ký. Hãy sửa đổi trang chủ của chúng tôi với đoạn mã sau

mục lục. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>

Như bạn có thể thấy, chúng tôi chỉ thêm hai liên kết dành cho Đăng nhập và đăng ký. Trước tiên hãy tạo trang đăng ký

Đăng ký. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

Như bạn có thể thấy, đây chỉ là một biểu mẫu cơ bản nơi người dùng có thể nhập thông tin đăng nhập của mình. Bây giờ cho trang đăng nhập

Gợi ý. Chỉ cần sao chép-dán cùng một mã để làm cho mọi thứ nhanh hơn

đăng nhập. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Login Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Login"/>
        </form>
    </body>
</html>

Về cơ bản, nó chỉ là mã giống như từ sổ đăng ký. php nhưng những thay đổi được thực hiện là những thay đổi được gạch chân

Nhấn vào đây để đăng nhập hoàn chỉnh. mã php

Hãy thử chạy lại localhost/MyFirstWebsite và các trang của bạn sẽ trông như thế này

mục lục. php

Thiết lập dự án PHP

Đăng ký. php

Thiết lập dự án PHP

3) Tạo cơ sở dữ liệu và các bảng của nó

Bây giờ chúng tôi có trang cơ bản cho công chúng. Hãy tiếp tục với cơ sở dữ liệu. Đầu tiên, gõ localhost/phpmyadmin. Điều này sẽ dẫn bạn đến trang chủ phpmyadmin

máy chủ cục bộ/phpmyadmin

Thiết lập dự án PHP

Từ đó, chuyển đến tab Cơ sở dữ liệu nằm ở trên cùng, sau đó từ hộp văn bản ở giữa, nhập first_db rồi nhấp vào tạo. Chỉ cần để Collation như là

Thiết lập dự án PHP

Bạn vừa tạo thành công cơ sở dữ liệu đầu tiên của mình. Bây giờ, từ đó, hãy tạo một bảng trong đó chúng ta có thể đăng ký người dùng và hiển thị thông tin. Đầu tiên, nhấp vào

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
9 nằm ở bên trái và tạo một bảng có tên
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
0 với 3 cột, sau đó nhấp vào Đi

Thiết lập dự án PHP

Đối với cấu trúc của bảng, đảm bảo có các trường sau, sau đó nhấp vào lưu

  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    1. Tên cột - Loại - Độ dài - Thuộc tính Null - Thuộc tính khác
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    2 - INT - N/A - Not Null - Auto Increment
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    3 - varchar - 50 - Not null
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    4 - varchar - 50 - Not null

Để mọi thứ theo mặc định nếu không được chỉ định

Thiết lập dự án PHP

Ghi chú. Bạn cần cuộn sang phải cho

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
5. Tôi vừa chỉnh sửa ảnh cho vừa với trường
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
6

Tiếp theo, tạo một bảng khác có tên là

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
7 với 7 cột và cấu trúc của bảng

  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    2 - INT - N/A - Not
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    9 - Auto Increment
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    0 - văn bản - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    2 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    3 - 30 - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    5 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    6 - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    8 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    3 - 30 - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    1 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    6 - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    4 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    3 - 5 - Không phải
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Login Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Login"/>
            </form>
        </body>
    </html>
    1

Thiết lập dự án PHP

4) Thêm người dùng vào cơ sở dữ liệu

Bây giờ chúng tôi có bảng của chúng tôi. Hãy chuyển sang phần thú vị, đưa trang đăng ký của bạn vào hoạt động. Từ đăng ký của bạn. php, thêm phần sau vào bên dưới mã html

Đăng ký. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

php
if($_SERVER["REQUEST METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    echo "Username entered is: ". $username . "
"
; echo "Password entered is: ". $password; } ?>

Đây là lời giải thích cho mã

  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    7 - kiểm tra xem biểu mẫu có nhận được phương thức
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    8 hay không khi nhấp vào nút gửi. Phương thức
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    8 được tạo trong html từ phương thức
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    0. Bấm vào đây để tham khảo phương pháp biểu mẫu
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    1 - lấy tên từ phương thức
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    8. Điều này chỉ đơn giản là lấy đầu vào dựa trên tên từ biểu mẫu. Trong trường hợp của chúng tôi, đó là tên người dùng và mật khẩu
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    3 - đóng gói đầu vào vào một
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    4 để ngăn đầu vào khỏi SQL Injection. Điều này đảm bảo rằng các
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    4 của bạn không thoát khỏi các ký tự không cần thiết. Nhấn vào đây để tìm hiểu thêm về SQL Injections

Bây giờ hãy thử vào sổ đăng ký của bạn. php và cố gắng nhập bất cứ thứ gì, sau đó nhấp vào "______36_______6. " Trong trường hợp của tôi, tôi đã nhập tên người dùng xtian và mật khẩu là 123456. Nó sẽ hiển thị các đầu vào bên dưới. Đây là mẫu của tôi

Thiết lập dự án PHP

Về phần này, bạn nên hiểu cách lấy thông tin đầu vào từ biểu mẫu. Bây giờ để thêm nó vào cơ sở dữ liệu. Trên sổ đăng ký của bạn. php, thêm đoạn mã sau

Đăng ký. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="post">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter Password: <input type="password" 
                            name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>


php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;
   
    mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
    mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users"); //Query the users table
    while($row = mysql_fetch_array($query)) //display all rows from query
    {
        $table_users == $row['username']; // the first username row 
                                          // is passed on to $table_users, 
                                          // and so on until the query is finished
        if($username == $table_users)     // checks if there are any matching fields
        {
            $bool = false; // sets bool to false
            Print '';     //Prompts the user
            Print '";//redirects to 
                                                                             //register.php
        }
    }

    if($bool) // checks if bool is true
    {
        mysql_query("INSERT INTO users (username, password) _
                     VALUES ('$username', 'password')"); // inserts value into table users
        Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
        Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                          // register.php
    }
}
?>

Nhấn vào đây để đăng ký hoàn chỉnh. tập tin php

Dưới đây là những lời giải thích cho mã

  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    7 - Cú pháp được sử dụng để kết nối với máy chủ XAMPP của chúng tôi. máy chủ cục bộ hoặc 127. 0. 0. 1 là tên của máy chủ. Tên người dùng mặc định là root và không có mật khẩu cho mặc định
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    8 - Chọn cơ sở dữ liệu sẽ được sử dụng
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="post">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter Password: <input type="password" 
                                name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    
    php
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
       
        mysql_connect("localhost", "root","") or die(mysql_error());      //Connect to server
        mysql_select_db("first_db") or due("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users"); //Query the users table
        while($row = mysql_fetch_array($query)) //display all rows from query
        {
            $table_users == $row['username']; // the first username row 
                                              // is passed on to $table_users, 
                                              // and so on until the query is finished
            if($username == $table_users)     // checks if there are any matching fields
            {
                $bool = false; // sets bool to false
                Print '';     //Prompts the user
                Print '";//redirects to 
                                                                                 //register.php
            }
        }
    
        if($bool) // checks if bool is true
        {
            mysql_query("INSERT INTO users (username, password) _
                         VALUES ('$username', 'password')"); // inserts value into table users
            Print '<script>alert("Successfully Registered!");</script>';      // Prompts the user
            Print '<script>window.location.assign("register.php");</script>'; // redirects to 
                                                                              // register.php
        }
    }
    ?>
    9 - Hiển thị thông báo lỗi nếu điều kiện không được đáp ứng
  • 
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    0 - thực hiện các truy vấn SQL. Bấm vào đây để xem một số mẫu truy vấn SQL. Một lần nữa, tôi không ở đây để thảo luận về mySQL
  • 
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    1 - tìm nạp tất cả các truy vấn trong bảng để hiển thị hoặc thao tác dữ liệu. Nó được đặt trong một vòng lặp
    
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    2 để nó có thể truy vấn tất cả các hàng. Hãy lưu ý rằng chỉ có 1 hàng được truy vấn trên mỗi vòng lặp, đó là lý do tại sao vòng lặp while là cần thiết
  • 
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    3- giá trị của cột trong truy vấn hiện tại. Nó được biểu diễn dưới dạng một mảng. Trong trường hợp của chúng tôi,
    
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    4 là tên của biến cho hàng của chúng tôi trong vòng lặp

Hãy thử các đầu vào mà bạn đã thực hiện trước đó và xem điều gì sẽ xảy ra. Nó sẽ nhắc rằng bạn đã đăng ký thành công. Hãy thử đi tới


    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;

    mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                            // users table
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "":
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
       while($row = mysql_fetch_assoc($query)) // display all rows from query
       {
          $table_users = $row['username'];     // the first username row is 
                                               // passed on to $table_users, 
                                               // and so on until the query is finished
          $table_password = $row['password'];  // the first password row is passed on 
                                               // to $table_password, and so on 
                                               // until the query is finished
       }
       if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                  // are any matching fields
       {
          if($password == $table_password)
          {
             $_SESSION['user'] = $username;    // set the username in a session. 
                                               // This serves as a global variable
             header("location: home.php");     // redirects the user to the authenticated 
                                               // home page
          }
       }
       else
       {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
       }
    }
    else
    {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
    }
?>
5 và xem bảng
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
0 của bạn

Thiết lập dự án PHP

Xin chúc mừng. Bây giờ bạn đã biết cách thêm dữ liệu vào cơ sở dữ liệu bằng xác thực dữ liệu

5) Đăng nhập người dùng. xác thực

Bây giờ cho trang đăng nhập. Hãy tạo một tệp mới có tên checklogin. php. Lý do là quay lại đăng nhập của chúng tôi. php, biểu mẫu của chúng tôi có một hành động gọi là "checklogin. php", đặc biệt là


    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;

    mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                            // users table
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "":
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
       while($row = mysql_fetch_assoc($query)) // display all rows from query
       {
          $table_users = $row['username'];     // the first username row is 
                                               // passed on to $table_users, 
                                               // and so on until the query is finished
          $table_password = $row['password'];  // the first password row is passed on 
                                               // to $table_password, and so on 
                                               // until the query is finished
       }
       if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                  // are any matching fields
       {
          if($password == $table_password)
          {
             $_SESSION['user'] = $username;    // set the username in a session. 
                                               // This serves as a global variable
             header("location: home.php");     // redirects the user to the authenticated 
                                               // home page
          }
       }
       else
       {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
       }
    }
    else
    {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
    }
?>
7. Nếu bạn để ý trên sổ đăng ký. php, nó cũng được đăng ký. php vì back-end cũng được thực hiện trên cùng một tệp

Bây giờ hãy viết mã đăng nhập. php với cú pháp sau

đăng nhập. php

Thiết lập dự án PHP

PHP


    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;

    mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                            // users table
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "":
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
       while($row = mysql_fetch_assoc($query)) // display all rows from query
       {
          $table_users = $row['username'];     // the first username row is 
                                               // passed on to $table_users, 
                                               // and so on until the query is finished
          $table_password = $row['password'];  // the first password row is passed on 
                                               // to $table_password, and so on 
                                               // until the query is finished
       }
       if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                  // are any matching fields
       {
          if($password == $table_password)
          {
             $_SESSION['user'] = $username;    // set the username in a session. 
                                               // This serves as a global variable
             header("location: home.php");     // redirects the user to the authenticated 
                                               // home page
          }
       }
       else
       {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
       }
    }
    else
    {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
    }
?>

Nhấn vào đây để đăng nhập hoàn chỉnh. mã php

Đây là lời giải thích của mã. (Một số đã giải thích trước nên không cần nhắc lại)

  • 
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    8 - Bắt đầu phiên. Điều này thường được thực hiện trên các trang được xác thực. Lý do tại sao chúng tôi sử dụng điều này là vì điều này là cần thiết cho
    
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    9
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        php
        session_start(); //starts the session
        if($_SESSION['user']){ // checks if the user is logged in  
        }
        else{
            header("location: index.php"); // redirects if user is not logged in
        }
        $user = $_SESSION['user']; //assigns user value
        ?>
        <body>
            <h2>Home Page</h2>
            <p>Hello php Print "$user"?>!</p> 
            <a href="logout.php">Click here to go logout</a><br/><br/>
            <form action="add.php" method="POST">
                Add more to list: <input type="text" name="details" /> <br/>
                Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
                <input type="submit" value="Add to list"/>
            </form>
        	<h2 align="center">My list</h2>
        	<table border="1px" width="100%">
    		<tr>
    			<th>Id</th>
    			<th>Details</th>
    			<th>Edit</th>
    			<th>Delete</th>
    		</tr>
    		</table>
    	</body>
    </html>
    0 - Điều này trả về một số nguyên. Điều này đếm tất cả các hàng tùy thuộc vào truy vấn
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        php
        session_start(); //starts the session
        if($_SESSION['user']){ // checks if the user is logged in  
        }
        else{
            header("location: index.php"); // redirects if user is not logged in
        }
        $user = $_SESSION['user']; //assigns user value
        ?>
        <body>
            <h2>Home Page</h2>
            <p>Hello php Print "$user"?>!</p> 
            <a href="logout.php">Click here to go logout</a><br/><br/>
            <form action="add.php" method="POST">
                Add more to list: <input type="text" name="details" /> <br/>
                Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
                <input type="submit" value="Add to list"/>
            </form>
        	<h2 align="center">My list</h2>
        	<table border="1px" width="100%">
    		<tr>
    			<th>Id</th>
    			<th>Details</th>
    			<th>Edit</th>
    			<th>Delete</th>
    		</tr>
    		</table>
    	</body>
    </html>
    1 - Đóng vai trò là tên phiên cho toàn bộ phiên. Điều này tương đối giống với biến
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    4 trong lập trình hướng đối tượng. Chúng tôi sẽ sử dụng điều này để xác thực xem người dùng có được xác thực hay không

Bây giờ hãy thử kiểm tra đầu vào của bạn với tên người dùng và mật khẩu sai. Nó sẽ trả về lời nhắc mong muốn. Sau khi kiểm tra, hãy thử nhập các giá trị chính xác. Nó sẽ dẫn bạn về nhà. php

Ghi chú. Trang Chủ. php chưa tồn tại nên sẽ báo lỗi 404

6) Thiết lập Trang chủ cho Người dùng Đăng nhập và Đăng xuất

Bây giờ đã được xác thực, bây giờ hãy tạo trang chủ của chúng ta (home. php) với cú pháp sau

Trang Chủ. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    php
    session_start(); //starts the session
    if($_SESSION['user']){ // checks if the user is logged in  
    }
    else{
        header("location: index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello php Print "$user"?>!</p> 
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <form action="add.php" method="POST">
            Add more to list: <input type="text" name="details" /> <br/>
            Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
            <input type="submit" value="Add to list"/>
        </form>
    	<h2 align="center">My list</h2>
    	<table border="1px" width="100%">
		<tr>
			<th>Id</th>
			<th>Details</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		</table>
	</body>
</html>

Đây là lời giải thích cho mã

  • 
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    8 - Về cơ bản bắt đầu phiên. Cần thiết cho
    
        session_start();
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
        $bool = true;
    
        mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
        mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
        $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                                // users table
        $exists = mysql_num_rows($query); //Checks if username exists
        $table_users = "":
        $table_password = "";
        if($exists > 0) //IF there are no returning rows or no existing username
        {
           while($row = mysql_fetch_assoc($query)) // display all rows from query
           {
              $table_users = $row['username'];     // the first username row is 
                                                   // passed on to $table_users, 
                                                   // and so on until the query is finished
              $table_password = $row['password'];  // the first password row is passed on 
                                                   // to $table_password, and so on 
                                                   // until the query is finished
           }
           if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                      // are any matching fields
           {
              if($password == $table_password)
              {
                 $_SESSION['user'] = $username;    // set the username in a session. 
                                                   // This serves as a global variable
                 header("location: home.php");     // redirects the user to the authenticated 
                                                   // home page
              }
           }
           else
           {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
           }
        }
        else
        {
            Print '';        // Prompts the user
            Print ''; // redirects to login.php
        }
    ?>
    9
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        php
        session_start(); //starts the session
        if($_SESSION['user']){ // checks if the user is logged in  
        }
        else{
            header("location: index.php"); // redirects if user is not logged in
        }
        $user = $_SESSION['user']; //assigns user value
        ?>
        <body>
            <h2>Home Page</h2>
            <p>Hello php Print "$user"?>!</p> 
            <a href="logout.php">Click here to go logout</a><br/><br/>
            <form action="add.php" method="POST">
                Add more to list: <input type="text" name="details" /> <br/>
                Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
                <input type="submit" value="Add to list"/>
            </form>
        	<h2 align="center">My list</h2>
        	<table border="1px" width="100%">
    		<tr>
    			<th>Id</th>
    			<th>Details</th>
    			<th>Edit</th>
    			<th>Delete</th>
    		</tr>
    		</table>
    	</body>
    </html>
    5 - chuyển hướng người dùng

Hãy thử làm mới trình duyệt của bạn và nó sẽ trông như thế này

Thiết lập dự án PHP

Bây giờ chúng ta đã có nhà của mình, hãy thử tạo đăng xuất của chúng ta. php và kiểm tra xem phiên của người dùng có tắt không. Những gì chúng tôi sẽ làm là nếu người dùng đăng xuất, người dùng không nên truy cập trang chủ. php. Vì vậy, đây là cú pháp đơn giản để đăng xuất. php

đăng xuất. php

Thiết lập dự án PHP

PHP


    session_start();
    session_destroy();
    header("location:index.php");
?>

Cú pháp đơn giản.

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    php
    session_start(); //starts the session
    if($_SESSION['user']){ // checks if the user is logged in  
    }
    else{
        header("location: index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello php Print "$user"?>!</p> 
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <form action="add.php" method="POST">
            Add more to list: <input type="text" name="details" /> <br/>
            Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
            <input type="submit" value="Add to list"/>
        </form>
    	<h2 align="center">My list</h2>
    	<table border="1px" width="100%">
		<tr>
			<th>Id</th>
			<th>Details</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		</table>
	</body>
</html>
6 chỉ cần xóa tất cả ý nghĩa của phiên, giá trị của

    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;

    mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                            // users table
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "":
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
       while($row = mysql_fetch_assoc($query)) // display all rows from query
       {
          $table_users = $row['username'];     // the first username row is 
                                               // passed on to $table_users, 
                                               // and so on until the query is finished
          $table_password = $row['password'];  // the first password row is passed on 
                                               // to $table_password, and so on 
                                               // until the query is finished
       }
       if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                  // are any matching fields
       {
          if($password == $table_password)
          {
             $_SESSION['user'] = $username;    // set the username in a session. 
                                               // This serves as a global variable
             header("location: home.php");     // redirects the user to the authenticated 
                                               // home page
          }
       }
       else
       {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
       }
    }
    else
    {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
    }
?>
9 sẽ bị xóa và
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    php
    session_start(); //starts the session
    if($_SESSION['user']){ // checks if the user is logged in  
    }
    else{
        header("location: index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello php Print "$user"?>!</p> 
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <form action="add.php" method="POST">
            Add more to list: <input type="text" name="details" /> <br/>
            Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
            <input type="submit" value="Add to list"/>
        </form>
    	<h2 align="center">My list</h2>
    	<table border="1px" width="100%">
		<tr>
			<th>Id</th>
			<th>Details</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		</table>
	</body>
</html>
5 sẽ chỉ chuyển hướng nó đến trang chủ

7) Bảo mật trang thử nghiệm

Bây giờ hãy thử làm mới nhà. php và nhấp vào đăng xuất. Bây giờ hãy thử nhấp vào nút quay lại của trình duyệt của bạn và xem điều gì sẽ xảy ra

Thiết lập dự án PHP

Như bạn có thể thấy, nó không hướng bạn về nhà. php vì bạn đã đăng xuất. Bây giờ cho thử nghiệm thứ hai, hãy thử nhập thủ công địa chỉ localhost/MyFirstWebsite/home. php. Trường hợp tương tự cũng nên xảy ra. Vì đã đăng xuất, ngay cả khi nhập thủ công địa chỉ cũng không truy cập được trang được ủy quyền. Những gì chúng tôi đã làm là một cơ chế bảo mật đơn giản trong đó chúng tôi chuyển hướng những người dùng trái phép trở lại một trang công khai

Bây giờ hãy thử đăng nhập lại và chúng ta hãy quay lại trang chủ. php

8) Thêm dữ liệu vào danh sách - Chỉ dành cho người dùng truy cập

Trong bước tiếp theo, hãy tạo việc thêm các mục vào danh sách. Như bạn sẽ thấy từ biểu mẫu, nó được viết là

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    php
    session_start(); //starts the session
    if($_SESSION['user']){ // checks if the user is logged in  
    }
    else{
        header("location: index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello php Print "$user"?>!</p> 
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <form action="add.php" method="POST">
            Add more to list: <input type="text" name="details" /> <br/>
            Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
            <input type="submit" value="Add to list"/>
        </form>
    	<h2 align="center">My list</h2>
    	<table border="1px" width="100%">
		<tr>
			<th>Id</th>
			<th>Details</th>
			<th>Edit</th>
			<th>Delete</th>
		</tr>
		</table>
	</body>
</html>
9, có nghĩa là yêu cầu đăng http của chúng tôi sẽ thêm. php và cùng với đó, chúng tôi tạo phần bổ sung của mình. php với cú pháp sau

PHP


    session_start();
    if($_SESSION['user']){
    }
    else{ 
       header("location:index.php");
    }

    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X"); //time
    $date = strftime("%B %d, %Y"); //date

    Print "$time - $date - $details";
?>

Hãy lưu ý rằng đây không phải là bổ sung chính thức của chúng tôi. php chưa, tôi sẽ trình bày cú pháp ngày giờ và nhận thông tin đầu vào của bạn

Bây giờ hãy trở về nhà của bạn. php và thử thêm một mục, sau đó nhấp vào "Thêm vào danh sách"

Thiết lập dự án PHP

Đây phải là đầu ra sau khi thêm. php

Thiết lập dự án PHP

Như bạn có thể thấy, chúng tôi có ngày, giờ hiện tại và thông tin đầu vào của bạn. Đây là lời giải thích cho mã

  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    0 - nhận thời gian dựa trên định dạng bạn đã đặt
  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    1- giờ hệ thống hiện tại
  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    2 - tháng hệ thống hiện tại
  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    3 - ngày hệ thống hiện tại
  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    4 - năm hệ thống hiện tại

Bây giờ hãy sửa đổi phần bổ sung của chúng tôi. php và thêm dữ liệu sau vào cơ sở dữ liệu cùng với dữ liệu từ hộp kiểm

cộng. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
0

Nhấn vào đây để thêm hoàn chỉnh. mã php

Đây là một lời giải thích nhỏ

  • 
        session_start();
        session_destroy();
        header("location:index.php");
    ?>
    5 - nhận giá trị của hộp kiểm. Như bạn sẽ thấy, định dạng hộp kiểm trong biểu mẫu là name="checkbox[]". Để lấy dữ liệu từ hộp kiểm, nó phải được khởi tạo dưới dạng một mảng. Làm như vậy sẽ có thể lấy dữ liệu từ nhiều hộp kiểm

Bây giờ hãy thử nhập một số dữ liệu và nhấp vào "Thêm vào danh sách". Trong trường hợp của tôi, tôi sẽ chỉ sử dụng lại kết thúc. Hãy truy cập phpmyadmin của chúng tôi và xem dữ liệu đã được thêm chưa. Đây là kết quả của trường hợp của tôi

máy chủ cục bộ/phpmyadmin

Thiết lập dự án PHP

9) Hiển thị dữ liệu trong Trang chủ

Bây giờ chúng ta đã thấy rằng dữ liệu đã được thêm thành công, bây giờ hãy hiển thị dữ liệu trong trang chủ của chúng ta. Hãy sửa đổi nhà của chúng tôi. php và hãy thêm một số cột cho ngày

Trang Chủ. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
1

Giải thích cho mã được thêm vào khá đơn giản. Về cơ bản, nó chỉ hiển thị dữ liệu đến từ vòng lặp


    session_start();
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $bool = true;

    mysql_connect("localhost", "root", "") or die (mysql_error());     //Connect to server
    mysql_select_db("first_db") or die ("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from users WHERE username='$username'"); // Query the 
                                                                            // users table
    $exists = mysql_num_rows($query); //Checks if username exists
    $table_users = "":
    $table_password = "";
    if($exists > 0) //IF there are no returning rows or no existing username
    {
       while($row = mysql_fetch_assoc($query)) // display all rows from query
       {
          $table_users = $row['username'];     // the first username row is 
                                               // passed on to $table_users, 
                                               // and so on until the query is finished
          $table_password = $row['password'];  // the first password row is passed on 
                                               // to $table_password, and so on 
                                               // until the query is finished
       }
       if(($username == $table_users) && ($password == $table_password)) // checks if there 
                                                                  // are any matching fields
       {
          if($password == $table_password)
          {
             $_SESSION['user'] = $username;    // set the username in a session. 
                                               // This serves as a global variable
             header("location: home.php");     // redirects the user to the authenticated 
                                               // home page
          }
       }
       else
       {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
       }
    }
    else
    {
        Print '';        // Prompts the user
        Print ''; // redirects to login.php
    }
?>
2. Nó đã được giải thích trước đó trong hướng dẫn của chúng tôi nên tôi tin rằng đến thời điểm này, bạn đã hiểu quy trình lấy dữ liệu trong truy vấn. Quay lại trình duyệt, thử làm mới trang chủ của bạn. php và xem điều gì sẽ xảy ra

Thiết lập dự án PHP

Bây giờ nó sẽ hiển thị dữ liệu đó. Từ danh sách kiểm tra CRUD của chúng tôi, chúng tôi hiện đã hoàn thành


    session_start();
    session_destroy();
    header("location:index.php");
?>
7 và

    session_start();
    session_destroy();
    header("location:index.php");
?>
8. Tiếp theo là cập nhật (sửa) và xóa thông tin. Nếu bạn để ý, chúng tôi có các liên kết chỉnh sửa và xóa được hiển thị trên cột. Tôi sẽ thêm một dữ liệu khác vào danh sách có tên "

    session_start();
    session_destroy();
    header("location:index.php");
?>
9" để có một ví dụ khác và lần này, đó là quyền riêng tư đối với không

Thiết lập dự án PHP

10) Chỉnh sửa dữ liệu

Bây giờ, hãy thử chỉnh sửa dữ liệu của chúng ta và để làm điều đó, chúng ta sẽ sử dụng một chức năng mới gọi là "


    session_start();
    if($_SESSION['user']){
    }
    else{ 
       header("location:index.php");
    }

    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X"); //time
    $date = strftime("%B %d, %Y"); //date

    Print "$time - $date - $details";
?>
0". Với các phương pháp trước đây của chúng tôi, chúng tôi đã sử dụng
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

php
if($_SERVER["REQUEST METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    echo "Username entered is: ". $username . "
"
; echo "Password entered is: ". $password; } ?>
8 làm yêu cầu http nhưng lần này, hãy sử dụng

    session_start();
    if($_SESSION['user']){
    }
    else{ 
       header("location:index.php");
    }

    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X"); //time
    $date = strftime("%B %d, %Y"); //date

    Print "$time - $date - $details";
?>
0 để chỉnh sửa và xóa bản ghi. Để bắt đầu, hãy sửa đổi ngôi nhà của chúng ta. php và thêm một chút mã vào 2 cột

Trang Chủ. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
2

Nếu bạn để ý, chúng tôi chỉ thêm tham số URL cho các liên kết chỉnh sửa và xóa có tên là

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
2. Chúng tôi sẽ sử dụng điều này sau để xử lý dữ liệu của bạn. Lý do tại sao chúng tôi sử dụng
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
2 là vì nó là một mã định danh duy nhất. Người đó có thể nhập cùng một dữ liệu, vì vậy không nên sử dụng
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Login Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Login"/>
        </form>
    </body>
</html>
0 làm phương tiện để thao túng sau này

Hãy thử đặt con trỏ của bạn vào liên kết chỉnh sửa và bạn sẽ thấy giá trị của id ở phía dưới bên trái

Thiết lập dự án PHP

Bây giờ chúng ta đã có điều đó, hãy thử tạo bản chỉnh sửa của chúng ta. php và hãy thử lấy dữ liệu từ đó

chỉnh sửa. php

Dòng 1 - 42

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
3

Một số giải thích cho mã

  • 
        session_start();
        if($_SESSION['user']){
        }
        else{ 
           header("location:index.php");
        }
    
        $details = mysql_real_escape_string($_POST['details']);
        $time = strftime("%X"); //time
        $date = strftime("%B %d, %Y"); //date
    
        Print "$time - $date - $details";
    ?>
    6 - một phương thức kiểm tra xem giá trị có trống không. Có thể đảo ngược cú pháp nếu bạn muốn kiểm tra xem nó có trống không bằng cách xóa điểm giải thích (. ), do đó cú pháp của nó sẽ là
    
        session_start();
        if($_SESSION['user']){
        }
        else{ 
           header("location:index.php");
        }
    
        $details = mysql_real_escape_string($_POST['details']);
        $time = strftime("%X"); //time
        $date = strftime("%B %d, %Y"); //date
    
        Print "$time - $date - $details";
    ?>
    7
  • 
        session_start();
        if($_SESSION['user']){
        }
        else{ 
           header("location:index.php");
        }
    
        $details = mysql_real_escape_string($_POST['details']);
        $time = strftime("%X"); //time
        $date = strftime("%B %d, %Y"); //date
    
        Print "$time - $date - $details";
    ?>
    8 - Dùng để lấy giá trị từ tham số. Trong trường hợp của chúng tôi, chúng tôi sử dụng
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    2 làm tham số URL nên cú pháp sẽ là
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    00
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    01 - biến kiểm tra xem
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    2 đã cho có tồn tại hay không
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    03 - chúng tôi đặt giá trị của
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="register.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    2 vào phiên để sử dụng nó trên một tệp khác

Dòng 42 - 76

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
4

Lý do tại sao chúng tôi đặt biến

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
01 là vì trong trường hợp người dùng sửa đổi tham số URL thành một số không tồn tại (trong trường hợp của chúng tôi, chúng tôi chỉ có 2 hàng/id), chúng tôi có thể hiển thị lời nhắc trong đó dữ liệu không tồn tại

Bây giờ hãy thử nhấp vào liên kết chỉnh sửa ở hàng đầu tiên và nó sẽ hiển thị như thế này

Thiết lập dự án PHP

Hãy thử sửa đổi tham số URL bằng cách xóa

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
06 và bây giờ sẽ dẫn đến localhost/MyFirstWebsite/edit. php và kết quả sẽ như thế này

Thiết lập dự án PHP

Bây giờ hãy thử đặt một giá trị lớn hơn số

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="register.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>
2, trong trường hợp của chúng ta, hãy thử đặt 5 và kết quả sẽ như thế này

localhost/MyFirstWebsite/chỉnh sửa. php?id=5

Thiết lập dự án PHP

Bây giờ chúng tôi đã bảo mật các tham số URL của mình, bây giờ hãy đặt cú pháp chỉnh sửa. Hãy quay lại chỉnh sửa. php và thêm một số mã sau để cập nhật thông tin vào cơ sở dữ liệu

chỉnh sửa. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
5

Nhấn vào đây để chỉnh sửa hoàn chỉnh. mã php (Github)

Bây giờ hãy thử làm mới và quay lại trang chỉnh sửa. Hãy thử một dữ liệu khác. Trong trường hợp của tôi, tôi sẽ sử dụng "

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
08" và thay đổi nó thành non-
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

php
if($_SERVER["REQUEST METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    echo "Username entered is: ". $username . "
"
; echo "Password entered is: ". $password; } ?>
4

chỉnh sửa. php?id=1

Thiết lập dự án PHP

Hãy tiếp tục và nhấp vào Cập nhật danh sách và bạn sẽ được chuyển hướng đến trang chủ. php và xem danh sách cập nhật

Thiết lập dự án PHP

Bây giờ, chúng tôi có thời gian và ngày chỉnh sửa được hiển thị trên cột Thời gian chỉnh sửa. Quyền riêng tư của chúng tôi hiện được đặt thành không công khai và giá trị đã thay đổi thành Cá hồi

Xin chúc mừng. Bây giờ chúng tôi đã thực hiện chức năng chỉnh sửa

11) Xóa dữ liệu

Nhìn lại CRUD, chúng ta đã hoàn thành việc tạo (thêm), đọc (hiển thị) và cập nhật (chỉnh sửa) bản ghi. Bây giờ là phần cuối cùng, xóa bản ghi. Đối với phần này, nó chỉ tương đối giống với những gì chúng tôi đã thực hiện khi chỉnh sửa nhưng điều khác biệt chỉ là câu lệnh SQL. Thay vì sử dụng

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
10, chúng tôi sẽ sử dụng cú pháp
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
11. Khi xóa bản ghi, chúng tôi phải nhắc mọi người đảm bảo rằng họ thực sự muốn xóa bản ghi, vì vậy chúng tôi sẽ thêm một chút JavaScript vào trang chủ. php. Để làm điều đó, hãy sửa đổi mã của chúng tôi và thêm một số vào home. php

Trang Chủ. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
6

Bấm vào đây để hoàn thành ngôi nhà. mã php

Như bạn đã nhận thấy, chúng tôi đã chỉnh sửa liên kết để xóa. Chúng tôi đã thay đổi

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
12 thành "_______1_______13" và thêm và hàm
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
14 cho JavaScript cho phương thức của
<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
15 và bên trong tham số của nó là id của hàng. Bên dưới bảng được viết là cú pháp JavaScript trong đó nó nhắc người dùng nếu họ muốn xóa bản ghi. Nếu người dùng xác nhận, trang sẽ hướng dẫn xóa. php được nhúng cùng với giá trị của id. Bây giờ hãy tạo xóa. php và đây là cú pháp sau

xóa bỏ. php

Thiết lập dự án PHP

PHP

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
7

Nhấn vào đây để xóa hoàn toàn. mã php

Mã này rất đơn giản và cú pháp cũng giống như những gì chúng tôi đã sử dụng trước đây nhưng nhận thấy rằng chúng tôi đã thay đổi phương thức yêu cầu của mình thành


    session_start();
    if($_SESSION['user']){
    }
    else{ 
       header("location:index.php");
    }

    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X"); //time
    $date = strftime("%B %d, %Y"); //date

    Print "$time - $date - $details";
?>
0. Chúng tôi hiện đang sử dụng yêu cầu

    session_start();
    if($_SESSION['user']){
    }
    else{ 
       header("location:index.php");
    }

    $details = mysql_real_escape_string($_POST['details']);
    $time = strftime("%X"); //time
    $date = strftime("%B %d, %Y"); //date

    Print "$time - $date - $details";
?>
0 vì chúng tôi có tham số URL. Bây giờ hãy thử làm mới nhà. php và hãy thử xóa bản ghi đầu tiên. Đây phải là kết quả

nhắc nhở

Thiết lập dự án PHP

Kết quả cuối cùng

Thiết lập dự án PHP

Xin chúc mừng. Bây giờ chúng tôi đã chính thức hoàn thành các câu lệnh CRUD của mình

12) Hiển thị dữ liệu công khai

Bây giờ là phần cuối cùng, hiển thị dữ liệu công khai. Chúng tôi sẽ hiển thị dữ liệu được đặt thành có trong chỉ mục của chúng tôi. php, trong đó là trang dành cho người dùng không được xác thực. Nó rất đơn giản. Chúng tôi chỉ cần chỉnh sửa chỉ mục của chúng tôi. php và thêm một số mã php và bảng. Đây là chỉ số cập nhật của chúng tôi. php

mục lục. php

Thiết lập dự án PHP

Bây giờ hãy đăng xuất và xem trang mặc định của bạn. Nó sẽ trông giống như thế này

Thiết lập dự án PHP

Ghi chú. Bạn sẽ chưa thấy dữ liệu vì chúng tôi chưa đặt bất kỳ thông tin nào ở chế độ công khai

Bây giờ, hãy đăng nhập lại và lần này, hãy thêm một số dữ liệu. Trong trường hợp của tôi, tôi đã thêm vào như sau

  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    18 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    4
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    20 - phi_______26_______4
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    22 -
    <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            <h2>Registration Page</h2>
            <a href="index.php">Click here to go back<br/><br/>
            <form action="checklogin.php" method="POST">
               Enter Username: <input type="text" 
               name="username" required="required" /> <br/>
               Enter password: <input type="password" 
               name="password" required="required" /> <br/>
               <input type="submit" value="Register"/>
            </form>
        </body>
    </html>
    
    php
    if($_SERVER["REQUEST METHOD"] == "POST"){
        $username = mysql_real_escape_string($_POST['username']);
        $password = mysql_real_escape_string($_POST['password']);
    
        echo "Username entered is: ". $username . "
    "
    ; echo "Password entered is: ". $password; } ?>
    4
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    24 - công cộng
  • <html>
        <head>
            <title>My first PHP Website</title>
        </head>
        <body>
            php
                echo "

    Hello World!

    "
    ; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
    25 - không phải_______26_______4

Với tổng cộng 6 dữ liệu với 3 trong số mỗi cài đặt quyền riêng tư

Trang Chủ. php

Thiết lập dự án PHP

Bây giờ, hãy đăng xuất và xem trang mặc định của chúng tôi (chỉ mục. php). Bây giờ nó sẽ trông như thế này

mục lục. php

Thiết lập dự án PHP

Như bạn có thể thấy, nó chỉ hiển thị dữ liệu được đặt thành

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        <h2>Registration Page</h2>
        <a href="index.php">Click here to go back<br/><br/>
        <form action="checklogin.php" method="POST">
           Enter Username: <input type="text" 
           name="username" required="required" /> <br/>
           Enter password: <input type="password" 
           name="password" required="required" /> <br/>
           <input type="submit" value="Register"/>
        </form>
    </body>
</html>

php
if($_SERVER["REQUEST METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);

    echo "Username entered is: ". $username . "
"
; echo "Password entered is: ". $password; } ?>
4

Xin chúc mừng. Chúng tôi cuối cùng đã hoàn thành các hướng dẫn cho phiên này

13) Giải thích và Tóm tắt

Cuối cùng, bạn sẽ có các tệp sau

Thiết lập dự án PHP

Như tôi đã đề cập trước đó, tôi sẽ tải lên thành phẩm. Bấm vào đây

Tôi cũng đã tải lên tệp SQL cho cơ sở dữ liệu trong trường hợp bạn muốn sử dụng dữ liệu chính xác mà tôi có. Để nhập tệp SQL, bạn chỉ cần nhấp vào xuất và chọn tệp SQL nhưng hãy đảm bảo tạo cơ sở dữ liệu trước. Nếu bạn thích, tôi có thể tạo một blog riêng cho điều đó. Chỉ cần cho tôi biết trong phần bình luận

câu hỏi dự đoán

Đây là những câu hỏi mà tôi nghĩ bạn sẽ hỏi. Tôi đã cố gắng lường trước những điều này để tránh nhầm lẫn

  • Bất cứ khi nào tôi đăng ký một người dùng mới, cùng một danh sách sẽ xuất hiện. Tôi đã mong đợi đối với mỗi người dùng khác nhau, sẽ có một danh sách riêng cho họ. Tôi đã làm gì sai sao?

Đó thực sự là một câu hỏi hay. Như bạn có thể thấy, tôi chỉ dạy các chức năng, xác thực và bảo mật CRUD CƠ BẢN chứ không phải hệ thống thành viên. Tạo một danh sách riêng cho mỗi người dùng yêu cầu một bảng riêng cho mỗi người trong số họ. Vì vậy, logic đằng sau nó là mỗi lần sau khi bạn chèn dữ liệu, bạn sẽ chèn nó vào một bảng khác và có mối quan hệ với nhau. Bạn có thể sử dụng cú pháp

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
28 để tạo một bảng riêng cho người dùng giống như thế này

SQL

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
    <body>
        php
            echo "

Hello World!

"
; ?> <a href="login.php"> Click here to login <a href="register.php"> Click here to register </body> </html>
8

Bạn có thể bấm vào đây để biết cú pháp Tạo bảng

Lý do tại sao tôi không làm phần đó là vì bạn sẽ phải thực hiện nhiều truy vấn và nó sẽ làm cho mã của tôi trông phức tạp, điều này có thể tiêu tốn nhiều thời gian để phát triển nó nên chắc chắn nó nằm ngoài phạm vi của tôi. Sẽ tốt hơn nếu bạn chỉ cần tự mình thử và tìm ra cách. Rốt cuộc, mọi thứ bạn cần đều đã có trong blog này

  • Nó sẽ được dễ dàng hơn để sử dụng các trang chính?

Có, việc sử dụng các trang chính sẽ dễ dàng hơn. Lý do tại sao tôi không sử dụng nó là để độc giả của chúng tôi dễ dàng hiểu logic đằng sau nó. Các trang chính có thể khiến người mới bắt đầu hơi bối rối

  • Tại sao không sử dụng một khuôn khổ?

Sau đó, một lần nữa, tôi đã đề cập trong tiêu đề Tạo chương trình PHP đầu tiên của bạn TỪ SCRATCH. Thật tuyệt khi bắt đầu từ con số không mà từ một "mẫu" để người đọc của chúng tôi có thể thuộc lòng mã

  • Tôi biết rằng đây có thể là một cách cũ để phát triển PHP. Tôi đã nghe nói rằng có những tính năng mới được phát hành

Tôi chỉ trình bày dựa trên sự thành thạo của mình. Tôi đã làm việc với PHP trong một thời gian dài và tôi có thể không nghe nói về các tính năng mới. Nếu bạn nghĩ cái mới làm cho nó trông đẹp, thì hãy dùng thử

  • Tại sao không tạo một tệp riêng cho CSS và JavaScript?

Đơn giản, tôi chỉ trình bày cho bạn về các chức năng. Quan tâm nhiều hơn đến mã back-end hơn là front-end. Tôi thực sự đã không tập trung vào thiết kế. Nếu bạn hỏi cách thiết kế mã của mình bằng PHP, thì nó cũng dễ như tạo kiểu cho một tệp HTML thông thường

Vâng, tôi đoán rằng kết luận tất cả mọi thứ. Cảm ơn các bạn. Tôi hy vọng bạn đã học được điều gì đó từ chủ đề này

Xin nhắc lại, trong trường hợp bạn muốn học cách tạo ASP của riêng mình. NET MVC 5 từ đầu, bạn có thể kiểm tra ngay tại đây. Theo dõi tôi trên Twitter và bạn có thể tweet phản hồi và nhận xét của mình về một trong hai bài viết

Giấy phép

Bài viết này, cùng với bất kỳ mã nguồn và tệp liên quan nào, được cấp phép theo Giấy phép Mở Dự án Mã (CPOL)

Làm cách nào để chạy một dự án trong PHP?

Bây giờ, để chạy tập lệnh PHP. .
Chuyển đến “C. \xampp\htdocs” và bên trong nó, tạo một thư mục. .
Trong thư mục demo, tạo một tệp văn bản mới và đặt tên là “index. .
Bây giờ, để xem đầu ra của tập lệnh, hãy mở bảng điều khiển XAMPP và khởi động Apache để lưu trữ máy chủ web cục bộ, nơi tập lệnh của chúng ta sẽ chạy

Làm cách nào để thiết lập MySQL với PHP?

Tạo nên cơ sở dữ liệu
Tạo thư mục trong htdocs
Tạo tệp kết nối cơ sở dữ liệu trong PHP
Tạo tệp php mới để kiểm tra kết nối cơ sở dữ liệu của bạn
chạy nó

Làm thế nào để xây dựng một ứng dụng PHP?

Để làm cho một ứng dụng PHP duy nhất có thể truy cập được tại URL gốc của máy chủ Web (ví dụ: http. //localhost), chỉ cần sao chép các tệp PHP vào thư mục /opt/bitnami/apache2/htdocs. Để biết ví dụ, hãy xem ví dụ về phpinfo

Làm thế nào để xây dựng và triển khai trang web PHP?

Triển khai các ứng dụng PHP trong vòng vài phút. Bốn công cụ tự động hóa để cắt giảm thời gian triển khai .
Tạo tài khoản DeployHQ
Tạo một dự án mới trong DeployHQ
Kết nối DeployHQ với kho mã của bạn
Thêm đường dẫn của kho lưu trữ Github
Cấu hình máy chủ
Triển khai