PHP/HTML/header — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
- 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
// Tell the web client to expect a CSV file
header("Content-Type: text/csv");
Changing the page type to XML
header("Content-Type: text/xml");
Forcing File "Save As" Downloads
<?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();
}
?>
Good and bad Location headers
<?php
// Good Redirect
header("Location: http://www.wbex.ru");
?>
If a User Is Not Logged in, the Login Form Is Loaded
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: /protected3/login.php?url=" . urlencode($_SERVER["SCRIPT_NAME"]));
}
?>
Logged In with cookie
<?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"><br>Visit another page on this site ?</a>
</body>
</html>
Mime types
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
Reading Queued Headers
<?
header("Expires: Sat, 22 Dec 1979 05:30:00 GMT");
echo "This is some text for output.<br />";
if (!headers_sent($filename, $linenum)) {
header("Location: www.yoursite.ru");
exit;
} else {
echo "Headers already sent in $filename on line $linenum.<br />";
echo "Headers sent are:<br /> <UL>";
$headers = headers_list( );
foreach($headers as $header) {
echo "<LI>$header</LI>";
}
echo "</UL>";
exit;
}
?>
Redirect header
<?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>
Redirecting with query string variables
<?php
header("Location: http://www.example.ru/?monkey=turtle");
exit();
?>
Sending an XML response
<?php header("Content-Type: text/xml"); ?>
<menu>
<dish type="appetizer">Chicken Soup</dish>
<dish type="main course">Fried Monkey Brains</dish>
</menu>
Sending Content Types Other Than HTML
<html>
</head>
<body>
<div align="center">
<img src="index.php" alt="" title="" style="border: none;" />
</div>
</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)
Setting a Cookie Using the header() Function
<?php
header("Set-Cookie: mycookie=myvalue; path=/; domain=.demo.org");
?>
Setting character encoding
<?php
header("Content-Type: text/html;charset=utf-8");
?>
Using header() to Send Raw Headers
<?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?><br />
<form method="post" action="<?php
print $_SERVER ["PHP_SELF"]?>">
<p><input type="hidden" name="num_tries"
value="<?php
print $num_tries?>" /> Number: <input
type="text" name="guess" value="<?php print $guess?>" /></p>
</form>
</body>
</html>