Professional Documents
Culture Documents
Web Engineering LW PHP Part C
Web Engineering LW PHP Part C
Lecture PHP
Instructor
Dr. Mariam Rehman
Professor
function writeName($fname)
{
echo $fname;
}
1. <html>
2. <body> PHP Functions - Parameters Example 1
3. <?php
4. function writeName($fname)
5. {
6. echo $fname;
7. }
8. echo "My name is ";
9. writeName(“Samander Khan");
10.echo "My sister's name is ";
11.writeName(“Gul Lala");
12.echo "My brother's name is ";
13.writeName(“Derya Khan");
14.?>
15.</body>
16.</html>
1. <html>
2. <body> PHP Functions - Parameters Example 1
3. <?php
4. function writeName($fname, $punctuation)
5. {
6. echo $fname . " Faraz" . $punctuation . "<br />";
7. }
8. echo "My name is ";
9. writeName(“Ali",".");
10.echo "My sister's name is ";
11.writeName(“Ammara","!");
12.echo "My brother's name is ";
13.writeName(“Usama","?");
14.?>
15.</body>
16.</html>
1. <html>
PHP Functions – Return Value Exp 3
2. <body>
3. <?php
4. function add($x,$y)
5. {
6. $total=$x+$y;
7. return $total;
8. }
9. echo "1 + 16 = " . add(1,16);
10.?>
11.</body>
12.</html>
PHP GET & POST
PHP GET Cont.
• The built-in $_GET function is used to collect values
from a form sent with method="get".
• Information sent from a form with the GET method is
visible to everyone (it will be displayed in the
browser's address bar)
• The GET method is restricted to send upto 1024
characters only.
• Never use GET method if you have password or other
sensitive information to be sent to the server.
PHP GET Cont.
• GET can't be used to send binary data, like images or word
documents, to the server.
• The data sent by GET method can be accessed using
QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all the
sent information using GET method.
• The GET method sends the encoded user information
appended to the page request. The page and the encoded
information are separated by the ? character.
• http://www.test.com/index.php?
name1=value1&name2=value2
PHP GET --- Example
1. <?php
2. if( $_GET["name"] || $_GET["age"] ) {
3. echo "Welcome ". $_GET['name']. "<br />";
4. echo "You are ". $_GET['age']. " years old.";
5.
6. exit();
7. }
8. ?>
9. <html>
10. <body>
11.
12. <form action = "<?php $_PHP_SELF ?>" method = "GET">
13. Name: <input type = "text" name = "name" />
14. Age: <input type = "text" name = "age" />
15. <input type = "submit" />
Output:
16. </form>
17.
18. </body> Welcome Ali
19.</html> Your are 22 years old.
PHP POST Cont.
• The built-in $_POST function is used to collect values
from a form sent with method="post".
• Information sent from a form with the POST method
is invisible to others.
• There is no limits on the amount of information to
send.
• http://www.test.com/index.php
PHP POST Cont.
• The POST method can be used to send ASCII as well
as binary data.
• The data sent by POST method goes through HTTP
header so security depends on HTTP protocol.
• By using Secure HTTP you can make sure that your
information is secure.
• The PHP provides $_POST associative array to access
all the sent information using POST method.
PHP GET --- Example
1. <?php
2. if( $_POST["name"] || $_POST["age"] ) {
3. if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
4. die ("invalid name and name should be alpha");
5. }
6. echo "Welcome ". $_POST['name']. "<br />";
7. echo "You are ". $_POST['age']. " years old.";
8.
9. exit();
10. }
11.?>
12.<html>
13. <body>
14.
15. <form action = "<?php $_PHP_SELF ?>" method = "POST">
16. Name: <input type = "text" name = "name" />
17. Age: <input type = "text" name = "age" />
18. <input type = "submit" />
19. </form>
20.
21. </body>
22.</html>
PHP File Inclusion
PHP File Inclusion Cont.
Hello World!
Error Example include() Function
1. <html>
2. <body>
3. <?php
4. require("wrongFile.php")echo "Hello World!";
5. ?>
6. </body> ERROR MESSAGE
7. </html>
Warning: require(wrongFile.php) [function.require]:
failed to open stream: No such file or directory in C:\
home\website\test.php on line 5
• Opening a file
• Reading a file
• Writing a file
• Closing a file
PHP Files and I/O --- Open Files Cont.
• PHP fopen() function is used to open a file.
• The first parameter of this function contains the name of the file to be
opened and
• the second parameter specifies in which mode the file should be
opened:
1. <html>
2. <body>
3. <?php
4. $file=fopen("welcome.txt","r");
5. ?>
6. </body>
7. </html>
PHP Files and I/O --- Open Files Cont.
• R Read only. Starts at the beginning of the file
• r+ Read/Write. Starts at the beginning of the file
• W Write only. Opens and clears the contents of file; or creates a new file if
it doesn't exist
• w+ Read/Write. Opens and clears the contents of file; or creates a new
file if it doesn't exist
• A Append. Opens and writes to the end of the file or creates a new file if
it doesn't exist
• a+ Read/Append. Preserves file content by writing to the end of the file
• X Write only. Creates a new file. Returns FALSE and an error if file already
exists
• x+ Read/Write. Creates a new file. Returns FALSE and an error if file
already exists
PHP Files and I/O --- Open Files
Example
2. <head>
3. <title>Reading a file using PHP</title>
4. </head>
5. <body>
6. <?php
7. $filename = "tmp.txt";
8. $file = fopen( $filename, "r" );
9. if( $file == false ) {
10. echo ( "Error in opening file" );
11. exit();
12. }
13. $filesize = filesize( $filename );
14. $filetext = fread( $file, $filesize );
15. fclose( $file );
16.
17. echo ( "File size : $filesize bytes" );
18. echo ( "<pre>$filetext</pre>" );
19. ?>
20. </body>
21. </html>
PHP Files and I/O --- Write File
Example
2. $filename = "/home/user/guest/newfile.txt";
3. $file = fopen( $filename, "w" );
4. if( $file == false ) {
5. echo ( "Error in opening new file" );
6. exit(); 19. if( $file == false ) {
7. } 20. echo ( "Error in opening file" );
8. fwrite( $file, "This is a simple test\n" ); 21. exit();
9. fclose( $file ); 22. }
10. ?> 23.
11. <html> 24. $filesize = filesize( $filename );
12. <head> 25. $filetext = fread( $file, $filesize );
13. <title>Writing a file using PHP</title> 26.
14. </head> 27. fclose( $file );
15. <body> 28.
16. <?php 29. echo ( "File size : $filesize bytes" );
17. $filename = "newfile.txt"; 30. echo ( "$filetext" );
18. $file = fopen( $filename, "r" ); 31. echo("file name: $filename");
32. ?>
33.
34. </body>
35. </html>
PHP Session Management
Session Management
• <?php
• session_destroy();
• ?>
Session Management --- Destroying Session
– <?php
– unset($_SESSION['views']);
– ?>
•
PHP EMAIL HANDLING
PHP EMAIL HANDLING Cont.
• PHP mail() function is used to send emails from inside a script.
• This function requires three mandatory arguments that
specify
– recipient's email address,
– subject of the message and
– actual message
• additionally there are other two optional parameters.
• mail(to,subject,message,headers,parameters)
PHP EMAIL HANDLING Cont.
PHP EMAIL HANDLING Cont.
• Simplest way to send an email with PHP is to send a text
email.
• First declare the variables ($to, $subject, $message, $from,
$headers)
1. <?php
2. $to = "someone@example.com";
3. $subject = "Test mail";
4. $message = "Hello! This is a simple email message.";
5. $from = "someonelse@example.com";
6. $headers = "From: $from";
7. mail($to,$subject,$message,$headers);
8. echo "Mail Sent.";
9. ?>
• HOW can you send email with attachment?
• What is the role of php.ini file?
Practice
• Practice all Content discussed in this Slide
• Embed code in html forms so you can get real look of
professional website.
• Go through all topics on you tube.
Reading and Practice
• https://www.tutorialspoint.com/php/php_files.htm
• https://codeforgeek.com/2014/08/session-handling-php/
• https://www.w3schools.com/php/func_mail_mail.asp
• https://www.youtube.com/watch?v=h6KID8n0zCU