PHP header() ফাংশন
সংজ্ঞা ও ব্যবহার
header() ফাংশন ক্লায়েন্টকে প্রাথমিক এইচটিপি হেডার পাঠায়。
একটি বিশেষ কথা সম্পর্কে উপলব্ধি করুন যে header() ফাংশনটির কল করা উচিত যখন কোনো বাস্তবিক ইউটপুট পাঠানো হয়েছে (PHP 4 এবং উচ্চতর সংস্করণে PHP 4 এবং উচ্চতর সংস্করণে আপনি ইউটপুট ক্যাশ ব্যবহার করতে পারেন):
<html> <?php // ফলাফল ভুল // header() কলের আগে ইউটপুট পূর্ববর্তীভাবে স্থাপিত header('Location: http://www.example.com/'); ?>
সংজ্ঞা
header(string,replace,http_response_code)
পারামিটার | বর্ণনা |
---|---|
string | অপরিহার্য।যাকে পাঠাতে হবে হেডার স্ট্রিংকে নির্দিষ্ট করে |
replace |
অপশনাল।এটি বলে দেয় যে হেডারটি পূর্ববর্তী হেডারকে প্রতিস্থাপন করবে কিংবা দ্বিতীয় হেডার যোগ করবে。 ডিফল্ট হলো true (প্রতিস্থাপন)।false (একই ধরনের একাধিক হেডারকে অনুমদন)। |
http_response_code | Optional. Force the HTTP response code to the specified value. (Available in PHP 4 and above) |
Tips and Comments
Note:From PHP 4.4 onwards, this function prevents the sending of multiple headers at once. This is a protective measure against header injection attacks.
Example
Example 1
<?php // Date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache"); ?> <html> <body> ... ...
Note:Users may set some options to change the browser's default cache settings. By sending the above headers, you can override any of these settings and force the browser not to cache!
Example 2
Prompt the user to save a generated PDF file (The Content-Disposition header is used to provide a recommended filename and force the browser to display a save dialog):
<?php header("Content-type:application/pdf"); // The file will be called downloaded.pdf header("Content-Disposition:attachment;filename='downloaded.pdf'"); // PDF source is in original.pdf readfile("original.pdf"); ?> <html> <body> ... ...
Note:Microsoft IE 5.5 has a bug that prevents the above mechanism. This bug can be resolved by upgrading to Service Pack 2 or a higher version.