PHP/HTML/header
Содержание
- 1 Changing the page type to CSV
- 2 Changing the page type to XML
- 3 Forcing File "Save As" Downloads
- 4 Good and bad Location headers
- 5 If a User Is Not Logged in, the Login Form Is Loaded
- 6 Logged In with cookie
- 7 Mime types
- 8 Reading Queued Headers
- 9 Redirect header
- 10 Redirecting with query string variables
- 11 Sending an XML response
- 12 Sending Content Types Other Than HTML
- 13 Setting a Cookie Using the header() Function
- 14 Setting character encoding
- 15 Using header() to Send Raw Headers
Changing the page type to CSV
<source lang="html4strict">
// Tell the web client to expect a CSV file header("Content-Type: text/csv");
</source>
Changing the page type to XML
<source lang="html4strict">
header("Content-Type: text/xml");
</source>
Forcing File "Save As" Downloads
<source lang="html4strict">
<?php
$path = "images/winter.jpg"; try { if (is_file ($path)){ if ($file = fopen($path, "rb")) { while(!feof($file) and (connection_status()==0)) { $f .= fread($file, 1024*8); } fclose($file); } $outputname = "myimage"; header ("Content-type: image/jpeg"); header("Content-disposition: attachment; filename=".$outputname.".jpg"); print $f; } else { throw new exception ("Sorry, file path is not valid."); } } catch (exception $e){ echo $e->getmessage(); }
?>
</source>
Good and bad Location headers
<source lang="html4strict">
<?php // Good Redirect header("Location: http://www.wbex.ru"); ?>
</source>
If a User Is Not Logged in, the Login Form Is Loaded
<source lang="html4strict">
<?php session_start(); if (!isset($_SESSION["username"])) {
header("Location: /protected3/login.php?url=" . urlencode($_SERVER["SCRIPT_NAME"]));
} ?>
</source>
Logged In with cookie
<source lang="html4strict">
<?php
$auth = $_COOKIE["auth"]; header("Cache-Control:no-cache"); if( ! $auth == "ok" ) { header("Location:login.php" ); exit(); }
?>
<html>
<head> <title>Logged In</title> </head> <body> You are logged in and can access all pages on this web site. <a href="anotherpage.php">
Visit another page on this site ?</a> </body>
</html>
</source>
Mime types
<source lang="html4strict">
application/msexcel Microsoft Excel data file
application/msword Microsoft Word data file
application/octet-stream Generic binary file
application/pdf Adobe PDF
application/x-shockwave-flash Macromedia Flash
application/zip Zip file
audio/mp3 MP3
audio/wav Wave sound file
audio/x-ogg Ogg file
font/ttf TrueType Font
image/bmp MS Windows .bmp image
image/gif GIF image
image/jpeg JPEG image
image/png PNG image
image/tiff TIFF image
image/svg+xml Scalable Vector Graphic (SVG)
text/html HTML file
text/plain Plain text
text/rtf Rich-Text File
text/tab-separated-values Tab-Separated Values (TSV)
text/xml XML
video/mpeg MPEG video
video/quicktime Quicktime video
</source>
Reading Queued Headers
<source lang="html4strict">
<?
header("Expires: Sat, 22 Dec 1979 05:30:00 GMT"); echo "This is some text for output.echo "Headers sent are:
"; if (!headers_sent($filename, $linenum)) { header("Location: www.yoursite.ru"); exit; } else { echo "Headers already sent in $filename on line $linenum.
";
- ";
$headers = headers_list( );
foreach($headers as $header) {
echo "
- $header ";
}echo "";
exit; }
?>
</source>
Redirect header
<source lang="html4strict">
<?php
$location = $_POST["location"]; $self = $_SERVER["PHP_SELF"]; if( $location != null ) { header( "Location:$location") ; exit(); }
?>
<html>
<head> <title>Redirect</title> </head> <body> Choose a site to visit: <form action = "<?php $self ?>" method = "post"> <select name = "location"> <option value = "http://www.google.ru">Google</option> <option value = "http://www.ebay.ru">Ebay</option> </select> <input type = "submit" name = "submit" value = "Go"> </form> </body>
</html>
</source>
Redirecting with query string variables
<source lang="html4strict">
<?php header("Location: http://www.example.ru/?monkey=turtle"); exit(); ?>
</source>
Sending an XML response
<source lang="html4strict">
<?php header("Content-Type: text/xml"); ?> <menu>
<dish type="appetizer">Chicken Soup</dish> <dish type="main course">Fried Monkey Brains</dish>
</menu>
</source>
Sending Content Types Other Than HTML
<source lang="html4strict">
<html> </head> <body>
<img src="index.php" alt="" title="" style="border: none;" />
</body> </html> <?php
$path = "myImage.jpg"; try { if (is_file ($path)){ if ($file = fopen($path, "rb")) { while(!feof($file) and (connection_status()==0)) { $f .= fread($file, 1024*8); } fclose($file); } header ("Content-type: image/jpeg"); print $f; } else { throw new exception ("Sorry, file path is not valid."); } } catch (exception $e){ $animage = imagecreate (500, 500); $red = imagecolorallocate ($animage, 255, 0, 0); $white = imagecolorallocate ($animage, 255, 255, 255); imagefilledrectangle ($animage, 0, 0, 500, 500, $white); imagestring ($animage, 4, ((500 - (strlen($e->getmessage()) * imagefontwidth(4))) / 2), 5, $e->getmessage(), $red); imagejpeg ($animage); header ("Content-type: image/jpeg"); imagedestroy ($animage); }
?> Common File Format Content Types Content Type Application application/pdf Adobe Portable Document Format (PDF) types application/msword Microsoft Word documents application/excel Microsoft Excel documents image/gif GIF images image/png PNG images application/octet-stream Zip files text/plain Plain text (text files)
</source>
Setting a Cookie Using the header() Function
<source lang="html4strict">
<?php
header("Set-Cookie: mycookie=myvalue; path=/; domain=.demo.org");
?>
</source>
Setting character encoding
<source lang="html4strict">
<?php header("Content-Type: text/html;charset=utf-8"); ?>
</source>
Using header() to Send Raw Headers
<source lang="html4strict">
<?php
$num_to_guess = 42; $message = ""; if (! isset ( $_POST ["guess"] )) { $message = "Welcome!"; } else if ($_POST ["guess"] > $num_to_guess) { $message = $_POST ["guess"] . " is too big!"; } else if ($_POST ["guess"] < $num_to_guess) { $message = $_POST ["guess"] . " is too small!"; } else { header ( "Location:congrats.html" ); exit (); } $guess = ( int ) $_POST ["guess"]; $num_tries = ( int ) $_POST ["num_tries"]; $num_tries ++; ?>
<html> <head> <title>A PHP Number Guessing Script</title> </head> <body>
<?php print $message?> Guess: <?php print $num_tries?>
<form method="post" action="<?php print $_SERVER ["PHP_SELF"]?>">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>" /> Number: <input type="text" name="guess" value="<?php print $guess?>" />
</form> </body> </html>
</source>