Trong bài này chúng ta tìm hiểu về hàm header, đây là một hàm được dùng khá nhiều trong lập trình web, ví dụ như dùng để chuyển hướng trang, dùng để khai báo định dạng file trả về từ Server, …

Trong tiếng anh thì có định nghĩa như sau: header() is used to send a raw HTTP header, khái niệm này bạn hãy tự dịch ra chứ mình cũng không biết dịch sao cho sát nghĩa nữa :3

Cú pháp của hàm header như sau:


header ($string, $replace = true, $http_response_code = null) {}


Trong đó:

  • $string: Chuỗi khai báo cho kết quả trả về từ Server, chuỗi này đóng vai trò quan trọng nhất vì nội dung của nó sẽ quyết định header sẽ làm gì.
  • $replace: Tham số này mặc định là true, nghĩa là định dạng của chuỗi $string sẽ được replace chứ không phải khai báo mới (trường hợp khai báo nhiều header).
  • $http_response_code: Mã code trả về từ Server. VD 404 là not found 301 là chuyển hướng có chủ đích

Lưu ý: Cũng như Session trong PHP, bạn phải chắc chắn rằng ở phía trên đoạn code sử dụng header không được xuất ra bất kì một ký tự nào, vì vậy thông thường chúng ta đặt header ở phía trên cùng của file, nơi mà chưa có những đoạn mã HTML.

Và bây giờ chúng ta tìm hiểu một số công dụng của hàm header nhé.


  1. Wow, amazing blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your web site is great, as well as the content!

  2. I will immediately grab your rss as I can’t in finding your email subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me recognise so that I could subscribe.

  3. Excellent post but I was wanting to know
    if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little bit further.
    Appreciate it!

  4. I don’t know whether it’s just me or if perhaps everyone else experiencing problems
    with your website. It appears as if some of the text within your posts are running off the
    screen. Can somebody else please provide feedback and let me
    know if this is happening to them as well? This may be a problem
    with my internet browser because I’ve had this happen before.

  5. Awesome things here. I am very glad to peer your article.
    Thanks so much and I’m having a look ahead to contact you.
    Will you kindly drop me a mail?

  6. I absolutely love your blog.. Great colors & theme.
    Did you create this site yourself? Please reply back as I’m trying to create my
    very own site and would like to learn where you got this from or just what the theme is named.

  7. Attractive section of content. I just stumbled upon your web
    site and in accession capital to assert that I acquire actually enjoyed account
    your blog posts. Anyway I will be subscribing to your
    feeds and even I achievement you access consistently rapidly.

  8. Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much.

    I hope to give something back and help others like you helped me.

  9. It’s really a cool and helpful piece of information. I’m satisfied
    that you simply shared this helpful information with us. Please stay us
    informed like this. Thank you for sharing.

  10. What i don’t understood is actually how you’re not actually much
    more smartly-preferred than you might be now.
    You are so intelligent. You recognize thus significantly relating to this topic, produced me individually imagine it from so many varied angles.
    Its like women and men are not fascinated except it is something to accomplish with Lady gaga!
    Your individual stuffs nice. Always handle it up!

  11. Attractive section of content. I just stumbled upon your web site and in accession capital
    to assert that I get actually enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access
    consistently rapidly.

  12. It’s a shame you don’t have a donate button! I’d certainly donate to
    this brilliant blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to
    my Google account. I look forward to new updates and will share this website with my
    Facebook group. Talk soon!


Please enter your comment!
Please enter your name here