Data Retention,Using Image in forms and Uploading Files
Data Retention,Using Image in forms and Uploading Files
But what if you have 20 elements and
just one was missing?
Data retention...
Data Retention
Create an extra string to store the value
if($_POST["n"] == NULL)
$boo_n = 1;
else $strStore_n = $_POST["n"];
See php_imagefields.php
File Upload Form
upload.html:
upload.html
<html>
<head><title>PHP File Upload Form</title></head>
<body>
File:<input type="file"
file name="userfile"><br>
userfile
</form>
</body> </html>
Character Encoding of Form-
Data
<form enctype=“value”>
enctype
• The enctype attribute specifies how form-data should be encoded
before sending it to the server.
• This means that all characters are encoded before they are sent to
the server
<form enctype=“value”>
enctype
Value Description
application/x-www-form- • All characters are encoded before
urlencoded sent (default setting)
multipart/form-data • No characters are encoded.
• This value is required when you are
using forms that have a file upload
control
text/plain • Spaces are converted to "+"
symbols
• Special characters are not encoded
File Upload Form
Label is automatically assigned
Receiving files
• $_FILES['userfile']['tmp_name']
– name of the temporary copy of the file stored on the
server.
• $_FILES['userfile']['name']
– name of uploaded file.
• $_FILES['userfile']['size']
– size of the uploaded file (in bytes).
• $_FILES['userfile']['type']
– MIME type of the file such as image/gif.
• $_FILES['userfile']['error']
– error that may have been generated as a result of the
upload.
Upload error check
$userfile_error = $_FILES[
$_FILES 'userfile']['error'];
'userfile'
if ($userfile_error > 0)
0 {
echo 'Problem: ';
switch ($userfile_error){
case 1:
echo 'File exceeded upload_max_filesize';
break;
case 2:
echo 'File exceeded max_file_size';
break;
case 3:
echo 'File only partially uploaded';
break;
case 4:
echo 'No file uploaded';
break;
}
PHP.ini : upload_max_filesize = 2M
exit;
} HTML form : MAX_FILE_SIZE directive.
bool is_uploaded_file ( string $filename )
• This is useful to help ensure that a malicious user hasn't tried to trick the
script into working on files upon which it should not be working
--for instance, /etc/passwd.
bool move_uploaded_file ( string $filename ,
string $destination )
Note: The target folder must exist for this example to work! See Upload.html, upload.php
Direct header manipulation
header(text);
header
ImagePng($image);
Imagedestroy($image);
?>
bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
<?php
header("Content-type: image/png");
$image = imagecreate(580, 280) or die("Failed to create");
$bgcolour = ImageColorAllocate($image, 80, 200, 255);
$fgcolour = ImageColorAllocate($image, 255, 255, 100);
e.g., in it026945:
<?php
header("Content-type: image/png");
$image = imagecreate(580, 280) or die("Failed to create");
$bgcolour = ImageColorAllocate($image, 80, 200, 255);
$fgcolour = ImageColorAllocate($image, 255, 255, 100);
?>
PHP redirection
Use header() to tell client to load another
URL,
e.g.
<?php
$url = “http://www.nzherald.co.nz”;
header(“Location: “ + $url);
?>
Prevent page caching:
<?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:
There are options that users may set to change the browser's default caching
settings.
By sending the headers above, you should override any of those settings and
force the browser not to cache!
Screen scrapers
$webpage = file_get_contents(‘http://www.example.com’);
Reads entire file into a string
string file_get_contents ( string $filename
[, bool $use_include_path = false
[, resource $context
[, int $offset = -1
[, int $maxlen = -1 ]]]] )
Simple Mail Transfer Protocol
Comes under the application level in the Internet
protocol stack
Works similar to HTTP where email
transactions involve the exchange of
request/response strings
Sending email
Involves a three way interaction
between sender, recipient, and email
client
Email client sends request strings to
smtp server and gets back
response strings
Sample email sending session
1. Client establishes connection to SMTP server
Server sends 220 level response string
4. Client sends rcpt to: request to tell server who to send the email to
Server responds with 250 rcpt ok
S: 220 hamburger.edu
C: HELO crepes.fr
S: 250 Hello crepes.fr, pleased to meet you
C: MAIL FROM: <alice@crepes.fr>
S: 250 alice@crepes.fr... Sender ok
C: RCPT TO: <bob@hamburger.edu>
S: 250 bob@hamburger.edu ... Recipient ok
C: DATA
S: 354 Enter mail, end with "." on a line by itself
C: Do you like ketchup?
C: How about pickles?
C: .
S: 250 Message accepted for delivery
C: QUIT
S: 221 hamburger.edu closing connection
Sending e-mail with PHP
mail(to,
mail subject, message, headers)
[mail function]
SMTP = smtp.hotmail.com
sendmail_from = me@hotmail.com
<html>
<head>
<title>Invitation</title>
<body>
<h1> Are you going to the party? </h1>
</form>
</body></html>
mail(to,subject,message,headers,parameters)
Parameter Description
to Required. Specifies the receiver / receivers of the email
subject Required. Specifies the subject of the email.
Note: This parameter cannot contain any newline characters
message Required. Defines the message to be sent.
Each line should be separated with a LF (\n).
Lines should not exceed 70 characters
headers Optional. Specifies additional headers, like From, Cc, and Bcc.
The additional headers should be separated with a CRLF (\r\n)
parameters Optional. Specifies an additional parameter to the sendmail
program
response.php
<?php
$mailto = "a_______@localhost";
$subject = "Party RSVP";
$message = "";
$comment = $_POST['comment'];
if ($comment == "*type comments in here*") {
$comment = "I have no comment";
}
$willgo = $_POST['attend'];
if ($willgo == "Y") {
$message .= "Yes I am going\n";
}
elseif ($willgo == "N") {
$message .= "No!\n";
}
$message .= "$comment\n";
mail($mailto,
mail $subject, $message, $headers);
Extending SMTP
SMTP is just a text sending/receiving
protocol (like HTTP)
To send other types of data (e.g. graphics
attachments), we need an additional
protocol
Multipurpose Internet Mail Extensions
MIME is an addition to the standard protocols
that just sends simple text messages
Content type
The key feature of MIME is the content-type
identifier
Each data segment in a complex email is
preceded by a number of content specification
headers:
Of course, the client needs to understands
these terms
PHP and MIME
PHP itself does not have support for
sending emails with attachments
A number of 3rd party libraries have
been developed for this
See the PEAR repository at:
http://pear.php.net
References